Python 3.2文档是指Collin Winter的functional模块,其中包含以下功能compose:
compose()函数实现了函数组合.换句话说,它返回外部和内部callables周围的包装器,这样内部的返回值直接送到外部.
不幸的是,这个模块自2006年7月以来一直没有更新; 我想知道是否有任何更换.
现在,我只需要compose功能.以下原始functional.compose定义是否仍适用于Python 3?
def compose(func_1, func_2, unpack=False):
"""
compose(func_1, func_2, unpack=False) -> function
The function returned by compose is a composition of func_1 and func_2.
That is, compose(func_1, func_2)(5) == func_1(func_2(5))
"""
if not callable(func_1):
raise TypeError("First argument to compose must be callable")
if not callable(func_2):
raise TypeError("Second argument to compose must be callable")
if unpack:
def composition(*args, **kwargs):
return func_1(*func_2(*args, **kwargs))
else:
def …Run Code Online (Sandbox Code Playgroud) python functional-programming function function-composition python-3.x
我正在尝试编写一个可变函数组合函数.基本上(.)除了第二个参数函数是可变参数之外.这应该允许表达式:
map even . zipWith (+)
Run Code Online (Sandbox Code Playgroud)
要不就
map even . zipWith
Run Code Online (Sandbox Code Playgroud)
目前,如果我添加IncoherentInstances并且需要第一个参数函数的非多态实例,我已达到的工作.
{-# LANGUAGE FlexibleInstances, OverlappingInstances, MultiParamTypeClasses,
FunctionalDependencies, UndecidableInstances, KindSignatures #-}
class Comp a b c d | c -> d where
comp :: (a -> b) -> c -> d
instance Comp a b (a :: *) (b :: *) where
comp f g = f g
instance Comp c d b e => Comp c d (a -> b) (a -> e) where
comp …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以使用带有多个参数的函数进行功能组合.我希望能够做这样的事情
x = (+3).(*)
Run Code Online (Sandbox Code Playgroud)
设置x等于将两个数字的乘积加三的函数.
我觉得我忽略了一些完全明显的东西,但是使用无点符号来组成二元函数和一元函数的正确方法(如果有的话)是什么?例如,以下代码编译:
sortedAppend :: (Ord a) -> [a] -> [a] -> [a]
sortedAppend xs ys = sort $ xs ++ ys
Run Code Online (Sandbox Code Playgroud)
但以下代码无法编译:
sortedAppend :: (Ord a) -> [a] -> [a] -> [a]
sortedAppend = sort . (++)
Run Code Online (Sandbox Code Playgroud)
难道我们能够组成(++)有sort(按顺序如上图所示)?如果是这样,怎么样?
有没有办法链接功能withCString?我的意思是任何看起来像的功能f :: Foo -> (CFoo -> IO a) -> IO a.
例如,假设有一个功能 cFunc :: CString -> CFoo -> CBar -> IO ()
Usualy,我会做类似的事情:
haskellFunc string foo bar =
withCString string $ \ cString ->
withCFoo foo $ \ cFoo ->
withCBar bar $ \ cBar ->
cFunc cString cFoo cBar
Run Code Online (Sandbox Code Playgroud)
但我想做的事情如下:
haskellFunc = (withCString |.| withCFoo |.| withCBar) cFunc
Run Code Online (Sandbox Code Playgroud)
与一些合适的组合操作员|.|.
我正在编写带有大量C绑定的库,这个样板经常出现.难道我做错了什么?
continuations haskell function-composition continuation-passing
我想在R中创建一个函数"compose",它将构成作为参数给出的任意数量的函数.
到目前为止,我已经通过定义一个函数"of"来完成这个,该函数组成两个参数,然后减少这个:
of <- function(f,g) function(x) f(g(x))
id <- function(x) x
compose <- function(...) {
argms = c(...)
Reduce(of,argms,id)
}
Run Code Online (Sandbox Code Playgroud)
这似乎工作得很好,但是因为我正在学习R,我以为我会尝试用一种明确的递归方式来编写它,即放弃使用Reduce,这就像你在Scheme中做的那样:
(define (compose . args)
(if (null? args) identity
((car args) (apply compose (cdr args)))))
Run Code Online (Sandbox Code Playgroud)
我遇到了许多障碍,目前主要的障碍似乎是论证的第一个要素没有被认为是一种功能.到目前为止我的弱尝试:
comp <- function(...) {
argms <- list(...)
len <- length(argms)
if(len==0) { return(id) }
else {
(argms[1])(do.call(comp,argms[2:len]))
}
}
Run Code Online (Sandbox Code Playgroud)
吐出: Error in comp(sin, cos, tan) : attempt to apply non-function
必须有某种方法可以做到这一点,这使我望而却步.有什么建议?
这是我在某处遇到的代码,但想知道它是如何工作的:
findIndices :: (a -> Bool) -> [a] -> [Int]
findIndices _ [] = []
findIndices pred xs = map fst (filter (pred . snd) (zip [0..] xs))
Run Code Online (Sandbox Code Playgroud)
输出:findIndices (== 0) [1,2,0,3,0]==[2,4],其中pred是(==0)&xs是[1,2,0,3,0]
我将展示我的一些理解:
(zip [0..] xs)
Run Code Online (Sandbox Code Playgroud)
上面一行的作用是为列表中的所有内容添加索引。对于上面给出的输入,它是这样的:[(0,1),(1,2),(2,0),(3,3),(4,0)]。
(pred . snd)
Run Code Online (Sandbox Code Playgroud)
我发现这意味着类似于pred (snd (x)). 我的问题是,x列表是由该zip行制作的吗?我倾向于是,但我的猜测是站不住脚的。
接下来是我对fstand 的理解snd。我知道
fst(1,2) = 1
Run Code Online (Sandbox Code Playgroud)
和
snd(1,2) = 2
Run Code Online (Sandbox Code Playgroud)
这两个命令如何在代码中有意义?
我的理解filter是它返回匹配条件的项目列表。例如, …
haskell composition pointfree function-composition higher-order-functions
(不重要的背景信息/动机)
我正在实施一个不同的版本nub,受到Yesod书的劝阻使用它的启发.
map head . group . sort比打电话更有效率nub.但是,在我们的案例中,订单很重要......
所以我开始写一个类似于订单不重要版本的"更好"的小块.我最终得到了这个:
mynub = unsort . map head . groupBy (\x y -> fst x == fst y) . sortBy (comparing fst) . rememberPosition
rememberPosition = flip zip [0..]
unsort = map fst . sortBy (comparing snd)
Run Code Online (Sandbox Code Playgroud)
这肯定会做很多额外的工作,但它应该是O(n log n)而不是原始nub的O(n 2).但这不是重点.问题是,它太长了!它真的不是那么复杂,但它很长(而且我是那些讨厌超过80列的人,或StackOverflow代码块上的水平滚动条).
(问题)
在Haskell中表达长链函数组合的更好方法是什么?
我在Python工作.最近,我发现了一个名为fn的精彩小包.我一直在用它来进行功能组合.
例如,而不是:
baz(bar(foo(x))))
Run Code Online (Sandbox Code Playgroud)
用fn,你可以写:
(F() >> foo >> bar >> baz)(x) .
Run Code Online (Sandbox Code Playgroud)
当我看到这个时,我立刻想到了Clojure:
(-> x foo bar baz) .
Run Code Online (Sandbox Code Playgroud)
但请注意,在Clojure中,输入位于左侧.我想知道这在python/fn中是否可行.
我一直在阅读一个wreq教程:
镜头提供了一种聚焦于Haskell值的一部分的方法.例如,该
Response类型具有responseStatus镜头,其关注于服务器返回的状态信息.Run Code Online (Sandbox Code Playgroud)ghci> r ^. responseStatus Status {statusCode = 200, statusMessage = "OK"}在
^.操作者需要一个值作为第一个参数,一个透镜作为其第二个,并返回聚焦值的该部分上由透镜.我们使用功能组合构成镜头,这使我们可以轻松地专注于深层嵌套结构的一部分.
Run Code Online (Sandbox Code Playgroud)ghci> r ^. responseStatus . statusCode 200
我无法想出如何使用这个参数顺序完成的函数组合可以按顺序处理嵌套结构.
看:r ^. responseStatus . statusCode可能是r ^. (responseStatus . statusCode)或者(r ^. responseStatus) . statusCode.
在第一个中我们构造了一个函数,它首先处理statusCode(从记录中获取它Status? - 因为我可以从显示的值中推导出来Status {statusCode = 200, statusMessage = "OK"}),然后将它传递给responseStatus必须处理响应状态的函数.所以,反过来说:实际上,状态代码是响应状态的一部分.
第二读对我来说也没有意义,因为它也首先处理状态代码.
haskell records function-composition higher-order-functions lenses
haskell ×7
pointfree ×3
python ×2
clojure ×1
coding-style ×1
composition ×1
function ×1
lenses ×1
python-3.x ×1
r ×1
records ×1
variadic ×1