43
loading...
This website collects cookies to deliver better user experience
In mathematics and computer science, "currying" is the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a single argument.
fn(param1, param2, param3)
into this fn(param1)(param2)(param3)
.function sum(num1, num2, num3) {
return num1 + num2 + num3
}
// It can be invoked in two different ways
sum(1, 2, 3) // 6
sum.appply(null, [1, 2, 3]) // 6
sum
partially, which means, it fixes the first parameter and returns a function that expects the other two.function partialSum(num1) {
return function(num2, num3) {
return sum(num1, num2, num3)
}
}
// Using it
const sumTwo = partialSum(2)
sumTwo(3, 4) // 9
function partial(fn) {
const args = Array.prototype.slice.call(arguments, 1)
return function() {
return fn.apply(null, args.concat(Array.prototype.slice.call(arguments, 0)))
}
}
// Using it
const sumTwo = partial(sum, 2)
sumTwo(3, 4) // 9
"currying" is the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a single argument
partial(sum, 2)
returns a function that deals with two arguments instead of one.const sumTwo = curry(soma, 2)
sumTwo(3)(4) // 9
const logFn = (timestamp, type, message) =>
console.log(`[${date.getHours()}:${date.getMinutes()}] [${type}] ${message}`)
const { curry } from 'lodash/fp'
const log = curry(logfn)
const timestamp = new Date()
const type = 'WARNING'
const message = 'Some warning'
log(timestamp, type, message)
const timestamp = new Date(
const type = 'WARNING'
const message = 'Some warning'
log(time)(type)(message)
const logNow = log(new Date().getTime())
// Usage
logNow('INFO', 'Message') // [HH:mm] INFO Message