News for hackers. Stuff that matters.
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'

There are no comments on this comment yet.