
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
@bitmap/fp
Advanced tools
Functional programming utilities for JavaScript.
npm install @bitmap/fp
Reduce values in a list according to a reducer function. reduce
args are curried.
reduce(reducer, initialValue, list)
import { reduce } from '@bitmap/fp'
const sum = (a, b) => a + b
const items = [1, 2, 3, 4]
reduce(sum, 0, items) // -> 10
Reduce values in a list according to a reducer function in reverse order. reduceRight
args are curried.
reduceRight(reducer, initialValue, list)
import { reduceRight } from '@bitmap/fp'
const sum = (a, b) => a + b
const items = [1, 2, 3, 4]
reduceRight(sum, 0, items) // -> 10
Apply function to each items in a list, and return a new list. map
args are curried.
map(mapFunction, list)
import { map } from '@bitmap/fp'
const double = (n) => n * 2
const doubleAll = map(double)
doubleAll([1, 2, 3]) // -> [2, 4, 6]
Return a flattened list.
flat(list, depth)
import { flat } from '@bitmap/fp'
flat([0, [1, 2], [3, 4], [5]]) // -> [0, 1, 2, 3, 4, 5]
flat([0, [1, 2, [3, 4, [5]]]], 3) // -> [0, 1, 2, 3, 4, 5]
Apply function to each items in a list, and return a flattened list. flatMap
args are curried.
flatMap(flatMapFunction, list)
import { flatMap } from '@bitmap/fp'
const users = ['@cabe', '@bitmap']
const indexUsers = flatMap((user, index) => [index, user])
indexUsers(users) // -> [0, '@cabe', 1, '@bitmap']
Filter items from a list, and return a new list. filter
args are curried.
filter(conditionFunction, list)
import { filter } from '@bitmap/fp'
const isOdd = (n) => n % 2 !== 0
const filterOdds = filter(isOdd)
filterOdds([1, 2, 3, 4]) // -> [1, 3]
Apply filter and map to a list, and return a new list. filterMap
args are curried.
filterMap(conditionFunction, mapFunction, list)
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]) // -> [3, 6]
Reject items from a list, and return a new list. The opposite of filter. reject
args are curried.
reject(conditionFunction, list)
import { reject } from '@bitmap/fp'
const isOdd = (n) => n % 2 !== 0
const rejectOdds = reject(isOdd)
rejectOdds([1, 2, 3, 4]) // -> [2, 4]
Apply reject and map to a list, and return a new list. rejectMap
args are curried.
rejectMap(conditionFunction, mapFunction, list)
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]) // -> [4, 8]
Concat n
lists into one list.
concat(...lists)
import { concat } from '@bitmap/fp'
const a = [1, 2, 3]
const b = [4, 5, 6]
concat(a, b) // -> [1, 2, 3, 4, 5, 6]
Returns a new copy of the list
copy(list)
import { copy } from '@bitmap/fp'
const list = [1, 2, 3]
copy(list) // -> [1, 2, 3]
Return a sliced list. slice
args are curried.
slice(start, end, list)
import { slice } from '@bitmap/fp'
const list = [1, 2, 3, 4, 5, 6]
slice(2, 5, list) // -> [3, 4, 5]
Appends item to the end of a list. Unlike Array.prototype.push
, doesn't mutate target. append
args are curried.
append(item, list)
import { append } from '@bitmap/fp'
const list = [1, 2]
append(3, list) // -> [1, 2, 3]
Prepends item to the beginning of a list. Unlike Array.prototype.unshift
, doesn't mutate target. prepend
args are curried.
prepend(item, list)
import { prepend } from '@bitmap/fp'
const list = [1, 2]
prepend(0, list) // -> [0, 1, 2]
Insert item into a list. Unlike Array.prototype.splice
, doesn't mutate target. insert
args are curried.
insert(start, item, list)
import { insert } from '@bitmap/fp'
const list = [1, 3]
insert(1, 2, list) // -> [1, 2, 3]
Insert itemm into a list. Unlike Array.prototype.splice
, doesn't mutate target. insertAll
args are curried.
insertAll(start, items, list)
import { insertAll } from '@bitmap/fp'
const list = [1, 4]
insertAll(1, [2, 3], list) // -> [1, 2, 3, 4]
Reverse a string or items in a list. Unlike Array.prototype.reverse
, doesn't mutate target.
reverse(list)
import { reverse } from '@bitmap/fp'
const list = [1, 2, 3, 4]
reverse(list) // -> [4, 3, 2, 1]
const string = 'functional'
reverse(string) // -> 'lanoitcnuf'
Sorts items in a list according to comparator function. Unlike Array.prototype.sort
, doesn't mutate target.
sort(compareFunction, list)
import { sort } from '@bitmap/fp'
const sortAscending = (a, b) => a - b
const list = [40, 21, 32, 17]
sort(sortAscending, list) // -> [17, 21, 32, 40]
Returns first item in a list.
first(list)
import { first } from '@bitmap/fp'
const list = [1, 2, 3, 4]
first(list) // -> 1
Returns last item in a list.
last(list)
import { last } from '@bitmap/fp'
const list = [1, 2, 3, 4]
last(list) // -> 4
Drops n
items from left. drop
args are curried.
drop(n, list)
import { drop } from '@bitmap/fp'
const list = [1, 2, 3, 4]
drop(2, list) // -> [3, 4]
Drops n
items from right. dropRight
args are curried.
dropRight(n, list)
import { dropRight } from '@bitmap/fp'
const list = [1, 2, 3, 4]
dropRight(2, list) // -> [1, 2]
Drops first item from list.
dropFirst(list)
import { dropFirst } from '@bitmap/fp'
const list = [1, 2, 3, 4]
dropFirst(2, list) // -> [2, 3, 4]
Drops last item from list.
dropLast(list)
import { dropLast } from '@bitmap/fp'
const list = [1, 2, 3, 4]
dropLast(2, list) // -> [1, 2, 3]
Takes n
items from left. take
args are curried.
take(n, list)
import { take } from '@bitmap/fp'
const list = [1, 2, 3, 4]
take(2, list) // -> [1, 2]
Takes n
items from right. takeRight
args are curried.
takeRight(n, list)
import { takeRight } from '@bitmap/fp'
const list = [1, 2, 3, 4]
takeRight(2, list) // -> [3, 4]
Returns true if any item in list meet the condition. any
args are curried.
any(conditionFunction, list)
import { any } from '@bitmap/fp'
const greaterThanTen = x => x > 10
const anyGreaterThanTen = any(greaterThanTen)
anyGreaterThanTen([3, 5, 7, 9]) // -> false
anyGreaterThanTen([5, 20, 100]) // -> true
Returns true if all item in list meet the condition. all
args are curried.
all(conditionFunction, list)
import { all } from '@bitmap/fp'
const greaterThanTen = x => x > 10
const allGreaterThanTen = all(greaterThanTen)
allGreaterThanTen([3, 5, 7, 9]) // -> false
allGreaterThanTen([5, 20, 100]) // -> false
allGreaterThanTen([50, 15, 99]) // -> true
Returns first item from list that meets predicate. find
args are curried.
find(conditionFunction, list)
import { find } from '@bitmap/fp'
const greaterThanTen = x => x > 10
const findGreaterThanTen = find(greaterThanTen)
findGreaterThanTen([3, 5, 7, 9]) // -> undefined
findGreaterThanTen([5, 20, 100]) // -> 20
Returns last item from list that meets predicate. findLast
args are curried.
findLast(conditionFunction, list)
import { findLast } from '@bitmap/fp'
const greaterThanTen = x => x > 10
const findLastGreaterThanTen = findLast(greaterThanTen)
findLastGreaterThanTen([3, 5, 7, 9]) // -> undefined
findLastGreaterThanTen([5, 20, 100]) // -> 100
Returns index of first item from list that meets predicate. findIndexOf
args are curried.
findIndexOf(conditionFunction, list)
import { findIndexOf } from '@bitmap/fp'
const greaterThanTen = x => x > 10
const findIndexOfGreaterThanTen = findIndexOf(greaterThanTen)
findIndexOfGreaterThanTen([3, 5, 7, 9]) // -> -1
findIndexOfGreaterThanTen([5, 20, 100]) // -> 1
Returns index of last item from list that meets predicate. findIndexOfLast
args are curried.
findIndexOfLast(conditionFunction, list)
import { findIndexOfLast } from '@bitmap/fp'
const greaterThanTen = x => x > 10
const findIndexOfLastGreaterThanTen = findIndexOfLast(greaterThanTen)
findIndexOfLastGreaterThanTen([3, 5, 7, 9]) // -> -1
findIndexOfLastGreaterThanTen([5, 20, 100]) // -> 2
Returns true if item is in the list. includes
args are curried.
includes(value, list)
import { includes } from '@bitmap/fp'
const hasApple = includes('apple')
hasApple(['orange', 'banana', 'pear']) // -> false
hasApple(['kiwi', 'apple', 'coconut']) // -> true
Returns first index of item in the list. indexOf
args are curried.
indexOf(value, list)
import { indexOf } from '@bitmap/fp'
const firstAppleIndex = indexOf('apple')
firstAppleIndex(['orange', 'banana', 'pear', 'lemon']) // -> -1
firstAppleIndex(['kiwi', 'apple', 'coconut', 'apple']) // -> 1
Returns last index of item in the list. indexOfLast
args are curried.
indexOfLast(value, list)
import { indexOfLast } from '@bitmap/fp'
const lastAppleIndex = indexOfLast('apple')
lastAppleIndex(['orange', 'banana', 'pear', 'lemon']) // -> -1
lastAppleIndex(['kiwi', 'apple', 'coconut', 'apple']) // -> 3
Compose functions from left to right.
pipe(...functions)(value)
import { pipe } from '@bitmap/fp'
const addOne = (n) => n + 1
const double = (n) => n * 2
const doubleThenAddOne = pipe(double, addOne)
doubleThenAddOne(20) // 41
Compose functions from right to left.
compose(...functions)(value)
import { compose } from '@bitmap/fp'
const addOne = (n) => n + 1
const double = (n) => n * 2
const addOneThenDouble = compose(double, addOne)
addOneThenDouble(20) // 42
Curry a function to allow it to be called partially.
curry(function)
import { curry } from '@bitmap/fp'
const sum = curry((a, b, c) => a + b + c)
sum(1)(2)(3) // -> 6
Returns the value of key
in object. prop
args are curried.
prop(key, object)
import { prop } from '@bitmap/fp'
const data = {
name: 'Cabe',
username: 'bitmap',
title: 'Developer',
}
prop('username', data) // -> 'bitmap'
Returns map of key
values from a list of objects. pluck
args are curried.
pluck(key, list)
import { pluck } from '@bitmap/fp'
const data = [
{
city: 'New York',
state: 'NY',
},
{
city: 'San Francisco',
state: 'CA',
},
{
city: 'Portland',
state: 'OR',
}
]
pluck('state', data) // -> ['NY', 'CA', 'OR']
Returns copy of object with supplied keys
and all other properties omitted. pick
args are curried.
pick(keys, object)
import { pick } from '@bitmap/fp'
const data = {
name: 'Cabe',
age: 32,
position: 'Developer',
state: 'NY',
city: 'New York',
}
pick(['name', 'position'], data) // -> { name: 'Cabe', position: 'Developer' }
Returns copy of object with supplied keys
omitted. opposite of pick. omit
args are curried.
omit(keys, object)
import { omit } from '@bitmap/fp'
const data = {
name: 'Cabe',
age: 32,
position: 'Developer',
state: 'NY',
city: 'New York',
}
omit(['age', 'state', 'city'], data) // -> { name: 'Cabe', position: 'Developer' }
Splits a string by delimiter into a list. split
args are curried.
split(delimiter, string)
import { split } from '@bitmap/fp'
split(':', 'name:Cabe') // -> ['name', 'Cabe']
Joins a list into a string, seperating each item by specified delimiter. join
args are curried.
join(delimiter, list)
import { join } from '@bitmap/fp'
join('|', [1, 2, 3, 4]) // -> '1|2|3|4'
FAQs
Functional programming utilities
The npm package @bitmap/fp receives a total of 30 weekly downloads. As such, @bitmap/fp popularity was classified as not popular.
We found that @bitmap/fp demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.