该uncurry函数仅适用于带有两个参数的函数:
uncurry :: (a -> b -> c) -> (a, b) -> c
Run Code Online (Sandbox Code Playgroud)
如果我想用任意数量的参数来解决函数,我可以编写单独的函数:
uncurry2 f (a, b) = f a b
uncurry3 f (a, b, c) = f a b c
uncurry4 f (a, b, c, d) = f a b c d
uncurry5 f (a, b, c, d, e) = f a b c d e
Run Code Online (Sandbox Code Playgroud)
但这很快就会变得乏味.有没有办法概括这个,所以我只需要编写一个函数?
在像Haskell这样的函数式编程中,我可以定义函数
add a b = a+b
Run Code Online (Sandbox Code Playgroud)
然后add 3将返回一个带有一个参数的函数并返回3 + something
我怎么能在GO中这样做?
当我定义一个多于一个(比如n)参数的函数时,我可以只给它一个参数并得到另一个带n-1个参数的函数吗?
更新:
对不起我原来问题中的不精确的话.
我认为我的问题应该被问为两个问题:
感谢TheOnly92和Alex解决我的第二个问题.但是,我对第一个问题也很好奇.
我想了解curryvs 之间的区别bind.
执行bind是:
/*1*/ Function.prototype.bind = function ()
/*2*/ {
/*3*/ var fn = this,
/*4*/ args = Array.prototype.slice.call(arguments);
/*5*/ var object = args.shift();
/*6*/ return function ()
/*7*/ {
/*8*/ return fn.apply(object,
/*9*/ args.concat(Array.prototype.slice.call(arguments)))
/*10*/ };
/*11*/ }
Run Code Online (Sandbox Code Playgroud)
执行curry是:
/*1*/ Function.prototype.curry = function ()
/*2*/ {
/*3*/ var fn = this,
/*4*/ args = Array.prototype.slice.call(arguments);
/*5*/ return function ()
/*6*/ {
/*7*/ return fn.apply(this,
/*8*/ args.concat(Array.prototype.slice.call(arguments)));
/*9*/ };
/*10*/ };
Run Code Online (Sandbox Code Playgroud)
我已经知道这 …
Haskell讨论它的功能.尽管Clojure允许partial并使用宏作为执行相同操作的可比方法.
我以为我记得听到/读过希基的决定背后的原因.有人回忆起那个原因是什么,或者我在哪里可以找到它?
我希望从理论上学到一些东西.
const f = (arg1) => (arg2) => { /* returns something */ }
Run Code Online (Sandbox Code Playgroud)
关于2个参数是否可以记忆f,即:
f(1)(2);
f(1)(3); // Cache not hit
f(4)(2); // Cache not hit
f(1)(2); // Cache hit
Run Code Online (Sandbox Code Playgroud) 让我们假设我有一个具有两个类型参数的特征,例如
trait Qux[A, B]
Run Code Online (Sandbox Code Playgroud)
和另一个具有更高通道类型参数的特征,例如
trait Turkle[C[_]]
Run Code Online (Sandbox Code Playgroud)
我希望能够将一个固定值替换为其中一个类型参数Qux,以便它可以用于参数化Turkle.
这是一个示例(在Scala中没有意义的代码!):
trait Baz[A] extends Turkle[Qux[A, _]]
Run Code Online (Sandbox Code Playgroud)
任何人有任何想法如何实现这种效果?
将地图视为有限函数的表示,可以以咖喱或非曲线的形式给出两个或更多变量的映射; 也就是说,类型Map (a,b) c和Map a (Map b c)同构,或接近它的东西.
有哪些实际考虑 - 效率等 - 用于在两种表示之间进行选择?
我有以下Scala类:
class Person(var name : String, var age : Int, var email : String)
Run Code Online (Sandbox Code Playgroud)
我想使用Person构造函数作为curried函数:
def mkPerson = (n : String) => (a : Int) => (e : String) => new Person(n,a,e)
Run Code Online (Sandbox Code Playgroud)
这有效,但还有另一种方法可以做到这一点吗?这种方法似乎有点乏味且容易出错.我可以想象像Function.curried这样的东西,但是对于构造函数来说.
是否有可能使用钻营的R?
一种可能性是具有特殊paste功能(可以将其视为此处的后续操作),例如(在错误的代码中):
'%+%' <- (sep)function(x,y) paste(x,y,sep=sep)
"a"%+%("")"b"%+%("_")"c" #gives "ab_c"
Run Code Online (Sandbox Code Playgroud)
什么是可能的实现R?
PS:这paste只是一个例子,我很好奇R......
是否可以foldLeft在参数列表上执行a ,其中提供给fold的初始值是完全curried函数,运算符是apply,并且列表是要传递给函数的参数列表f?
例如,假设f定义为:
scala> val f = (i: Int, j: Int, k: Int, l: Int) => i+j+k+l
f: (Int, Int, Int, Int) => Int = <function4>
Run Code Online (Sandbox Code Playgroud)
我们当然可以直接使用它:
scala> f(1, 2, 3, 4)
res1: Int = 10
Run Code Online (Sandbox Code Playgroud)
或者咖喱并一次应用一个参数:
scala> f.curried
res2: Int => Int => Int => Int => Int = <function1>
scala> f.curried.apply(1).apply(2).apply(3).apply(4)
res3: Int = 10
Run Code Online (Sandbox Code Playgroud)
乍一看,这看起来像是一份工作foldLeft.
我第一次尝试描述这个apply使用顺序foldLeft如下:
scala> List(1, 2, 3, 4).foldLeft(f.curried)({ (g, x) => g.apply(x) …Run Code Online (Sandbox Code Playgroud)