0.0.1 • Published 5 years ago

@sullux/fp-light-curry v0.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

home

fp-light-curry

npm i @sullux/fp-light-curry source test

To curry a function is to make it so that arguments can be progressively applied.

The following example demonstrates an uncurried function versus a manually curried function.

const uncurriedAdd = (x, y) => x + y
uncurriedAdd(1, 2) // 3
uncurriedAdd(1) // NaN

const add = x => y => x + y
add(1)(2) // 3
add(1) // function
const increment = add(1)
increment(2) // 3

curry

curry(fn: Function, arity: ?Number): Function

Returns a function that supports partial argument application. Note: the term "arity" means "number of arguments". If the arity is not supplied, the value of fn.length is used. Note that fn.length will not include optional or rest arguments.

A vanilla example of currying:

const add = curry((x, y) => x + y)
add(1, 2) // 3
const increment = add(1)
increment(2) // 3

An example showing that optional arguments are not curried by default:

// optional argument is not curried!
const mul = curry((x, y = 1) => x * y)
mul(2, 3) // 6
mul(2) // 2

An example using arity to explicitly curry an optional argument:

const div = curry((x, y = 1) => x / y, 2) // note: arity 2 is explicit
div(6, 3) // 2
const half = div(2)
half(6) // 3