partial-application

Can one partially apply the second argument of a function that takes no keyword arguments?

。_饼干妹妹 提交于 2019-11-27 03:44:10
Take for example the python built in pow() function. xs = [1,2,3,4,5,6,7,8] from functools import partial list(map(partial(pow,2),xs)) >>> [2, 4, 8, 16, 32, 128, 256] but how would I raise the xs to the power of 2? to get [1, 4, 9, 16, 25, 49, 64] list(map(partial(pow,y=2),xs)) TypeError: pow() takes no keyword arguments I know list comprehensions would be easier. No According to the documentation , partial cannot do this (emphasis my own): partial.args The leftmost positional arguments that will be prepended to the positional arguments You could always just "fix" pow to have keyword args:

What are the rules to govern underscore to define anonymous function?

霸气de小男生 提交于 2019-11-27 03:27:38
问题 I am using _ as placeholder for creating anonymous function, and the problem is I cannot predict how Scala is going to transform my code. More precisely, it mistakenly determines how "large" the anonymous function I want. List(1,2,3) foreach println(_:Int) //error ! List(1,2,3) foreach (println(_:Int)) //work List(1,2,3) foreach(println(_:Int)) //work Using -Xprint:typer I can see Scala transforms the first one into "a big anonymous function": x$1 => List(1,2,3) foreach(println(x$1:Int)) the

Ordering of parameters to make use of currying

那年仲夏 提交于 2019-11-27 02:32:13
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? 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 operations on the data nicely. E.g. insert 1 $ insert 2 $ insert 3 $ s . This also helps for functions on state .

How to correctly curry a function in JavaScript?

不羁的心 提交于 2019-11-27 01:21:48
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 function`); } if (functor[curried] || initArgs.length >= functor.length) return functor(...initArgs); const

Replace parameter in lambda expression

限于喜欢 提交于 2019-11-26 19:08:10
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 each Foo in foos , but instead get two new expressions. [Update] : Basically, I want to be able to

JavaScript curry: what are the practical applications?

不羁岁月 提交于 2019-11-26 18:02:58
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 function” built-in. Why is that better than just applying the higher-up function with some defaults? Are

What's the difference between multiple parameters lists and multiple parameters per list in Scala?

纵饮孤独 提交于 2019-11-26 17:05:09
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 only one difference - this is partially applied functions. But both ways are equivalent for me. Are

How does functools partial do what it does?

人走茶凉 提交于 2019-11-26 16:51:32
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 incr2(4) . How does the 4 gets passed as x in partial function? To me, 4 should replace the sum2 . What is

Python Argument Binders

回眸只為那壹抹淺笑 提交于 2019-11-26 13:22:32
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 Jeremy Banks 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 . print_hello = lambda *a, **kw: sys.stdout.write("Hello world\n", *a, **kw) I'm not overly

Why does Scala provide both multiple parameters lists and multiple parameters per list? [duplicate]

半城伤御伤魂 提交于 2019-11-26 11:52:14
问题 This question already has answers here : What's the difference between multiple parameters lists and multiple parameters per list in Scala? (4 answers) Closed 6 years ago . Multiple parameters lists, e.g. def foo(a:Int)(b:Int) = {} and multiple parameters per list, e.g. def foo(a:Int, b:Int) = {} are semantically equivalent so far as I can tell, and most functional languages have only one way of declaring multiple parameters, e.g. F#. The only reason I can figure out for supporting both these