Show HN: Omg-Curry – JavaScript currying library
github.com
github.com
My final impression was that currying works better a language like Haskell that has a strong type system. In a dynamic language it is more problematic.
const add = (a) => (b) => a + b;
[1, 2, 3, 4].map(add(2)); // [3, 4, 5, 6]Something I would really like, although this would require some language-level changes, I think, is the ability to curry arguments in any order, maybe using placeholders. So you could say add(_, 2) and get back a function of one argument, such that if you call it with 1, for example, you invoke add(1, 2).
add(1)(2)
Which is admittedly a little bit clunky, though from a quick read I don't think the library helps with this.You might be interested in the "pipeline operator" proposal. In addition to pairing nicely with curried functions, it offers a certain amount of re-ordering.
add4(1)(3)(5)(10)
add4(1)(3,5)(10)
add4(1,3,5,10)
// 19The example doesn't feel that exciting...
add4(1)(3)(5)(10)
add4(1)(3,5)(10)
add4(1,3,5,10)
// 19https://lodash.com/docs/4.17.15#partial https://lodash.com/docs/4.17.15#curry
function greet(greeting, name) {
return greeting + ' ' + name;
}
var sayHelloTo = _.partial(greet, 'hello');
sayHelloTo('fred'); // => 'hello fred'
var sayHi = _.partial(greet, 'hi');
greetFred('fred'); // => 'hi fred'
Currying works similarly, except that the function by does the partial evaluation automatically if you call it with less arguments. var greet = _.curry(function(greeting, name) {
return greeting + ' ' + name
})
var sayHelloTo = greet('hello');
sayHelloTo('fred'); // => 'hello fred'
var sayHi = greet('hi');
greetFred('fred'); // => 'hi fred' /**
* These functions are intended to be used with array iteration methods
* -------------------
* // Example data:
* let e1 = { a: 1, b: 2, c: "C" };
* let e2 = { a: 2, b: 12, c: "D" };
* let myArray = [ e1, e2 ];
*
* // Basic usage, matching a constant
* myArray.filter(match({a: 1})); // [ e1 ]
* myArray.filter(match({a: 3})); // [ ]
*
* // Matching multiple properties
* myArray.filter(match({ a: 1, c: "C" })); // [ e1 ]
* myArray.filter(match({ a: 1, c: "D" })); // [ ]
* myArray.filter(matchAny({ a: 1, c: "D" })); // [ e1, e2 ]
*
* // If a function is provided, return the result of the function
* // called with the corresponding property as its argument.
* let belowTen = n => n < 10;
* myArray.filter(match({ a: belowTen, b: belowTen })); // [ e1 ]
* myArray.filter(matchAny({ a: belowTen, b: belowTen })); // [ e1, e2 ]
*
*/
export const match = o1 => o2 => Object.entries(o1).every(
([k, v]) => typeof v === "function" ? v(o2[k]) : o2[k] === v
);
export const matchAny = o1 => o2 => Object.entries(o1).some(
([k, v]) => typeof v === "function" ? v(o2[k]) : o2[k] === v
);> A Curry takes arguments one by one unlike functions which takes all arguments at once.
Took the words right out of my brain.
still think it(redux) was a misplaced usage of currying.