1.0.0 • Published 7 years ago

to-curried v1.0.0

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

Build Status

to-curried

Convert JavaScript function to curried function.

toCurried(fn, arity, [argsFn]) ⇒ Function | *

Transform the function into functional programming friendly function, like those in lodash and date-fns, which support currying and functional-style function composing.

If arity is 0, toCurried calls a function immediately and returns its result.

Returns

  • Function | *: the resulted curried function (or the constant if arity is 0)

Throws

  • RangeError: arity must be non-negative number

Arguments

ParamTypeDescription
fnFunctionthe function to convert
arityNumberthe number of arguments of the resulted curried function
[argsFn]Functioncallback that transforms the list of arguments

Example

import toCurried from 'to-curried'

function map (mapFn, xs) {
  return xs.map(mapFn)
}

const curriedMap = toCurried(map, 2)
const multiplyEachBy2 = curriedMap(x => x * 2)
console.log(multiplyEachBy2([1, 2, 3, 4]))
//=> [2, 4, 6, 8]

Example

// Change an order of arguments in curried function with optional callback:
import toCurried from 'to-curried'

function reduce (xs, reduceFn, initialValue) {
  return xs.reduce(reduceFn, initialValue)
}

const curriedReduce = toCurried(reduce, 3, args => args.reverse())
const sum = curriedReduce(0, (a, b) => a + b)
console.log(sum([1, 2, 3, 4]))
// => 10
1.0.0

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago