标签: function-composition

如何知道符号是代表函数还是宏?

我正在为函数/宏组合编写一个宏(混合组合是可能的).在宏的内部,我必须处理代表函数的符号和不同地命名宏的符号.这是因为结果函数必须使用任意数量的参数(如果组合中的'最低'函数可以),并且我不能应用于apply宏.我的问题:如何确定给定符号代表什么:函数还是宏?

lisp macros symbols common-lisp function-composition

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

如何在Haskell中使用(.)

我想在Haskell中写这样的东西:

length . nub . intersect
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

*Main Data.List> :t intersect
intersect :: Eq a => [a] -> [a] -> [a]
*Main Data.List> :t nub
nub :: Eq a => [a] -> [a]
*Main Data.List> :t length
length :: [a] -> Int
Run Code Online (Sandbox Code Playgroud)

根据上式,我的理解是,intersect返回类型[a],并捐赠给nub,这恰恰是一个类型的[a],然后也返回一个类型[a]length,最后返回应该是一个Int.它出什么问题了?

haskell combinators pointfree function-composition dot-operator

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

与功能组成混淆

开始学习Haskell:

*Main> map double [1,2,3]
[2,4,6]

*Main> sum (map double [1,2,3])
12

*Main> (sum . map) (double) ([1,2,3])

<interactive>:71:8:
Couldn't match type ‘[b0] -> [b0]’ with ‘[[t0] -> t]’
Expected type: (b0 -> b0) -> [[t0] -> t]
  Actual type: (b0 -> b0) -> [b0] -> [b0]
Relevant bindings include it :: t (bound at <interactive>:71:1)
Probable cause: ‘map’ is applied to too few arguments
In the second argument of ‘(.)’, namely ‘map’
In the expression: sum . map
Run Code Online (Sandbox Code Playgroud)

根据这个答案: …

haskell function function-composition

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

块内可以知道它收到的选择器并根据它自行评估吗?

假设我们在代码中有一个块,我们将它分配给一个变量(实例或本地),就像这样.

someName := [ anInstanceVariable doThis. anotherInstanceVariable doThat.] 
Run Code Online (Sandbox Code Playgroud)

从外面我想用这种方式:

someName someMessageTheBlockDoesntImplement: argument.
Run Code Online (Sandbox Code Playgroud)

块是否可以作用于特定选择器someName并拥有anInstanceVariableanotherInstanceVariable执行它并分别返回这些对象?

PS.它会充当各种各样的货运代理.

arguments smalltalk block function-composition

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

Haskell和函数组成

我在Haskell学习了一些基本的功能组合,当我在玩的时候,我意识到我无法解释的东西.当我使用下面的代码块时,编译器似乎对此感到高兴并且工作正常:

doSomeX x = if x==7 then True else False
doSomeY (x,y) = x+y+1
doSomeXY = doSomeX.doSomeY
Run Code Online (Sandbox Code Playgroud)

但是当我将doSomeY拆分为2个args而不是一对时,即:

doSomeX x = if x==7 then True else False
doSomeY x y = x+y+1
doSomeXY = doSomeX.doSomeY
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

 No instance for (Num a0) arising from a use of `doSomeY'
 The type variable `a0' is ambiguous
 Relevant bindings include
   doSomeXY :: a0 -> Bool (bound at test.hs:21:1)
 Note: there are several potential instances:
   instance Integral a => Num (GHC.Real.Ratio a)
     -- Defined in …
Run Code Online (Sandbox Code Playgroud)

haskell function-composition

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

如何创建执行多个合成的函数?

我想组合两个或更多函数来生成一个新函数.

如何通过返回一个接受一个参数的函数来创建一个执行从左到右函数组合的函数?

例如:

const square = v => v * v;
const double = v => v * 2;
const addOne = v => v + 1;

const cal = myFunction(square, double, addOne);
cal(2) // 9; addOne(double(square(2)))
Run Code Online (Sandbox Code Playgroud)

javascript function-composition

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

如何使用运算符在 Python 中组合函数?

编写一个由其他两个函数组成的函数是相当简单的。(为简单起见,假设它们各有一个参数。)

def compose(f, g):
    fg = lambda x: f(g(x))
    return fg

def add1(x):
    return x + 1

def add2(x):
    return x + 2

print(compose(add1, add2)(5))  # => 8
Run Code Online (Sandbox Code Playgroud)

我想使用运算符进行组合,例如(add1 . add2)(5).

有没有办法做到这一点?

我尝试了各种装饰器配方,但我无法让它们中的任何一个起作用。

def composable(f):
  """
    Nothing I tried worked. I won't clutter up the question 
    with my failed attempts.
  """

@composable
def add1(x):
    return x + 1
Run Code Online (Sandbox Code Playgroud)

谢谢。

function-composition python-3.x python-decorators

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

为什么带有“ join”的函数组成可以改变函数输入?

我最近开始学习Haskell,并且尝试进行以下函数组合(join . mapM),但是该函数产生了一些我不理解的怪异类型。我以为GHC会假设t == mmapM类型和输出mapM中将变为m (m b)可以连接的状态,否则将不会,并且由于类型不匹配而导致错误。而是发生了以下情况:

mapM :: (Traversable t, Monad m) => (a -> m b) -> t a -> m (t b)
join :: Monad m => m (m a) -> m a
join . mapM :: Traversable t => (a -> t a -> b) -> t a -> t b
Run Code Online (Sandbox Code Playgroud)

我不知道这怎么可能。以我的理解,函数组合应该使用第一个函数的输入(或第二个函数取决于您的外观)和第二个函数的输出。但是在这里,mapM从一元函数到二进制函数的期望输入函数,我不知道为什么。即使GHC不能仅仅t == m像我一样做出这样的假设(这是我的一半期望),它也应该由于类型不匹配而出错,而不更改输入函数的类型,对吗?这是怎么回事

monads haskell function-composition

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

如何使用关键字参数为复合函数别名?

我可以像这样为单个函数设置别名:

julia> f(y;x=1) = x * y
f (generic function with 2 methods)
julia> const g = f
f (generic function with 1 method)
julia> g(3,x=2)
6
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试使用复合函数来做到这一点,kwarg 会引起麻烦:

julia> const gg= sqrt ? f
#62 (generic function with 1 method)
julia> gg(3,x=2)
ERROR: MethodError: no method matching (::Base.var"#62#63"{typeof(sqrt),typeof(f)})(::Int64; x=2)
Closest candidates are:
  #62(::Any...) at operators.jl:875 got unsupported keyword argument "x"
Run Code Online (Sandbox Code Playgroud)

这个问题有方法解决吗?我正在尝试将参数传递给f,然后通过快捷方式(gg在上面的 MWE 中)转换该结果。

alias function-composition julia

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

在 Haskell 中组合具有中间多态类型的函数

我有以下文件:

module SimpleComposition where

class Intermediate a where
    f :: a -> Int
    g :: Char -> a

h :: Char -> Int
h = f . g
Run Code Online (Sandbox Code Playgroud)

尝试在 ghci 中加载它时,出现错误:

main.hs:8:5: error:
    * No instance for (Intermediate a0) arising from a use of `f'
    * In the first argument of `(.)', namely `f'
      In the expression: f . g
      In an equation for `h': h = f . g
  |
8 | h = f . g
  |     ^ …
Run Code Online (Sandbox Code Playgroud)

polymorphism haskell function-composition

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