1.6.0
Returns the sum of two numbers.
Number
:
The result of
b + a
.
import { add } from 'fkit'
add(1, 2) // 3
Determines whether all elements in a list satisfy a predicate function.
Boolean
:
true
if all elements of the list of
as
satisfy the
predicate,
false
otherwise.
import { all, eq, gt } from 'fkit'
all(gt(1), [1, 2, 3]) // false
all(gt(1), [2, 3]) // true
all(gt(1), [3]) // true
all(eq('o'), 'foo') // false
all(eq('o'), 'oo') // true
all(eq('o'), 'o') // true
Returns a function that always returns the same value, regardless of the arguments.
(any)
The constant value.
Function
:
A function that always returns the value
c
.
import { always } from 'fkit'
always(1)() // 1
Returns the logical AND of two values.
(any)
The first value.
(any)
The second value.
Boolean
:
The result of
b && a
.
import { and } from 'fkit'
and(true, true) // true
and(0, 1) // false
and('', 'foo') // false
Determines whether any elements in a list satisfy a predicate function.
Boolean
:
true
if any elements in the list of
as
satisfy the
predicate function
p
,
false
otherwise.
import { any, eq, gt } from 'fkit'
any(gt(1), [1, 2, 3]) // true
any(gt(1), [1, 2]) // true
any(gt(1), [1]) // false
any(eq('o'), 'foo') // true
any(eq('o'), 'fo') // true
any(eq('o'), 'f') // false
Appends a value to a list.
(Array | String)
:
A list that contains the value
a
appended to the
list of
bs
.
import { append } from 'fkit'
append(3, [1, 2]) // [1, 2, 3]
append('o', 'fo') // 'foo'
Applies a function to a value.
(Function)
The function to apply.
(any)
The value.
any
:
The result of
f(a)
.
import { apply } from 'fkit'
const sayHi = a => ['Hi', a, '!'].join(' ')
apply(sayHi, 'jane') // Hi Jane!
Applies a function to two values.
any
:
The result of
f(a, b)
.
import { apply2 } from 'fkit'
const sayHi = (a, b) => ['Hi', a, b, '!'].join(' ')
apply2(sayHi, 'Jane', 'Appleseed') // Hi Jane Appleseed!
Applies a function to three values.
(Function)
The function to apply.
(any)
The first value.
(any)
The second value.
(any)
The third value.
any
:
The result of
f(a, b, c)
.
import { apply3 } from 'fkit'
const sayHi = (a, b, c) => ['Hi', a, b, c, '!'].join(' ')
apply3(sayHi, 'Ms', 'Jane', 'Appleseed') // Hi Ms Jane Appleseed!
Applies a method to a value.
any
:
The result of the method
k
of object
o
applied to the value
a
.
import { applyMethod } from 'fkit'
const person = { sayHi: a => ['Hi', a, '!'].join(' ') }
applyMethod('sayHi', 'Jane', person) // Hi Jane!
Applies a method to two values.
(String)
The method to apply.
(any)
The first value.
(any)
The second value.
(Object)
The object.
any
:
The result of the method
k
of object
o
applied to the values
a
and
b
.
import { applyMethod2 } from 'fkit'
const person = { sayHi: (a, b) => ['Hi', a, b, '!'].join(' ') }
applyMethod2('sayHi', 'Jane', 'Appleseed', person) // Hi Jane Appleseed!
Applies a method to three values.
(String)
The method to apply.
(any)
The first value.
(any)
The second value.
(any)
The third value.
(Object)
The object.
any
:
The result of the method
k
of object
o
applied to the values
a
,
b
, and
c
.
import { applyMethod3 } from 'fkit'
const person = { sayHi: (a, b, c) => ['Hi', a, b, c, '!'].join(' ') }
applyMethod3('sayHi', 'Ms', 'Jane', 'Appleseed', person) // Hi Ms Jane Appleseed!
Applies a function to a value. This is similar to apply
, however the order
of the arguments is flipped.
(any)
The value.
(Function)
The function to apply.
any
:
The result of
f(a)
.
import { applyRight } from 'fkit'
function sayHi (a) { return ['Hi', a, '!'].join(' ') }
applyRight('Jane', sayHi) // Hi Jane!
Creates a new array.
(Number)
The length of the array.
Array
:
An array of length
n
.
import { array } from 'fkit'
array(3) // [undefined, undefined, undefined]
Determines whether a number is between two numbers.
Number
:
true
if the number
n
is between the numbers
a
and
b
(i.e. a <= n <= b),
false
otherwise.
import { between } from 'fkit'
between(1, 3, 2) // true
between(1, 3, 4) // false
Converts a function of any arity to a binary function.
(Function)
The function.
Function
:
A function that wraps the function
f
to accept only
two arguments.
import { binary } from 'fkit'
function f (a, b) { ... }
const g = binary(f)
g(1, 2, 3) // f(1, 2)
Branches execution based on a predicate function.
If p(a)
is true then f
is applied to a
, otherwise g
is applied to
a
.
(Function)
The predicate function.
(Function)
The function to apply when the predicate is satisfied.
(Function)
The function to apply when the predicate is unsatisfied.
(any)
The value.
any
:
The result.
import { branch, gt } from 'fkit'
const big = a => a + ' is a big number'
const small = a => a + ' is a small number'
const f = branch(gt(10), big, small)
f(10) // small number
f(11) // big number
Calculates the Cartesian product of two lists.
Array
:
Returns a list that contains all the ordered pairs
[a, b]
in the lists of
as
and
bs
.
import { cartesian } from 'fkit'
cartesian([1, 2], [3, 4]) // [[1, 3], [1, 4], [2, 3], [2, 4]]
cartesian('ab', 'cd') // [['a', 'c'], ['a', 'd'], ['b', 'c'], ['b', 'd']]
Clamps a number between two values.
Number
:
The result of clamping the number
n
between the numbers
a
and
b
(i.e.
a <= n <= b
).
import { clamp } from 'fkit'
clamp(1, 3, 0) // 1
clamp(1, 3, 2) // 2
clamp(1, 3, 4) // 3
Compares two values using natural ordering.
This function compares two elements, a
and b
. If a
is greater than
b
, then it returns 1
. If a
is less than b
, then it returns -1
. If
both elements are equal, then it returns 0
.
(any)
The first value.
(any)
The second value.
Number
:
The result.
import { compare } from 'fkit'
compare(2, 1) // 1
compare(1, 2) // -1
compare(2, 2) // 0
Composes a list of functions.
(Array)
The functions to compose.
Function
:
A function that is the composition of the list of
functions
fs
.
import { compose } from 'fkit'
compose(f, g, h)(a) // f(g(h(a)))
Concatenates multiple lists into a single list.
(Array | String)
:
A list that contains the concatenated elements in
the list of
as
.
import { concat } from 'fkit'
concat([1], [2, 3], [4, 5, 6]) // [1, 2, 3, 4, 5, 6]
concat('f', 'oo', 'bar') // 'foobar'
Maps a function, that returns a list, over a list and concatenates the results.
(Function)
The function to apply to each value in the list. It must
also return a list.
(Array | String)
:
A list that contains the elements in the list of
as
, mapped with the function
f
, and concatenated together.
import { concatMap } from 'fkit'
concatMap(a => [a, 0], [1, 2, 3]) // [1, 0, 2, 0, 3, 0]
concatMap(a => [a, '-'], 'foo') // 'f-o-o-'
Creates a copy of one or more objects.
Properties with the same key will take precedence from right to left. The copy will have the same prototype as the first object in the list.
(Array)
The objects to copy.
Object
:
A copy of the objects in the list of
os
.
import { copy } from 'fkit'
const person = { name: 'Jane', age: 20, city: 'Melbourne' }
copy(person, { name: 'Steve' }) // { name: 'Steve', age: 20, city: 'Melbourne' }
Curries the given function f
.
Currying turns a function that takes multiple arguments into a function that can be applied to one or more values to give a partially applied function.
(Function)
The function to curry.
Function
:
A curried function.
import { curry } from 'fkit'
const add = curry((a, b) => a + b)
const add2 = add(2)
add2(1) // 3
Decrements a number.
(Number)
The number.
Number
:
The result of
a - 1
.
import { dec } from 'fkit'
dec(1) // 0
Calculates the difference of two lists.
This is a special case of the differenceBy
function where the elements are
compared using the strict equality ===
operator.
(Array | String)
:
A list that contains the difference of the elements
in the lists of
as
and
bs
.
import { difference } from 'fkit'
difference([1, 2, 3], [2, 3, 4]) // [1]
difference('hello', 'world') // 'he'
Calculates the difference of two lists.
The comparator function f
compares two elements, a
and b
. If the
elements are considered to be equal, then the comparator function should
return true
. Otherwise it should return false
.
(Function)
The comparator function.
(Array | String)
:
A list that contains the difference of the elements
in the lists of
as
and
bs
.
import { differenceBy } from 'fkit'
differenceBy((a, b) => a === b, [1, 2, 3], [2, 3, 4]) // [1]
differenceBy((a, b) => a === b, 'hello', 'world') // 'he'
Returns the division of two numbers.
Number
:
The result of
b / a
.
import { div } from 'fkit'
div(2, 10) // 5
Drops a number of elements from the start of a list.
(Array | String)
:
The result after dropping
n
elements from the list
of
as
.
import { drop } from 'fkit'
drop(2, [1, 2, 3]) // [3]
drop(2, 'foo') // 'o'
Drops elements from the start of a list using a predicate function.
(Array | String)
:
A list that drops elements from the list of
as
while the predicate function
p
is satisfied.
import { dropWhile, lt, neq } from 'fkit'
dropWhile(lt(3), [1, 2, 3]) // [3]
dropWhile(neq(o), 'foo') // 'oo'
Determines if a value is present in a list.
Boolean
:
true
if the list of
as
contains the value
a
,
false
otherwise.
import { elem } from 'fkit'
elem(0, [1, 2, 3]) // false
elem(1, [1, 2, 3]) // true
elem('a', 'foo') // false
elem('o', 'foo') // true
Gets the index of the first occurance of a value in a list.
Number
:
The index of the first occurance of the value
a
in the
list of
as
, or
undefined
if no value was found.
import { elemIndex } from 'fkit'
elemIndex(0, [1, 2, 3]) // undefined
elemIndex(1, [1, 2, 3]) // 0
elemIndex('a', 'foo') // undefined
elemIndex('o', 'foo') // 1
Gets the indices of all occurances of a value in a list.
Array
:
The indices of all occurances of the value
a
in the list
of
as
.
import { elemIndices } from 'fkit'
elemIndices(0, [1, 2, 3]) // []
elemIndices(1, [1, 2, 3]) // [0]
elemIndices('a', 'foo') // []
elemIndices('o', 'foo') // [1, 2]
Determines if a list is empty.
Boolean
:
true
if the list of
as
is empty,
false
otherwise.
import { empty } from 'fkit'
empty([]) // true
empty([1, 2, 3]) // false
empty('') // true
empty('foo') // false
Determines whether two values are equal.
(any)
The first value.
(any)
The second value.
Boolean
:
true
if the value
a
is strictly not equal (
===
) to
the value
b
, false otherwise.
import { eq } from 'fkit'
eq(1, 1) // true
eq(1, 2) // false
Determines whether a number is even.
(Number)
The number.
Boolean
:
true
if the number
a
is even, false otherwise.
import { even } from 'fkit'
even(2) // true
even(1) // false
Filters a list using a predicate function.
(Array | String)
:
A list that contains only the elements in the list
of
as
that satisfy the predicate function
p
.
import { eq, filter, gt } from 'fkit'
filter(gt(1), [1, 2, 3]) // [2, 3]
filter(eq('o'), 'foo') // 'oo'
Finds the first value in a list that satisfies a predicate function.
any
:
A value in the list of
as
that satisfies the predicate function
p
, or
undefined
if no value was found.
import { eq, find, gt } from 'fkit'
find(gt(1), []) // undefined
find(gt(1), [1, 2, 3]) // 2
find(eq('o'), '') // undefined
find(eq('o'), 'foo') // 'o'
Finds the index of the first occurance of a value in a list that satisfies a predicate function.
Number
:
An index of the first occurance of a value in the list of
as
that satisfies the predicate function
p
, or
undefined
if no value
was found.
import { eq, findIndex, gt } from 'fkit'
findIndex(gt(1), []) // undefined
findIndex(gt(1), [1, 2, 3]) // 1
findIndex(eq('o'), '') // undefined
findIndex(eq('o'), 'foo') // 1
Finds the indices of all the values in a list that satisfy a predicate function.
Array
:
A list of indices that satisfy the predicate function
p
.
import { eq, findIndices, gt } from 'fkit'
findIndices(gt(1), []) // []
findIndices(gt(1), [1, 2, 3]) // [1, 2]
findIndices(eq('o'), '') // []
findIndices(eq('o'), 'foo') // [1, 2]
Swaps the order of the arguments to a function.
Function
:
The result of applying the function
f
to the values
b
and
a
.
import { flip } from 'fkit'
function f (a, b) { ... }
const g = flip(f)
g(1, 2) // f(2, 1)
Folds a list from left-to-right with a function.
(Function)
The folding function.
(any)
The starting value.
(Array | String)
:
A list that contains the elements in the list of
as
folded left-to-right with the binary function
f
and starting value
s
.
import { fold } from 'fkit'
fold((a, b) => a.concat(b), [], [1, 2, 3]) // [1, 2, 3]
fold((a, b) => a.concat(b), '', 'foo') // 'foo'
Folds a list from right-to-left with a function.
(Function)
The folding function.
(any)
The starting value.
(Array | String)
:
A list that contains the elements in the list of
as
folded right-to-left with the binary function
f
and starting value
s
.
import { foldRight } from 'fkit'
foldRight((b, a) => a.concat(b), [], [1, 2, 3]) // [3, 2, 1]
foldRight((b, a) => a.concat(b), '', 'foo') // 'oof'
Gets a property of an object.
any
:
The property at the key
k
in the object
o
.
import { get } from 'fkit'
const person = { name: 'Jane', age: 20, city: 'Melbourne' }
get('name', person) // 'Jane'
Gets a property of an object using a key path.
any
:
The property at the key path
ks
in the object
o
.
import { getIn } from 'fkit'
const person = { name: 'Jane', age: 20, address: { city: 'Melbourne', country: 'Australia' } }
getIn(['address', 'city'], person) // 'Melbourne'
getIn('address.city', person) // 'Melbourne'
Groups the values in a list.
This is a special case of the groupBy
function where the values are compared
using the strict equality ===
operator.
(Array | String)
:
A list that contains the values in the list of
as
grouped into sublists of equal values.
import { group } from 'fkit'
group([1, 2, 2, 3, 3, 3]) // [[1], [2, 2], [3, 3, 3]]
group('Mississippi') // ['M', 'i', 'ss', 'i', 'ss', 'i', 'pp', 'i']
Groups the values in a list using a comparator function.
The comparator function f
compares two values, a
and b
. If the values
are both considered to be in the same group, then the comparator function
should return true
. Otherwise it should return false
.
(Array | String)
:
A list that contains the values in the list of
as
grouped into sublists that satisfy the comparator function
c
.
import { groupBy } from 'fkit'
groupBy((a, b) => a === b, [1, 2, 2, 3, 3, 3]) // [[1], [2, 2], [3, 3, 3]]
groupBy((a, b) => a === b, 'Mississippi') // ['M', 'i', 'ss', 'i', 'ss', 'i', 'pp', 'i']
Determines whether one number is greater than another.
Boolean
:
true
if the number
b
is greater than the number
a
,
false otherwise.
import { gt } from 'fkit'
gt(1, 2) // true
gt(2, 1) // false
Determines whether one number is greater than or equal to another.
Boolean
:
true
if the number
b
is greater than or euqal to the
number
a
, false otherwise.
import { gte } from 'fkit'
gte(1, 2) // true
gte(2, 1) // false
gte(2, 2) // true
Gets the first element in a list.
any
:
The first element in the list of
as
, or
undefined
if the list
is empty.
import { head } from 'fkit'
head([1, 2, 3]) // 1
head('foo') // 'f'
The identity function.
(any)
The value to return.
any
:
The value
a
unchanged.
import { id } from 'fkit'
id(1) // 1
Increments a number.
(Number)
The number.
Number
:
The result of
a + 1
.
import { inc } from 'fkit'
inc(1) // 2
Gets the elements before the last element in a list.
(Array | String)
:
A list that contains the elements before the last
element in the list of
as
.
import { init } from 'fkit'
init([1, 2, 3]) // [1, 2]
init('foo') // 'fo'
Gets all initial segments of a list.
Array
:
A list that contains all initial segments of the list of
as
.
import { inits } from 'fkit'
inits([1, 2, 3]) // [[], [1], [1, 2], [1, 2, 3]]
inits('foo') // ['', 'f', 'fo', 'foo']
Calculates the intersection of two lists.
This is a special case of the intersectBy
function where the elements are
compared using the strict equality ===
operator.
Duplicates are removed from bs
, but if as
contains duplicates then so
will the result.
(Array | String)
:
A list that contains the intersection of the elments
in the lists of
as
and
bs
.
import { intersect } from 'fkit'
intersect([1, 2, 3], [2, 3, 4]) // [2, 3]
intersect('hello', 'world') // 'ol'
Calculates the intersection of two lists.
The comparator function f
compares two elements, a
and b
. If the
elements are considered to be equal, then the comparator function should
return true
. Otherwise it should return false
.
Duplicates are removed from bs
, but if as
contains duplicates then so
will the result.
(Function)
The comparator function.
(Array | String)
:
A list that contains the intersection of the elments
in the lists of
as
and
bs
.
import { intersectBy } from 'fkit'
intersectBy((a, b) => a === b, [1, 2, 3], [2, 3, 4]) // [2, 3]
intersectBy((a, b) => a === b, 'hello', 'world') // 'ol'
Intersperses the elements of a list with separator.
(Array | String)
:
A list that contains the elements in the list of
as
interspersed with the separator
s
.
import { intersperse } from 'fkit'
intersperse(4, [1, 2, 3]) // [1, 4, 2, 4, 3]
intersperse('-', 'foo') // 'f-o-o'
Determines if a list is contained within another list.
Boolean
:
true
if the list of
as
is contained within the list
of
bs
,
false
otherwise.
import { isInfixOf } from 'fkit'
isInfixOf([], [1, 2, 3]) // true
isInfixOf([2, 3], [1, 2, 3]) // true
isInfixOf([3, 2], [1, 2, 3]) // false
isInfixOf('', 'foo') // true
isInfixOf('oo', 'foo') // true
isInfixOf('of', 'foo') // false
Determines if a list is a prefix of another list.
Boolean
:
true
if the list of
as
is a prefix of the list of
bs
,
false
otherwise.
import { isPrefixOf } from 'fkit'
isPrefixOf([], [1, 2, 3]) // true
isPrefixOf([1, 2], [1, 2, 3]) // true
isPrefixOf([2, 3], [1, 2, 3]) // false
isPrefixOf('', 'foo') // true
isPrefixOf('fo', 'foo') // true
isPrefixOf('oo', 'foo') // false
Determines if a list is a suffix of another list.
Boolean
:
true
if the list of
as
is a suffix of the list of
bs
,
false
otherwise.
import { isSuffixOf } from 'fkit'
isSuffixOf([], [1, 2, 3]) // true
isSuffixOf([1, 2], [1, 2, 3]) // false
isSuffixOf([2, 3], [1, 2, 3]) // true
isSuffixOf('', 'foo') // true
isSuffixOf('fo', 'foo') // false
isSuffixOf('oo', 'foo') // true
Gets the keys of an object.
(Object)
The object.
Array
:
A list of keys for the properties of the object
o
.
import { keys } from 'fkit'
const person = { name: 'Jane', age: 20, city: 'Melbourne' }
keys(person) // ['name', 'age', 'city']
Gets the last element in a list.
any
:
The last element in the list of
as
, or
undefined
if the list is empty.
import { last } from 'fkit'
last([1, 2, 3]) // 3
last('foo') // 'o'
Gets the length of a list.
Number
:
The number of elements in the list of
as
.
import { length } from 'fkit'
length([1, 2, 3]) // 3
length('foo') // 3
Determines whether one number is less than another.
Boolean
:
true
if the number
b
is less than the number
a
,
false otherwise.
import { lt } from 'fkit'
lt(1, 2) // false
lt(2, 1) // true
Determines whether one number is less than or equal to another.
Boolean
:
true
if the number
b
is less than or equal to the
number
a
, false otherwise.
import { lte } from 'fkit'
lte(1, 2) // false
lte(2, 1) // true
lte(2, 2) // true
Maps a function over a list.
(Array | String)
:
A list that contains the elements in the list of
as
mapped with the function
f
.
import { inc, map, toUpper } from 'fkit'
map(inc, [1, 2, 3]) // [2, 3, 4]
map(toUpper, 'foo') // ['F', 'O', 'O']
Determines the largest of two numbers.
Number
:
The largest of the numbers
a
and
b
.
import { max } from 'fkit'
max(1, 2) // 2
Calculates the maximum value of a list.
This a special case of the maximumBy
function where the values are
compared using natural ordering.
any
:
Returns the maximum value in the list of
as
.
import { maximum } from 'fkit'
maximum([1, 2, 3]) // 3
maximum('abc') // 'c'
Calculates the maximum value of a list using a comparator function.
The comparator function compares two elements, a
and b
. If a
is
greater than b
, then the comparator function should return 1
. If a
is
less than b
, then the comparator function should return -1
. If both
elements are equal then, the comparator function should return 0
.
any
:
The maximum value in the list of
as
using the comparator function
f
.
import { compare, maximumBy } from 'fkit'
maximumBy(compare, [1, 2, 3]) // 3
maximumBy(compare, 'abc') // 'c'
Determines the smallest of two numbers.
Number
:
The smallest of the numbers
a
and
b
.
import { min } from 'fkit'
min(1, 2) // 1
Calculates the minimum value of a list.
This a special case of the minaximumBy
function where the values are
compared using natural ordering.
any
:
Returns the minimum value in the list of
as
.
import { minimum } from 'fkit'
minimum([1, 2, 3]) // 1
minimum('abc') // 'a'
Calculates the minimum value of a list using a comparator function.
The comparator function compares two elements, a
and b
. If a
is
greater than b
, then the comparator function should return 1
. If a
is
less than b
, then the comparator function should return -1
. If both
elements are equal, then the comparator function should return 0
.
any
:
The minimum value in the list of
as
using the comparator function
f
.
import { compare, minimumBy } from 'fkit'
minimumBy(compare, [1, 2, 3]) // 1
minimumBy(compare, 'abc') // 'a'
Returns the modulo of two numbers.
Number
:
The result of
b % a
.
import { mod } from 'fkit'
mod(3, 2) // 1
Returns the product of two numbers.
Number
:
The result of
b * a
.
import { mul } from 'fkit'
mul(2, 3) // 6
Returns the negation of a number.
(Number)
The number.
Number
:
The result.
import { negate } from 'fkit'
negate(1) // -1
Determines whether two values are not equal.
(any)
The first value.
(any)
The second value.
Boolean
:
true
if the value
a
is strictly not equal (
!==
) to
the value
b
, false otherwise.
import { neq } from 'fkit'
neq(1, 1) // false
neq(1, 2) // true
Returns the logical NOT of a value.
(any)
The value.
Boolean
:
true
if
a
is not truthy,
false
otherwise.
import { not } from 'fkit'
not(true) // false
not(false) // true
Removes duplicate elements from a list.
This is a special case of the nubBy
function where the elements are compared
using the strict equality ===
operator.
(Array | String)
:
A list with all duplicate elements removed from the list of
as
.
import { nub } from 'fkit'
nub([1, 2, 2, 3, 3, 3]) // [1, 2, 3]
nub('abbccc') // 'abc'
Removes duplicate elements from a list using a comparator function.
The comparator function compares two elements, a
and b
. If the elements
are both considered to equal, then the comparator function should return
true
. Otherwise it should return false
.
(Array | String)
:
A list with all duplicate elements removed from the
list of
bs
.
import { nubBy } from 'fkit'
nubBy((a, b) => a === b, [1, 2, 2, 3, 3, 3]) // [1, 2, 3]
nubBy((a, b) => a === b, 'abbccc') // 'abc'
Determines whether a number is odd.
(Number)
The number.
Boolean
:
true
if the number
a
is odd, false otherwise.
import { odd } from 'fkit'
odd(2) // true
odd(1) // false
Omits properties of an object.
Object
:
A copy of the object
o
without
the properties in the
list of
ks
.
import { omit } from 'fkit'
const person = { name: 'Jane', age: 20, city: 'Melbourne' }
omit(['name', 'age'], person) // { city: 'Melbourne' }
Returns the logical OR of two values.
(any)
The first value.
(any)
The second value.
Boolean
:
The result of
b || a
.
import { or } from 'fkit'
or(false, true) // true
or(0, 1) // true
or('', 'foo') // true
Creates a new ordered pair.
(any)
The first value.
(any)
The second value.
Array
:
An ordered pair with the values
a
and
b
.
import { pair } from 'fkit'
pair(1, 2) // [1, 2]
pair('a', 'b') // ['a', 'b']
Gets the key-value pairs of an object.
(Object)
The object.
Array
:
A list of key-value pairs for the properties of the object
o
.
import { pairs } from 'fkit'
const person = { name: 'Jane', age: 20, city: 'Melbourne' }
pairs(person) // [['name', 'Jane'], ['age', 20], ['city', 'Melbourne']]
Partitions a list using a predicate function.
Array
:
A list that contains the elements in the list of
as
split
into a pair of lists: the elements that satisfy the predicate function
p
and the elements that do not satisfy the predicate function
p
.
import { eq, gt, partition } from 'fkit'
partition(gt(1), [1, 2, 3]) // [[2, 3], [1]]
partition(eq('o'), 'foo') // ['oo', 'f']
Calculates the permutations of a list.
(Array | String)
:
A list that contains all the permutations of the
elements in the list of
as
.
import { permutations } from 'fkit'
permutations([1, 2, 3]) // [[1, 2, 3], [2, 1, 3], [3, 2, 1], [2, 3, 1], [3, 1, 2], [1, 3, 2]]
permutations('abc') // ['abc', 'bac', 'cba', 'bca', 'cab', 'acb']
Picks properties of an object.
Object
:
A copy of the object
o
with
the properties in the list
of
ks
.
import { pick } from 'fkit'
const person = { name: 'Jane', age: 20, city: 'Melbourne' }
pick(['name', 'age'], person) // { name: 'Jane', age: 20 }
Prepends a value to a list.
(Array | String)
:
A list that contains the value
a
prepended to the
list of
bs
.
import { prepend } from 'fkit'
prepend(1, [2, 3]) // [1, 2, 3]
prepend('f', 'oo') // 'foo'
Calculates the product of the elements in a list.
(Array)
The list.
Number
:
The product of the elements in the list of
as
.
import { product } from 'fkit'
product([1, 2, 3]) // 6
Generates a random float.
Number
:
A random float between
a
and
b
.
import { randomFloat } from 'fkit'
randomFloat(1, 3) // 2.3
Generates a random integer.
Number
:
A random integer between
a
and
b
.
import { randomInt } from 'fkit'
randomInt(1, 3) // 2
Creates a new array of numbers.
Array
:
An array of numbers of length
n
starting from
a
.
import { range } from 'fkit'
range(1, 3) // [1, 2, 3]
Removes the first occurance of an element from a list.
This is a special case of the removeBy
function where the elements are
compared using the strict equality ===
operator.
(Array | String)
:
A list with the first occurance of the element
a
removed from the list of
bs
.
import { remove } from 'fkit'
remove(2, [1, 2, 3]) // [1, 3]
remove('f', 'foo') // 'oo'
Removes the first occurance of an element from a list using a comparator function.
The comparator function f
compares two elements, a
and b
. If the
elements are both considered to equal, then the comparator function should
return true
. Otherwise it should return false
.
(Array | String)
:
A list with the first occurance of the element
a
removed from the list of
bs
.
import { removeBy } from 'fkit'
removeBy((a, b) => a === b, 2, [1, 2, 3]) // [1, 3]
removeBy((a, b) => a === b, 'f', 'foo') // 'oo'
Replaces a term in a string.
String
:
The result of replacing term
a
with the string
b
in
the string
s
.
import { replace } from 'fkit'
replace('r', 'z', 'bar') // baz
replace(/^hello/, 'goodbye', 'hello world!') // goodbye world!
Replicates a value.
(Number)
The number of copies.
(any)
The value to replicate.
(Array | String)
:
A list of length
n
with
a
the value of every
element.
import { replicate } from 'fkit'
replicate(3, 1) // [1, 1, 1]
replicate(3, 'a') // 'aaa'
Reverses the elements in a list.
(Array | String)
:
A list that contains the elements in the list of
as
in reverse order.
import { reverse } from 'fkit'
reverse([1, 2, 3]) // [3, 2, 1]
reverse('foo') // 'oof'
Samples random elements from a list.
(Array | String)
:
A list of
n
elements randomly sampled from the
list of
as
.
import { sample } from 'fkit'
sample(2, [1, 2, 3]) // [3, 1]
sample(2, 'abc') // 'ca'
Scans a list from left-to-right with a function.
(Function)
The folding function.
(any)
The starting value.
(Array | String)
:
A list that contains the elements in the list of
as
scanned left-to-right with the binary function
f
and starting value
s
.
import { scan } from 'fkit'
scan((a, b) => a.concat(b), [], [1, 2, 3]) // [[], [1], [1, 2], [1, 2, 3]]
scan((a, b) => a.concat(b), '', 'foo') // ['', 'f', 'fo', 'foo']
Scans a list from right-to-left with a function.
(Function)
The folding function.
(any)
The starting value.
(Array | String)
:
A list that contains the elements in the list of
as
scanned right-to-left with the binary function
f
and starting value
s
.
import { scanRight } from 'fkit'
scanRight((b, a) => a.concat(b), [], [1, 2, 3]) // [[3, 2, 1], [3, 2], [3], []]
scanRight((b, a) => a.concat(b), '', 'foo') // ['oof', 'oo', 'o', '']
Sets a property of an object.
Object
:
A copy of the object
o
with the property
k
set to the
value
v
.
import { set } from 'fkit'
const person = { name: 'Jane', age: 20, city: 'Melbourne' }
set('name', 'Steve', person) // { name: 'Steve', age: 20, city: 'Melbourne' }
Shuffles a list using the Fisher-Yates algorithm.
(Array | String)
:
A list that contains the elements in the list of
as
randomly shuffled.
import { shuffle } from 'fkit'
shuffle([1, 2, 3]) // [2, 3, 1]
shuffle('abc') // 'bca'
Sorts a list using natural ordering.
This a special case of the sortBy
function where the values are compared
using natural ordering.
(Array | String)
:
A list that contains the elements in the list of
as
sorted.
import { sort } from 'fkit'
sort([2, 3, 1]) // [1, 2, 3]
sort('bca') // 'abc'
Sorts a list using a comparator function.
The comparator function compares two elements, a
and b
. If a
is
greater than b
, then the comparator function should return 1
. If a
is
less than b
, then the comparator function should return -1
. If both
elements are equal then, the comparator function should return 0
.
(Array | String)
:
A list that contains the elements in the list of
as
sorted using the comparator function
c
.
import { compare, sortBy } from 'fkit'
sortBy(compare, [2, 3, 1]) // [1, 2, 3]
sortBy(compare, 'bca') // 'abc'
Splits a list using a predicate function.
Array
:
A list that contains the elements in the list of
as
split
into a pair of lists: a prefix of elements that satisfy the predicate
function
p
and the remainder of the list.
import { lt, neq, span } from 'fkit'
span(lt(3), [1, 2, 3]) // [[1, 2], [3]]
span(neq(o), 'foo') // ['f', 'oo']
Splits a list.
Array
:
A list that contains the elements in the list of
as
split
into a pair of lists: a prefix of length
n
and the remainder of the list.
import { splitAt } from 'fkit'
splitAt(1, [1, 2, 3]) // [[1], [2, 3]]
splitAt(1, 'foo') // ['f', 'oo']
Creates a new string.
(Number)
The length of the string.
String
:
A string of length
n
.
import { string } from 'fkit'
string(3) // ' '
Returns the difference of two numbers.
Number
:
The result of
b - a
.
import { sub } from 'fkit'
sub(1, 2) // 1
Calculates the subsequences of a list.
Array
:
A list that contains all the subsequences of the elements
in the list of
as
.
import { subsequences } from 'fkit'
subsequences([1, 2, 3]) // [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
subsequences('abc') // ['', 'a', 'b', 'ab', 'c', 'ac', 'bc', 'abc']
Calculates the sum of the elements in a list.
(Array)
The list.
Number
:
The sum of the elements in the list of
as
.
import { sum } from 'fkit'
sum([1, 2, 3]) // 6
Surrounds the list of cs
with the values a
and b
.
(Array | String)
:
A new list.
import { surround } from 'fkit'
surround(0, 4, [1, 2, 3]) // [0, 1, 2, 3, 4]
surround('(', ')', 'foo') // '(foo)'
Get the elements after the first element in a list.
(Array | String)
:
A list that contains the elements after the first
element in the list of
as
.
import { tail } from 'fkit'
tail([1, 2, 3]) // [2, 3]
tail('foo') // 'oo'
Gets all final segments of a list.
Array
:
A list that contains all final segments of the list of
as
.
import { tails } from 'fkit'
tails([1, 2, 3]) // [[1, 2, 3], [2, 3], [3], []]
tails('foo') // ['foo', 'oo', 'o', '']
Takes a number of elements from the start of a list.
(Array | String)
:
The result after taking
n
elements from the list
of
as
.
import { take } from 'fkit'
take(2, [1, 2, 3]) // [1, 2]
take(2, 'foo') // 'fo'
Gets the prefix of a list using a predicate function.
(Array | String)
:
A list that takes the elements from the list of
as
while the predicate function
p
is satisfied.
import { lt, neq, takeWhile } from 'fkit'
takeWhile(lt(3), [1, 2, 3]) // [1, 2]
takeWhile(neq(o), 'foo') // 'f'
Applies the function f
to the value a
and returns the value a
unchanged.
(Function)
The function to apply.
(any)
The value.
any
:
The value
a
unchanged.
import { tap } from 'fkit'
const f = a => console.log(a)
tap(f)(1) // 1
Converts a string to lowercase.
(String)
The string.
String
:
A new string.
import { toLower } from 'fkit'
toLower('FOO') // foo
Converts a string to uppercase.
(String)
The string.
String
:
A new string.
import { toUpper } from 'fkit'
toUpper('foo') // FOO
Creates a new tuple.
(...any)
The values.
Array
:
A tuple with the values from
as
.
import { tuple } from 'fkit'
tuple(1, 2, 3) // [1, 2, 3]
tuple('a', 'b', 'c') // ['a', 'b', 'c']
Converts a function of any arity to a unary function.
(Function)
The function.
Function
:
A function that wraps the function
f
to accept only
one argument.
import { unary } from 'fkit'
function f (a) { ... }
const g = unary(f)
g(1, 2, 3) // f(1)
Converts a binary function to a function on pairs.
Function
:
A function that wraps the binary function
f
to accept
a pair.
import { uncurry } from 'fkit'
const add = uncurry((a, b) => a + b)
add([1, 2]) // 3
Calculates the union of two lists.
This is a special case of the unionBy
function where the elements are
compared using the strict equality ===
operator.
Duplicates are removed from bs
, but if as
contains duplicates then so
will the result.
(Array | String)
:
A list that contains the union of elements in the
lists of
as
and
bs
.
import { union } from 'fkit'
union([1, 2, 3], [2, 3, 4]) // [1, 2, 3, 4]
union('hello', 'world') // 'hellowrd'
Calculates the union of two lists.
The comparator function f
compares two elements, a
and b
. If the
elements are both considered to equal, then the comparator function should
return true
. Otherwise it should return false
.
Duplicates are removed from bs
, but if as
contains duplicates then so
will the result.
(Function)
The comparator function.
(Array | String)
:
A list that contains the union of elements in the
lists of
as
and
bs
.
import { unionBy } from 'fkit'
unionBy((a, b) => a === b, [1, 2, 3], [2, 3, 4]) // [1, 2, 3, 4]
unionBy((a, b) => a === b, 'hello', 'world') // 'hellowrd'
Unzips a list of pairs into a pair of lists.
(Array)
The list.
Array
:
The list of pairs
as
unzipped into a pair of lists.
import { unzip } from 'fkit'
unzip([[1, 4], [2, 5], [3, 6]]) // [[1, 2, 3], [4, 5, 6]]
unzip([['f', 'b'], ['o', 'a'], ['o', 'r']]) // ['foo', 'bar']
Updates a property of an object with a function.
Object
:
A copy of the object
o
with the property
k
updated
with the function
f
.
import { inc, update } from 'fkit'
const person = { name: 'Jane', age: 20, city: 'Melbourne' }
update('age', inc, person) // { name: 'Jane', age: 21, city: 'Melbourne' }
Gets the values of an object.
(Object)
The object.
Array
:
A list of values for the properties of the object
o
.
import { values } from 'fkit'
const person = { name: 'Jane', age: 20, city: 'Melbourne' }
values(person) // ['Jane', 20, 'Melbourne']
Converts a function to a variadic function.
The last named parameter will be given an array of arguments.
(Function)
The function.
Function
:
A function that wraps the function
f
to accept any
number of arguments.
import { variadic } from 'fkit'
function f (head, tail) { ... }
variadic(f)(1, 2, 3) // f(1, [2, 3])
Determines whether all predicate functions in a list are satisfied they are applied to a value.
(Array)
The list of predicate functions.
(any)
The value.
Boolean
:
true
if all predicate functions in the list of
ps
are
satisfied when applied to the value
a
,
false
otherwise.
import { gt, whereAll } from 'fkit'
const ps = [gt(1), gt(2)]
whereAll(ps, 1) // false
whereAll(ps, 2) // false
whereAll(ps, 3) // true
Determines whether any predicate functions in a list are satisfied when they are applied to a value.
(Array)
The list of predicate functions.
(any)
The value.
Boolean
:
true
if any predicate function in the list of
ps
are
satisfied when applied to the value
a
,
false
otherwise.
import { gt, whereAny } from 'fkit'
const ps = [gt(1), gt(2)]
whereAny(ps, 1) // false
whereAny(ps, 2) // true
whereAny(ps, 3) // true
Zips two lists into a list of pairs.
This is a special case of the zipWith
function where the elements are
combined using the pair
function.
Array
:
The lists of
as
and
bs
zipped into a list of pairs.
import { zip } from 'fkit'
zip([1, 2, 3], [4, 5, 6]) // [[1, 4], [2, 5], [3, 6]]
zip('foo', 'bar') // [['f', 'b'], ['o', 'a'], ['o', 'r']]
Zips two lists with a function.
(Function)
The zipping function.
Array
:
The lists of
as
and
bs
zipped with the binary function
f
.
import { zipWith } from 'fkit'
zipWith((a, b) => a + b, [1, 2, 3], [4, 5, 6]) // [5, 7, 9]
zipWith((a, b) => a + b, 'foo', 'bar') // ['fb', 'oa', or']