3 ms·
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
by ufo 6y ago
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#partial
https://lodash.com/docs/4.17.15#curry 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'