Re:Show HN: Omg-Curry – JavaScript currying library
by ufo on Monday August 10, 2020 @11:24PM
(#24116871)
The add example is a bit boring because all the arguments are the same "kind". Partial evaluation is more interesting if the arguments are more independent. To borrow a contrived example from the Lodash docs, you can use partial evaluation to make a custom greeter function:
https://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'
Re:Show HN: Omg-Curry – JavaScript currying library
by smichel17 on Tuesday August 11, 2020 @01:02AM
(#24117319)
It's nice for any type of list iteration. I'll share my "match" function (for use when filtering) when I'm at my computer tomorrow.
Re:Show HN: Omg-Curry – JavaScript currying library
by smichel17 on Monday August 24, 2020 @12:11PM
(#24262038)
It's tomorrow somewhere…
/**
* 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
);
The example doesn't feel that exciting...