currying

How do you curry any javascript function of arbitrary arity?

自作多情 提交于 2019-12-10 03:05:25
问题 Let's say I have some function: function g(a,b,c){ return a + b + c } And I'd like to turn it into its "curried" form (in quotations since it's not exactly curried per se): function h(a,b,c){ switch(true){ case (a !== undefined && b !== undefined && c !== undefined): return a + b + c case (a !== undefined && b !== undefined && c === undefined): return function(c){ return a + b + c } case (a !== undefined && b == undefined && c === undefined ): return function(b,c){ return (c === undefined) ?

Partial Application with Infix Functions

给你一囗甜甜゛ 提交于 2019-12-10 01:34:57
问题 While I understand a little about currying in the mathematical sense, partially applying an infix function was a new concept which I discovered after diving into the book Learn You a Haskell for Great Good. Given this function: applyTwice :: (a -> a) -> a -> a applyTwice f x = f (f x) The author uses it in a interesting way: ghci> applyTwice (++ [0]) [1] [1,0,0] ghci> applyTwice ([0] ++) [1] [0,0,1] Here I can see clearly that the resulting function had different parameters passed, which

Scala基础

别来无恙 提交于 2019-12-09 23:20:57
1. 介绍 柯里化(currying, 以逻辑学家Haskell Brooks Curry的名字命名)指的是将原来接受两个参数的函数变成新的接受一个参数的函数的过程。新的函数返回一个以原有第二个参数作为参数的函数。 在Scala中方法和函数有细微的差别,通常编译器会自动完成方法到函数的转换。如果想了解Scala方法和函数的具体区别,请参考博文 Scala基础 - 函数和方法的区别 。 2. Scala中柯里化的形式 Scala中柯里化方法的定义形式和普通方法类似,区别在于柯里化方法拥有多组参数列表,每组参数用圆括号括起来,例如: def multiply(x: Int)(y: Int): Int = x * y multiply方法拥有两组参数,分别是(x: Int)和(y: Int)。 multiply方法对应的柯里化函数类型是: Int => Int => Int 柯里化函数的类型声明是右结合的,即上面的类型等价于: Int => (Int => Int) 表明该函数若只接受一个Int参数,则返回一个Int => Int类型的函数,这也和柯里化的过程相吻合。 3. 探究柯里化函数 我们仍以上面定义的multiply方法为例探索柯里化的一些细节: def multiply(x: Int)(y: Int): Int = x * y 上面的代码定义了一个柯里化方法

Scala基础

一个人想着一个人 提交于 2019-12-09 20:45:22
1. 函数和方法 在Scala中函数是一等公民,你可以像操作数字一样将函数赋值给一个变量。使用val语句可以定义函数,def语句定义方法: class Test{ def m(x: Int) = x + 3 val f = (x: Int) => x + 3 } 在Scala中无法直接操作方法,如果要操作方法,必须先将其转换成函数。有两种方法可以将方法转换成函数: val f1 = m _ 在方法名称m后面紧跟一个空格和下划线告诉编译器将方法m转换成函数,而不是要调用这个方法。 也可以显示地告诉编译器需要将方法转换成函数: val f1: (Int) => Int = m 通常情况下编译器会自动将方法转换成函数,例如在一个应该传入函数参数的地方传入了一个方法,编译器会自动将传入的方法转换成函数。 2. 两者的区别 可以直接调用函数上的方法,而方法却不行,例如: f.toString //编译通过 m.toString //编译失败 3. Currying函数和Currying方法 Currying函数可以只传入部分参数返回一个偏函数(partially applied function, 也叫部分应用函数),而Currying方法在转换成偏函数时需要加上显式说明,让编译器完成转换: object TestCurrying { def invoke(f: Int => Int =>

What is the point of multiple parameter clauses in function definitions in Scala?

爱⌒轻易说出口 提交于 2019-12-09 15:50:35
问题 I'm trying to understand the point of this language feature of multiple parameter clauses and why you would use it. Eg, what's the difference between these two functions really? class WTF { def TwoParamClauses(x : Int)(y: Int) = x + y def OneParamClause(x: Int, y : Int) = x + y } >> val underTest = new WTF >> underTest.TwoParamClauses(1)(1) // result is '2' >> underTest.OneParamClause(1,1) // result is '2' There's something on this in the Scala specification at point 4.6. See if that makes

Currying for templates in C++ metaprogramming

可紊 提交于 2019-12-09 15:06:53
问题 This is more of a conceptual question. I'm trying to find the easiest way of converting a two-arg template (the arguments being types) into a one-arg template. I.e., binding one of the types. This would be the meta-programming equivalent of bind in boost/std. My example includes a possible use-case, which is, passing std::is_same as template argument to a template that takes a one-arg template template argument ( std::is_same being a two-arg template), i.e. to TypeList::FindIf . The TypeList

Haskell - Currying? Need further explanation

僤鯓⒐⒋嵵緔 提交于 2019-12-09 13:13:18
问题 So something like addList :: [int] -> int addList = foldl1 (+) Why does this work? The Currying part. Why no variable? 回答1: If you define a function like f x y = bla , this is the same as f x = \y -> bla , which is the same as f = \x -> (\y -> bla) . In other words f is a function which takes one argument, x , and returns another function which takes one argument, y , and then returns the actual result. This is known as currying. Analogously when you do f x y , it's the same as (f x) y . I.e.

Need help understanding lambda (currying)

一曲冷凌霜 提交于 2019-12-09 11:19:18
问题 i am reading Accelerated C# i don't really understand the following code: public static Func<TArg1, TResult> Bind2nd<TArg1, TArg2, TResult> ( this Func<TArg1, TArg2, TResult> func, TArg2 constant ) { return (x) => func( x, constant ); } in the last line what is x referring to? and there's another: public static Func<TArg2, Func<TArg1, TResult>> Bind2nd<TArg1, TArg2, TResult> ( this Func<TArg1, TArg2, TResult> func ) { return (y) => (x) => func( x, y ); } How do i evaluate this? (y) => (x) =>

Swift subtle difference between curried and higher order function

爱⌒轻易说出口 提交于 2019-12-09 07:18:27
问题 NOTE: This question was asked while Swift 2.1 was the latest. Given: class IntWrapper { var i: Int = 1 } func add(inout m: Int, i: Int) { m += i } And a higher order function func apply() -> (inout i: Int) -> () -> () { return { (inout i: Int) -> () -> () in return { add(&i, i: 1) } } } Application as so results in the member variable value never changing: var intW = IntWrapper() print(intW.i) // Prints '1' apply()(i: &intW.i)() print(intW.i) // Prints '1' However, when changing the function

When are F# function calls evaluated; lazily or immediately?

佐手、 提交于 2019-12-08 14:44:00
问题 Curried functions in F#. I get the bit where passing in a subset of parameters yields a function with presets. I just wondered if passing all of the parameters is any different. For example: let addTwo x y = x + y let incr a = addTwo 1 let added = addTwo 2 2 incr is a function taking one argument. Is added an int or a function? I can imagine an implementation where "added" is evaluated lazily only on use (like Schroedinger's Cat on opening the box). Is there any guarantee of when the addition