currying

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

How should I make function curry?

人走茶凉 提交于 2019-11-27 00:22:47
问题 In C++14, what is a good way to curry functions or function objects? In particular, I have an overloaded function foo with some random number of overloads: some overloads may be found via ADL, others may be defined in a myriad of places. I have a helper object: static struct { template<class...Args> auto operator()(Args&&...args)const -> decltype(foo(std::forward<Args>(args)...)) { return (foo(std::forward<Args>(args)...));} } call_foo; that lets me pass the overload set around as a single

What is 'Currying'?

五迷三道 提交于 2019-11-26 23:18:34
问题 I've seen references to curried functions in several articles and blogs but I can't find a good explanation (or at least one that makes sense!) 回答1: Currying is when you break down a function that takes multiple arguments into a series of functions that each take only one argument. Here's an example in JavaScript: function add (a, b) { return a + b; } add(3, 4); // returns 7 This is a function that takes two arguments, a and b, and returns their sum. We will now curry this function: function

Is it possible to curry method calls in PHP?

陌路散爱 提交于 2019-11-26 23:08:05
问题 I have a SoapClient instance generated for a WSDL file. All except one of the method invocations require the username and the password to be passed id. Is there any way of currying the method calls so that I can omit the username and password? 回答1: As of php 5.3 you can store an anonymous function in a variable. This anonymous function can call the "original" function with some predefined parameters. function foo($x, $y, $z) { echo "$x - $y - $z"; } $bar = function($z) { foo('A', 'B', $z); };

How to curry a function across an unknown number of parameters

最后都变了- 提交于 2019-11-26 22:03:41
问题 Say I have a function called multiplyDivide If I were to call multiplyDivide(2)(3)(4)(6) it would be equivalent to 2 * 3 / 4 * 6 . Update: Is it possible to write a function like this if I don't know in advance how many parameters I will be taking? For example, I could have multiplyDivide(1)(2) or multiplyDivide(1)(2)(3)(4)...(n-1)(n) 回答1: Why does it have to be curried? Wouldn't this be much easier to just "overload" a function by supplying n arguments? function multiplyDivide() { var result

Is there a way to do currying in C?

南楼画角 提交于 2019-11-26 20:16:54
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? Jared Oberhaus 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 interest to how it is implemented in C: Our current implementation uses existing C constructs to

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

浪尽此生 提交于 2019-11-26 20:06:22
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. 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; }; } function add(x) { return function(y) { return x + y; }; } Ah, the beauty of JavaScript This syntax is pretty neat as well function add(x) { return function(y) { if (typeof y !== 'undefined') { x = x + y; return arguments

Scala currying vs partially applied functions

妖精的绣舞 提交于 2019-11-26 19:24:31
I realize that there are several questions on here about what currying and partially applied functions are, but I'm asking about how they are different. As a simple example, here is a curried function for finding even numbers: def filter(xs: List[Int], p: Int => Boolean): List[Int] = if (xs.isEmpty) xs else if (p(xs.head)) xs.head :: filter(xs.tail, p) else filter(xs.tail, p) def modN(n: Int)(x: Int) = ((x % n) == 0) So you could write the following to use this: val nums = List(1,2,3,4,5,6,7,8) println(filter(nums, modN(2)) which returns: List(2,4,6,8) . But I've found that I can do the same

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