ƒp
Functional programming utilities for JavaScript.
Install
npm install @bitmap/fp
Functions
Arrays
Objects
Strings
Utilities
reduce
Reduce values in a list according to a reducer function. reduce
args are curried.
reduce(reducer, initialValue, list)
Example
import { reduce } from '@bitmap/fp'
const sum = (a, b) => a + b
const items = [1, 2, 3, 4]
reduce(sum, 0, items)
reduceRight
Reduce values in a list according to a reducer function in reverse order. reduceRight
args are curried.
reduceRight(reducer, initialValue, list)
Example
import { reduceRight } from '@bitmap/fp'
const sum = (a, b) => a + b
const items = [1, 2, 3, 4]
reduceRight(sum, 0, items)
map
Apply function to each items in a list, and return a new list. map
args are curried.
map(mapFunction, list)
Example
import { map } from '@bitmap/fp'
const double = (n) => n * 2
const doubleAll = map(double)
doubleAll([1, 2, 3])
flat
Return a flattened list.
flat(list, depth)
Example
import { flat } from '@bitmap/fp'
flat([0, [1, 2], [3, 4], [5]])
flat([0, [1, 2, [3, 4, [5]]]], 3)
flatMap
Apply function to each items in a list, and return a flattened list. flatMap
args are curried.
flatMap(flatMapFunction, list)
Example
import { flatMap } from '@bitmap/fp'
const users = ['@cabe', '@bitmap']
const indexUsers = flatMap((user, index) => [index, user])
indexUsers(users)
filter
Filter items from a list, and return a new list. filter
args are curried.
filter(conditionFunction, list)
Example
import { filter } from '@bitmap/fp'
const isOdd = (n) => n % 2 !== 0
const filterOdds = filter(isOdd)
filterOdds([1, 2, 3, 4])
filterMap
Apply filter and map to a list, and return a new list. filterMap
args are curried.
filterMap(conditionFunction, mapFunction, list)
Example
import { filterMap } from '@bitmap/fp'
const isOdd = (n) => n % 2 !== 0
const double = (n) => n * 2
const doubleOdds = filterMap(isOdd, double)
doubleOdds([1, 2, 3, 4])
reject
Reject items from a list, and return a new list. The opposite of filter. reject
args are curried.
reject(conditionFunction, list)
Example
import { reject } from '@bitmap/fp'
const isOdd = (n) => n % 2 !== 0
const rejectOdds = reject(isOdd)
rejectOdds([1, 2, 3, 4])
rejectMap
Apply reject and map to a list, and return a new list. rejectMap
args are curried.
rejectMap(conditionFunction, mapFunction, list)
Example
import { rejectMap } from '@bitmap/fp'
const isOdd = (n) => n % 2 !== 0
const double = (n) => n * 2
const doubleEvens = rejectMap(isOdd, double)
doubleEvens([1, 2, 3, 4])
concat
Concat n
lists into one list.
concat(...lists)
Example
import { concat } from '@bitmap/fp'
const a = [1, 2, 3]
const b = [4, 5, 6]
concat(a, b)
copy
Returns a new copy of the list
copy(list)
Example
import { copy } from '@bitmap/fp'
const list = [1, 2, 3]
copy(list)
slice
Return a sliced list. slice
args are curried.
slice(start, end, list)
Example
import { slice } from '@bitmap/fp'
const list = [1, 2, 3, 4, 5, 6]
slice(2, 5, list)
append
Appends item to the end of a list. Unlike Array.prototype.push
, doesn't mutate target. append
args are curried.
append(item, list)
Example
import { append } from '@bitmap/fp'
const list = [1, 2]
append(3, list)
prepend
Prepends item to the beginning of a list. Unlike Array.prototype.unshift
, doesn't mutate target. prepend
args are curried.
prepend(item, list)
Example
import { prepend } from '@bitmap/fp'
const list = [1, 2]
prepend(0, list)
insert
Insert item into a list. Unlike Array.prototype.splice
, doesn't mutate target. insert
args are curried.
insert(start, item, list)
Example
import { insert } from '@bitmap/fp'
const list = [1, 3]
insert(1, 2, list)
insertAll
Insert items into a list. Unlike Array.prototype.splice
, doesn't mutate target. insertAll
args are curried.
insertAll(start, items, list)
Example
import { insertAll } from '@bitmap/fp'
const list = [1, 4]
insertAll(1, [2, 3], list)
reverse
Reverse a string or items in a list. Unlike Array.prototype.reverse
, doesn't mutate target.
reverse(list)
Example
import { reverse } from '@bitmap/fp'
const list = [1, 2, 3, 4]
reverse(list)
const string = 'functional'
reverse(string)
sort
Sorts items in a list according to comparator function. Unlike Array.prototype.sort
, doesn't mutate target.
sort(compareFunction, list)
Example
import { sort } from '@bitmap/fp'
const sortAscending = (a, b) => a - b
const list = [40, 21, 32, 17]
sort(sortAscending, list)
first
Returns first item in a list.
first(list)
Example
import { first } from '@bitmap/fp'
const list = [1, 2, 3, 4]
first(list)
last
Returns last item in a list.
last(list)
Example
import { last } from '@bitmap/fp'
const list = [1, 2, 3, 4]
last(list)
drop
Drops n
items from left. drop
args are curried.
drop(n, list)
Example
import { drop } from '@bitmap/fp'
const list = [1, 2, 3, 4]
drop(2, list)
dropRight
Drops n
items from right. dropRight
args are curried.
dropRight(n, list)
Example
import { dropRight } from '@bitmap/fp'
const list = [1, 2, 3, 4]
dropRight(2, list)
dropFirst
Drops first item from list.
dropFirst(list)
Example
import { dropFirst } from '@bitmap/fp'
const list = [1, 2, 3, 4]
dropFirst(2, list)
dropLast
Drops last item from list.
dropLast(list)
Example
import { dropLast } from '@bitmap/fp'
const list = [1, 2, 3, 4]
dropLast(2, list)
take
Takes n
items from left. take
args are curried.
take(n, list)
Example
import { take } from '@bitmap/fp'
const list = [1, 2, 3, 4]
take(2, list)
takeRight
Takes n
items from right. takeRight
args are curried.
takeRight(n, list)
Example
import { takeRight } from '@bitmap/fp'
const list = [1, 2, 3, 4]
takeRight(2, list)
any
Returns true if any item in list meet the condition. any
args are curried.
any(conditionFunction, list)
Example
import { any } from '@bitmap/fp'
const greaterThanTen = x => x > 10
const anyGreaterThanTen = any(greaterThanTen)
anyGreaterThanTen([3, 5, 7, 9])
anyGreaterThanTen([5, 20, 100])
all
Returns true if all item in list meet the condition. all
args are curried.
all(conditionFunction, list)
Example
import { all } from '@bitmap/fp'
const greaterThanTen = x => x > 10
const allGreaterThanTen = all(greaterThanTen)
allGreaterThanTen([3, 5, 7, 9])
allGreaterThanTen([5, 20, 100])
allGreaterThanTen([50, 15, 99])
find
Returns first item from list that meets predicate. find
args are curried.
find(conditionFunction, list)
Example
import { find } from '@bitmap/fp'
const greaterThanTen = x => x > 10
const findGreaterThanTen = find(greaterThanTen)
findGreaterThanTen([3, 5, 7, 9])
findGreaterThanTen([5, 20, 100])
findLast
Returns last item from list that meets predicate. findLast
args are curried.
findLast(conditionFunction, list)
Example
import { findLast } from '@bitmap/fp'
const greaterThanTen = x => x > 10
const findLastGreaterThanTen = findLast(greaterThanTen)
findLastGreaterThanTen([3, 5, 7, 9])
findLastGreaterThanTen([5, 20, 100])
includes
Returns true if item is in the list. includes
args are curried.
includes(value, list)
Example
import { includes } from '@bitmap/fp'
const hasApple = includes('apple')
hasApple(['orange', 'banana', 'pear'])
hasApple(['kiwi', 'apple', 'coconut'])
excludes
Returns true if item is not in the list. excludes
args are curried.
excludes(value, list)
Example
import { excludes } from '@bitmap/fp'
const hasApple = excludes('apple')
hasApple(['orange', 'banana', 'pear'])
hasApple(['kiwi', 'apple', 'coconut'])
position
Return index of first found item in list. If arg is a predicate function,
returns index of the first item in a list that meets the condition. If no item
meets the criteria, it returns -1. position
args are curried.
position(value, list)
position(predicate, list)
Example
import { position } from '@bitmap/fp'
const firstAppleIndex = position('apple')
firstAppleIndex(['orange', 'banana', 'pear', 'lemon'])
firstAppleIndex(['kiwi', 'apple', 'coconut', 'apple'])
positionLast
Return index of last found item in list. If arg is a predicate function, returns
index of the last item in a list that meets the condition. If no item meets the
criteria, it returns -1. positionLast
args are curried.
positionLast(value, list)
positionLast(predicate, list)
Example
import { positionLast } from '@bitmap/fp'
const lastAppleIndex = positionLast('apple')
lastAppleIndex(['orange', 'banana', 'pear', 'lemon'])
lastAppleIndex(['kiwi', 'apple', 'coconut', 'apple'])
prop
Returns the value of key
in object. prop
args are curried.
prop(key, object)
Example
import { prop } from '@bitmap/fp'
const data = {
name: 'Cabe',
username: 'bitmap',
title: 'Developer',
}
prop('username', data)
pluck
Returns map of key
values from a list of objects. pluck
args are curried.
pluck(key, list)
Example
import { pluck } from '@bitmap/fp'
const data = [
{
city: 'New York',
state: 'NY',
},
{
city: 'San Francisco',
state: 'CA',
},
{
city: 'Portland',
state: 'OR',
}
]
pluck('state', data)
pick
Returns copy of object with supplied keys
and all other properties omitted. pick
args are curried.
pick(keys, object)
Example
import { pick } from '@bitmap/fp'
const data = {
name: 'Cabe',
age: 32,
position: 'Developer',
state: 'NY',
city: 'New York',
}
pick(['name', 'position'], data)
omit
Returns copy of object with supplied keys
omitted. opposite of pick. omit
args are curried.
omit(keys, object)
Example
import { omit } from '@bitmap/fp'
const data = {
name: 'Cabe',
age: 32,
position: 'Developer',
state: 'NY',
city: 'New York',
}
omit(['age', 'state', 'city'], data)
split
Splits a string by delimiter into a list. split
args are curried.
split(delimiter, string)
Example
import { split } from '@bitmap/fp'
split(':', 'name:Cabe')
join
Joins a list into a string, seperating each item by specified delimiter. join
args are curried.
join(delimiter, list)
Example
import { join } from '@bitmap/fp'
join('|', [1, 2, 3, 4])
compose
Compose functions from right to left.
compose(...functions)(value)
Example
import { compose } from '@bitmap/fp'
const addOne = (n) => n + 1
const double = (n) => n * 2
const addOneThenDouble = compose(double, addOne)
addOneThenDouble(20)
pipe
Compose functions from left to right.
pipe(...functions)(value)
Example
import { pipe } from '@bitmap/fp'
const addOne = (n) => n + 1
const double = (n) => n * 2
const doubleThenAddOne = pipe(double, addOne)
doubleThenAddOne(20)
curry
Curry a function to allow it to be called partially.
curry(function)
Example
import { curry } from '@bitmap/fp'
const sum = curry((a, b, c) => a + b + c)
sum(1)(2)(3)
identity
Pass input as output.
idendity(value)
Example
import { idenity } from '@bitmap/fp'
const id = idenity('hello, world')
isEqual
Compares two items and returns true if equal. isEqual
args are curried.
isEqual(a, b)
Example
import { isEqual } from '@bitmap/fp'
isEqual(2, 2)
isEqual(2, 3)
isTypeOf
Evaluate the returned string from an operand.
Note: Additional helper functions isUndefined
, isFunction
, isBoolean
, isString
, isNumber
, isBigInt
, isSymbol
, and isObject
are also exported for convenience.
isTypeOf('undefined' | 'function' | 'boolean' | 'string' | 'number' | 'bigint' | 'symbol' | 'object', value)
Example
import { isTypeOf } from '@bitmap/fp'
isTypeOf('number', 2)
isTypeOf('object', [])
isArray
Returns true if value is of the Array
class.
isArray(value)
Example
import { isArray } from '@bitmap/fp'
isArray([1, 2, 3])
isArray({ length: 3, 0: 1, 1: 2, 2: 3 })
isNull
Returns true if value is null
isNull(value)
Example
import { isNull } from '@bitmap/fp'
let value = null
isNull(value)
value = 'hello, world'
isNull(value)