小编Gen*_*cos的帖子

Haskell无意义的性能 - 有效地将多个函数映射到相同的数据

我经常需要将多个函数映射到相同的数据.我已经实现了dpMap来为我做这个

dpMap fns = (`map` fns) . flip ($)
Run Code Online (Sandbox Code Playgroud)

dpMap是一个函数,这是否意味着我只读取数据dt一次(就像一个输入相同输入的电路.一个无意义的系统提醒一个电路;只是管道没有数据)?

例如,考虑计算列表dt的最小值和最大值.

minimax dt = (dpMap [minimum, maximum]) dt
Run Code Online (Sandbox Code Playgroud)

(我可以摆脱dt但必须使用-XNoMonomorphismRestriction)

相比于像这样的点完全形式实现相同的函数是否有性能优势?:

minimax2 dt = [minimum dt, maximum dt]
Run Code Online (Sandbox Code Playgroud)

编辑:是否有一个dpMap的一般实现,它适用于常量内存?

我找到了另一篇不错的博文:http://www.haskellforall.com/2013/08/composable-streaming-folds.html ;希望这会有所帮助.

编辑2:经过一些更多的上下文,这里有一个解决方案,即使我没有dpMap的确切实现,模式很简单,它不保证单独的功能:

minimax = (,) <$> minimum <*> maximum
Run Code Online (Sandbox Code Playgroud)

用法:

> minimax [1..100]
(1,100)
Run Code Online (Sandbox Code Playgroud)

如果你还想计算总和和长度

func = (,,,) <$> minimum <*> maximum <*> sum <*> length
Run Code Online (Sandbox Code Playgroud)

用法:

> func [1..100]
(1,100,5050,100)
Run Code Online (Sandbox Code Playgroud)

haskell functional-programming pointfree

5
推荐指数
2
解决办法
560
查看次数

Python设计:OOP

我试图沿着这些方向做点什么:

class A:

    def __init__( self, x=None, y=None ):
        self.x = x
        self.y = y

class B( A ):

     def do_something_in_Bs_context( self ):
         print "In B"

class C( A ):

    def do_something_in_Cs_context( self ):
        print "In C"


a = A(1,2)
b = B.do_something_in_Bs_context( a )
c = C.do_something_in_Cs_context( a )
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,此代码将生成此错误:

TypeError: unbound method do_something_in_Bs_context() must be called with B instance as first argument (got A instance instead)
Run Code Online (Sandbox Code Playgroud)

这种设计的根本原因是A是数据的容器(比如表),B和C是A上的一组操作.B和C都在相同的数据上运行,但在概念上是一组独特的操作,可以对相同的数据执行.我可以将A和B中的所有操作联合起来,但我想在概念中创建分离.(例如,在表格中,我可以执行不同的操作集合,可以分组为"微积分"或"三角函数".所以A:表,B:微积分,C:Trignometry)

这让我想起了模型 - 视图 - 控制器范例,略有不同.

我想出了以下解决方案:

  1. B和C实现为概念上不同的类(视图/控制器),它们维护对A实例的引用并对该实例进行操作.
  2. 由于B和C只是将A上的方法组合在一起,因此创建具有在A实例上运行的函数的模块.

我不喜欢这些解决方案中的任何一个(2稍微好于1),但后来我不确定是否有更好/更清洁/更pythonic的方法来解决这个问题.有什么指针吗?

python oop inheritance

4
推荐指数
1
解决办法
187
查看次数

动态创建的类中的Python继承

我正在尝试使用元类来实现以下功能:

class foo( object ):

    def __init__( self ):
        self.val = 'foo'

    def bar( self ):
        print 'hello world'
        print self.val

f = foo()
f.bar() #prints 'hello world' followed by foo

def newbar( self ):
    super( **?**, self).bar()
    print 'another world!'

fooNew = type('fooNew', (foo,), {'bar':newbar})
n = fooNew()
n.bar() # should print everything in f.bar() followed by 'another world!'
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用猴子修补来实现我自己的功能newbar.但是有一个细微的区别,我希望新的bar函数首先运行基类bar函数,然后才运行任何其他功能.

我怎样才能做到这一点?或者我怎么能做得更好?

python monkeypatching metaclass

2
推荐指数
1
解决办法
150
查看次数

将函数元组应用于元组

我正在尝试将一组函数应用于值元组

?> let foo = ((+1), (*3), ((:)5))  #Each function has type: a -> a

?> let bar  = (1, 5, [0])          #Each value of the corresponding tuple has type a
Run Code Online (Sandbox Code Playgroud)

我该如何实现:

toImplement foo bar = ?
Run Code Online (Sandbox Code Playgroud)

这样:

?> toImplement foo bar
-> (2, 15, [0,5]) # using foo and bar above
Run Code Online (Sandbox Code Playgroud)

对于任何有效的foo和bar(长度相同),你如何实现这一点?

[我看了这个问题,但它是针对固定类型实现的.我要求一般实施]

动机:

我正在努力有效地写折叠.

let acc1 = \x acc -> x*x:acc
let acc2 = (+)
foldr (\x acc -> (acc1 x (fst acc), acc2 x ( …
Run Code Online (Sandbox Code Playgroud)

haskell

2
推荐指数
1
解决办法
474
查看次数