currying

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

Two ways of defining functions in Scala. What is the difference?

强颜欢笑 提交于 2019-11-26 07:58:38
问题 Here is a little Scala session that defines and tries out some functions: scala> def test1(str: String) = str + str; test1: (str: String)java.lang.String scala> test1(\"ab\") res0: java.lang.String = abab works nicely. scala> val test2 = test1 <console>:6: error: missing arguments for method test1 in object $iw; follow this method with `_\' if you want to treat it as a partially applied function val test2 = test1 ^ oops. scala> val test2 = test1 _ test2: (String) => java.lang.String =

Two ways of currying in Scala; what&#39;s the use-case for each?

倖福魔咒の 提交于 2019-11-26 07:53:28
问题 I am having a discussion around Multiple Parameter Lists in the Scala Style Guide I maintain. I\'ve come to realize that there are two ways of currying, and I\'m wondering what the use cases are: def add(a:Int)(b:Int) = {a + b} // Works add(5)(6) // Doesn\'t compile val f = add(5) // Works val f = add(5)_ f(10) // yields 15 def add2(a:Int) = { b:Int => a + b } // Works add2(5)(6) // Also works val f = add2(5) f(10) // Yields 15 // Doesn\'t compile val f = add2(5)_ The style guide incorrectly

Is there a way to do currying in C?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 07:33:48
问题 Say I have a pointer to a function _stack_push(stack* stk, void* el) . I want to be able to call curry(_stack_push, my_stack) and get back a function that just takes void* el . I couldn\'t think of a way to do it, since C doesn\'t allow runtime function definition, but I know there are far cleverer people than me here :). Any ideas? 回答1: I found a paper by Laurent Dami that discusses currying in C/C++/Objective-C: More Functional Reusability in C/C++/Objective-c with Curried Functions Of

How can I make var a = add(2)(3); //5 work?

与世无争的帅哥 提交于 2019-11-26 07:29:20
问题 I want to make this syntax possible: var a = add(2)(3); //5 based on what I read at http://dmitry.baranovskiy.com/post/31797647 I\'ve got no clue how to make it possible. 回答1: You need add to be a function that takes an argument and returns a function that takes an argument that adds the argument to add and itself. var add = function(x) { return function(y) { return x + y; }; } 回答2: function add(x) { return function(y) { return x + y; }; } Ah, the beauty of JavaScript This syntax is pretty

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

Variadic curried sum function

China☆狼群 提交于 2019-11-26 03:14:42
问题 I need a js sum function to work like this: sum(1)(2) = 3 sum(1)(2)(3) = 6 sum(1)(2)(3)(4) = 10 etc. I heard it can\'t be done. But heard that if adding + in front of sum can be done. Like +sum(1)(2)(3)(4) . Any ideas of how to do this? 回答1: Not sure if I understood what you want, but function sum(n) { var v = function(x) { return sum(n + x); }; v.valueOf = v.toString = function() { return n; }; return v; } console.log(+sum(1)(2)(3)(4)); JsFiddle 回答2: This is an example of using empty

How do I define Lisp’s apply in Haskell?

拥有回忆 提交于 2019-11-26 01:53:52
问题 Shouldn’t this definition be allowed in a lazy language like Haskell in which functions are curried? apply f [] = f apply f (x:xs) = apply (f x) xs It’s basically a function that applies the given function to the given list of arguments and is very easily done in Lisp for example. Are there any workarounds? 回答1: It is hard to give a static type to the apply function, since its type depends on the type of the (possibly heterogeneous) list argument. There are at least two ways one way to write

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: