functional javascript playground
npm install fjp





Functional Programming was always a weird "why would you use this" way of coding as I've primarily been OOP. I set out to change my view on that and learn more about it and when to use it.
These are some of the functions I collected/made along the way. They are all ES6/ESM, the parameter order for most lends itself to currying + composing. Still a little iffy on the Hindley-milner signatures so they may be slightly off.
Sets the given property and value on the object. Returning a new object.
Averages the given array values
Calls the given function with the given value.
Takes a joiner func, and two other funcs and a value. The value is given to both funcs and the
results of each of these is given to the joiner func.
Returns the given value.
Takes two values and returns the given first.
Given two functions that take the same value, returns the first if the result is truthy, otherwise, the second.
Calls the given function with the given value. (Reverse order of apply)
Removes falsey values from an array.
Performs right-to-left function composition.
Concatenates two String|Arrays together.
Returns empty array if value arent of the same type or not String|Array.
Concatenates N Arrays together.
Wraps the given function, if the number of provided args is sufficient, call the passed function fn.
Otherwise, return a wrapped function fn that expects the rest of the arguments.
If you want to curry a function that accepts a variable number of arguments (a variadic function),
you can optionally pass the number of arguments to the second parameter arity.
Returns the difference between two arrays.
Returns all of the distinct values of an array.
Returns all of the distinct values of the given arrays.
Applies the given func to each element in the array.
Determines if all element in an array satisfy the given test function
Filters the array using the given function.
Finds the first element that satisfies the given test func.
Flattens single nested array.
Determines if the given value is an array.
Determines if the given value is a function.
Determines if the given value is a number.
Determines if the given value is an object.
Determines if the given value is a string.
Negates the given boolean-like value.
Calls the given function with the given value and returns the value.
Kind: global function
Signature: associate :: String k -> {} -> v -> {k: v}
Example
``js`
const obj = associate('c', { a: b }, d) // { a: b, c: d }
Kind: global function
Signature: average :: [Number] -> Number
Example
`js`
average([ 1, 2, 3 ]); // 2
average(1, 2, 3); // 2
average(); // 0
Kind: global function
Signature: A :: (a -> b) -> a -> b
Aka: apply
Kind: global function
Signature: Fork :: (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d
Aka: join
Kind: global function
Signature: I :: a -> a
Aka: identity
Kind: global function
Signature: K :: a -> b -> a
Aka: constant
Kind: global function
Signature: OR :: (a -> b) -> (a -> b) -> b
Aka: alternation
Kind: global function
Signature: T :: a -> (a -> b) -> b
Aka: thrush, applyTo
Kind: global function
Signature: compact :: [a] -> [a]
Example
`js`
compact([ 0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34 ]); // [ 1, 2, 3, 'a', 's', 34 ]
compact(null) // []
Kind: global function
Signature: compose :: [(m -> n), ..., (b -> c), (a -> b)] -> a -> n
Example
`js`
const addOne = x => x + 1;
const timeTen = x => x * 10;
const addOneTimeTen = compose(timeTen, addOne);
const result = addOneTimeTen(9); // 100
Kind: global function
Signature: concat :: a -> b -> c
Example
`js`
concat('foo', 'bar') // foobar
concat([1, 2], [3, 4]) // [1, 2, 3, 4]
Kind: global function
Signature: concat :: [a] -> [b] -> ...[n] -> [m]
Example
`js`
concatN([1, 2], [3, 4], [5, 6]) // [1, 2, 3, 4, 5, 6]
Kind: global function
Signature: curry :: ((a, b, ..., n) -> m) -> a -> b -> ...n -> m
Example
`js`
const add = curry((x, y) => x + y);
const addFiveTo = add(5);
addFiveTo(10); // 15
Kind: global function
Signature: difference :: [a] -> [b] -> [c]
Example
`js`
difference([ 1, 2, 3 ], [ 1, 2, 4 ]) // [3]
difference([], [ 1, 2, 4 ]) // [ 1, 2, 4 ]
difference([ 1, 2, 3 ], []) // [ 1, 2, 3 ]
difference([ 1, 2, 3 ], null) // [ 1, 2, 3 ]
Kind: global function
Signature: distinct :: [a] -> [b]
Example
`js`
distinct([ 1, 2, 2, 3, 4, 4, 5 ]) // [ 1, 2, 3, 4, 5 ]
Kind: global function
Signature: distinctN :: [a] -> [b] -> ...[n] -> [m]
Example
`js`
distinctN([ 1, 2 ], [ 2, 3, 4 ], [ 4, 5 ]) // [ 1, 2, 3, 4, 5 ]
Kind: global function
Signature: each :: (a -> b) -> [c] -> undefined
Example
`js`
difference(log, [1, 2, 3])
Kind: global function
Signature: every :: (a -> Bool) -> [a] -> Bool
Aka: all
Example
`js`
every(Boolean, [1, 2, 3, 4]) // true
every(Boolean, [1, 2, null, 4]) // false
Kind: global function
Signature: filter :: (a -> Boolean) -> [a] -> [a]
Example
`js`
filter(x => x > 5, [1, 2, 3, 5, 6, 7]) // [6, 7]
Kind: global function
Signature: find :: (a -> Boolean) -> [a] -> a
Example
`js`
find(x => x.score === 5, [{score: 1}, {score: 2}, {score: 5}, {score: 6}, {score: 7}]) // {score: 5}
Kind: global function
Signature: flatten :: [a] -> [a]
Example
`js`
flatten([[ 1, 2 ], [ 3, 4 ]];); // [ 1, 2, 3, 4 ]
flatten(null) // []
Kind: global function
Signature: isArray :: a -> Boolean
Example
`js`
isArray([1, 2, 3]) // true
isArray({ a: 'b' }) // false
Kind: global function
Signature: isFunction :: a -> Boolean
Example
`js`
isFunction(() => {}) // true
isFunction([1, 2, 3]) // false
Kind: global function
Signature: isNumber :: a -> Boolean
Example
`js`
isNumber(42) // true
isNumber(8e5) // true
isNumber(0x2F) // true
isNumber('foo bar') // false
Kind: global function
Signature: isObject :: a -> Boolean
Example
`js`
isObject({ a: 'b' }) // true
isObject([1, 2, 3]) // false
Kind: global function
Signature: isString :: a -> Boolean
Example
`js`
isString('foo bar') // true
isString({ a: 'b' }) // false
Kind: global function
Signature: not :: Boolean -> Boolean
Example
`js`
not(true); // false
not(false); // true
not(2); // false
Kind: global function
Signature: tap :: (a -> b) -> a -> a
Example
`js``
tap(console.log, 'foobar') // foobar
FJP is licensed under the MIT license.
Copyright © 2018 Bryan Kizer