partial-application

Ordering of parameters to make use of currying

我的梦境 提交于 2019-11-26 10:08:21
问题 I have twice recently refactored code in order to change the order of parameters because there was too much code where hacks like flip or \\x -> foo bar x 42 were happening. When designing a function signature what principles will help me to make the best use of currying? 回答1: For languages that support currying and partial-application easily, there is one compelling series of arguments, originally from Chris Okasaki: Put the data structure as the last argument Why? You can then compose

How to correctly curry a function in JavaScript?

半腔热情 提交于 2019-11-26 09:38:00
问题 I wrote a simple curry function in JavaScript which works correctly for most cases: const add = curry((a, b, c) => a + b + c); const add2 = add(2); const add5 = add2(3); console.log(add5(5)); <script> const curried = Symbol(\"curried\"); Object.defineProperty(curry, curried, { value: true }); function curry(functor, ...initArgs) { if (arguments.length === 0) return curry; if (typeof functor !== \"function\") { const value = JSON.stringify(functor); throw new TypeError(`${value} is not a

What&#39;s the difference between multiple parameters lists and multiple parameters per list in Scala?

痞子三分冷 提交于 2019-11-26 08:53:14
问题 In Scala one can write (curried?) functions like this def curriedFunc(arg1: Int) (arg2: String) = { ... } What is the difference between the above curriedFunc function definition with two parameters lists and functions with multiple parameters in a single parameter list: def curriedFunc(arg1: Int, arg2: String) = { ... } From a mathematical point of view this is (curriedFunc(x))(y) and curriedFunc(x,y) but I can write def sum(x) (y) = x + y and the same will be def sum2(x, y) = x + y I know

Usefulness (as in practical applications) of Currying v.s. Partial Application in Scala

醉酒当歌 提交于 2019-11-26 07:59:15
问题 I\'m trying to understand the advantages of currying over partial applications in Scala. Please consider the following code: def sum(f: Int => Int) = (a: Int, b: Int) => f(a) + f(b) def sum2(f: Int => Int, a: Int, b: Int): Int = f(a) + f(b) def sum3(f: Int => Int)(a: Int, b: Int): Int = f(a) + f(b) val ho = sum({identity}) val partial = sum2({ identity }, _, _) val currying = sum3({ identity }) val a = currying(2, 2) val b = partial(2, 2) val c = ho(2, 2) So, if I can calculate partially

Replace parameter in lambda expression

瘦欲@ 提交于 2019-11-26 06:48:22
问题 Considering this code: public class Foo { public int a { get; set; } public int b { get; set; } } private void Test() { List<Foo> foos = new List<Foo>(); foos.Add(new Foo()); foos.Add(new Foo()); Expression<Func<Foo, int>> exp0 = f => f.a * f.b; Expression<Func<int>> exp1 = () => foos[0].a * foos[0].b; Expression<Func<int>> exp2 = () => foos[1].a * foos[1].b; } How can you take exp0 and turn it into two expressions identical to exp1 and exp2 . Note that I don\'t want to just evaluate exp0 for

JavaScript curry: what are the practical applications?

≡放荡痞女 提交于 2019-11-26 06:09:39
问题 I don’t think I’ve grokked currying yet. I understand what it does, and how to do it. I just can’t think of a situation I would use it. Where are you using currying in JavaScript (or where are the main libraries using it)? DOM manipulation or general application development examples welcome. One of the answers mentions animation. Functions like slideUp , fadeIn take an element as an arguments and are normally a curried function returning the high order function with the default “animation

What&#39;s the difference between multiple parameters lists and multiple parameters per list in Scala?

邮差的信 提交于 2019-11-26 05:15:39
问题 In Scala one can write (curried?) functions like this def curriedFunc(arg1: Int) (arg2: String) = { ... } What is the difference between the above curriedFunc function definition with two parameters lists and functions with multiple parameters in a single parameter list: def curriedFunc(arg1: Int, arg2: String) = { ... } From a mathematical point of view this is (curriedFunc(x))(y) and curriedFunc(x,y) but I can write def sum(x) (y) = x + y and the same will be def sum2(x, y) = x + y I know

How does functools partial do what it does?

非 Y 不嫁゛ 提交于 2019-11-26 04:02:08
问题 I am not able to get my head on how the partial works in functools. I have the following code from here: >>> sum = lambda x, y : x + y >>> sum(1, 2) 3 >>> incr = lambda y : sum(1, y) >>> incr(2) 3 >>> def sum2(x, y): return x + y >>> incr2 = functools.partial(sum2, 1) >>> incr2(4) 5 Now in the line incr = lambda y : sum(1, y) I get that whatever argument I pass to incr it will be passed as y to lambda which will return sum(1, y) i.e 1 + y . I understand that. But I didn\'t understand this

Python Argument Binders

可紊 提交于 2019-11-26 03:40:56
问题 How can I bind arguments to a Python method to store a nullary functor for later invocation? Similar to C++\'s boost::bind . For example: def add(x, y): return x + y add_5 = magic_function(add, 5) assert add_5(3) == 8 回答1: functools.partial returns a callable wrapping a function with some or all of the arguments frozen. import sys import functools print_hello = functools.partial(sys.stdout.write, "Hello world\n") print_hello() Hello world The above usage is equivalent to the following lambda

What is the difference between currying and partial application?

北城以北 提交于 2019-11-26 00:34:49
问题 I quite often see on the Internet various complaints that other peoples examples of currying are not currying, but are actually just partial application. I\'ve not found a decent explanation of what partial application is, or how it differs from currying. There seems to be a general confusion, with equivalent examples being described as currying in some places, and partial application in others. Could someone provide me with a definition of both terms, and details of how they differ? 回答1: