FKit API

fkit.fn

Core Functions

This module defines basic operations on functions.

always(c)

The constant function.

Discussion

Returns a function that always returns the value c, regardless of the arguments.

Parameters

c

A value.

Return Value

A new function.

Example

F.always(1)(2, 3) // 1

apply(f, a) curried

Applies a function to a value.

Discussion

Returns the result of the function f applied to the value a.

Parameters

f

A function.

a

A value.

Return Value

The result of f(a).

Example

function sayHi (a) { return ['Hi', a, '!'].join(' ') }
F.apply(sayHi, 'Jane') // Hi Jane!

apply2(f, a, b) curried

Applies a function to two values.

Discussion

Returns the result of the function f applied to the values a and b.

Parameters

f

A function.

a

A value.

b

A value.

Return Value

The result of f(a, b).

Example

function sayHi (a, b) { return ['Hi', a, b, '!'].join(' ') }
F.apply2(sayHi, 'Jane', 'Appleseed') // Hi Jane Appleseed!

apply3(f, a, b, c) curried

Applies a function to three values.

Discussion

Returns the result of the function f applied to the values a, b, and c.

Parameters

f

A function.

a

A value.

b

A value.

c

A value.

Return Value

The result of f(a, b, c).

Example

function sayHi (a, b, c) { return ['Hi', a, b, c, '!'].join(' ') }
F.apply3(sayHi, 'Ms', 'Jane', 'Appleseed') // Hi Ms Jane Appleseed!

applyRight(a, f) curried

Applies a function to a value.

Discussion

Returns the result of the function f applied to the value a.

This is similar to apply, however the order of the arguments is flipped.

Parameters

a

A value.

f

A function.

Return Value

The result of f(a).

Example

function sayHi (a) { return ['Hi', a, '!'].join(' ') }
F.applyRight('Jane', sayHi) // Hi Jane!

binary(f)

Converts a function to a binary function.

Discussion

Returns a function that wraps the function f to accept only two arguments. Any other arguments will be ignored.

Parameters

f

A function.

Return Value

A new function.

Example

function f () { ... }
const g = F.binary(f)
g(1, 2, 3) // f(1, 2)

compare(a, b) curried

Compares two values using natural ordering.

Discussion

Returns the ordering of the two values a and b.

Parameters

a

A value.

b

A value.

Return Value

A number.

Example

F.compare(1, 2) // -1
F.compare(2, 1) // 1
F.compare(2, 2) // 0

compose(fs)

Composes a list of functions.

Discussion

Returns a function that is the composition of the list of functions fs.

Parameters

fs

A list of functions.

Return Value

A new function.

Example

F.compose(f, g, h)(a) // f(g(h(a)))

curry(f)

Converts a function to a curried function.

Discussion

Returns a function that allows partial application of the arguments to the function f.

Parameters

f

A function.

Return Value

A new function.

Example

var add = F.curry((a, b) => a + b)
add(1)(2) // 3

equal(a, b) curried

The strict equality operator.

Discussion

Returns true if the value a is strictly equal (===) to the value b, false otherwise.

Parameters

a

A value.

b

A value.

Return Value

A boolean value.

Example

F.equal(1, 1) // true
F.equal(1, 2) // false

flip(f, a, b)

Flips the order of the arguments to a function.

Discussion

Returns the result of applying the function f to the values b and a.

Parameters

f

A function.

a

A value.

b

A value.

Return Value

A new function.

Example

function f (a, b) { ... }
var g = F.flip(f)
g(1, 2) // f(2, 1)

id(a)

The identity function.

Discussion

Returns the value a unchanged.

Parameters

a

A value.

Return Value

The value a.

Example

F.id(1) // 1

notEqual(a, b) curried

The strict inequality operator. F.notEqual(1, 1) // false F.notEqual(1, 2) // true

Discussion

Returns true if the value a is strictly not equal (!==) to the value b, false otherwise.

Parameters

a

A value.

b

A value.

Return Value

A boolean value.

tap(f, a) curried

Applies a side-effecting function to a value.

Discussion

Applies the function f to the value a and returns the value a unchanged.

Parameters

f

A function.

a

A value.

Return Value

The value a.

Example

function f (a) { console.log(a) }
F.tap(f)(1) // 1

unary(f)

Converts a function to a unary function.

Discussion

Returns a function that wraps the function f to accept only one argument. Any other arguments will be ignored.

Parameters

f

A function.

Return Value

A new function.

Example

function f () { ... }
const g = F.unary(f)
g(1, 2, 3) // f(1)

uncurry(f)

Converts a binary function to a function on pairs.

Discussion

Returns a function that wraps the binary function f to accept a pair.

Parameters

f

A function.

Return Value

A new function.

Example

var add = F.uncurry((a, b) => a + b)
add([1, 2]) // 3

variadic(f)

Converts a function to a variadic function.

Discussion

Returns a function that wraps the function f to accept any number of arguments.

The last named parameter will be given an array of arguments.

Parameters

f

A function.

Return Value

A new function.

Example

function f (head, tail) { ... }
F.variadic(f)(1, 2, 3) // f(1, [2, 3])

fkit.list

Working with Lists

FKit treats both arrays and strings as lists: an array is a list of elements, and a string is a list of characters.

Representing strings as lists may be a novel concept for some JavaScript users, but it is quite common in other languages. This seemingly simple abstractions yields a great deal of power: it allows you to apply the same list combinators to both arrays and strings.

all(p, as) curried

Determines if all elements in a list satisfy a predicate function.

Discussion

Returns true if all elements in the list of as satisfy the predicate function p, false otherwise.

Parameters

p

A predicate function.

as

A list.

Return Value

A boolean value.

Example

F.all(F.gt(1), [1, 2, 3]) // false
F.all(F.gt(1), [2, 3]) // true
F.all(F.gt(1), [3]) // true

F.all(F.eq('o'), 'foo') // false
F.all(F.eq('o'), 'oo') // true
F.all(F.eq('o'), 'o') // true

any(p, as) curried

Determines if any elements in a list satisfy a predicate function.

Discussion

Returns true if any elements in the list of as satisfy the predicate function p, false otherwise.

Parameters

p

A predicate function.

as

A list.

Return Value

A boolean value.

Example

F.any(F.gt(1), [1, 2, 3]) // true
F.any(F.gt(1), [1, 2]) // true
F.any(F.gt(1), [1]) // false

F.any(F.eq('o'), 'foo') // true
F.any(F.eq('o'), 'fo') // true
F.any(F.eq('o'), 'f') // false

append(a, bs) curried

Appends a value to a list.

Discussion

Returns a list that contains the value a appended to the list of bs.

Parameters

a

A value.

bs

A list.

Return Value

A new list.

Example

F.append(3, [1, 2]) // [1, 2, 3]
F.append('o', 'fo') // 'foo'

array(n)

Creates a new array.

Discussion

Returns an array of length n.

Parameters

n

A number.

Return Value

A new array.

Example

F.array(3) // [undefined, undefined, undefined]

cartesian(as, bs) curried

Calculates the cartesian product of two lists.

Discussion

Returns a list that contains all the ordered pairs [a, b] in the lists of as and bs.

Parameters

as

A list.

bs

A list.

Return Value

A new list.

Example

F.cartesian([1, 2], [3, 4]) // [[1, 3], [1, 4], [2, 3], [2, 4]]
F.cartesian('ab', 'cd') // [['a', 'c'], ['a', 'd'], ['b', 'c'], ['b', 'd']]

concat(as)

Concatenates lists.

Discussion

Returns a list that contains the concatenated elements in the list of as.

Parameters

as

A list.

Return Value

A new list.

Example

F.concat([1], [2, 3], [4, 5, 6]) // [1, 2, 3, 4, 5, 6]
F.concat('f', 'oo', 'bar') // 'foobar'

concatMap(f, as) curried

Maps a function over a list and concatenates the results.

Discussion

Returns a list that contains the elements in the list of as mapped with the function f concatenated together.

Parameters

f

A function.

as

A list.

Return Value

A new list.

Example

F.concatMap(a => [a, 0], [1, 2, 3]) // [1, 0, 2, 0, 3, 0]

F.concatMap(a => [a, '-'], 'foo') // 'f-o-o-'

difference(as, bs) curried

Calculates the difference of two lists.

Discussion

Returns a list that contains the difference of the elements in the lists of as and bs.

It is a special case of the differenceBy function where the elements are compared using the strict equality === operator.

Parameters

as

A list.

bs

A list.

Return Value

A new list.

Example

F.difference([1, 2, 3], [2, 3, 4]) // [1]
F.difference('hello', 'world') // 'hel'

differenceBy(f, as, bs) curried

Calculates the difference of two lists.

Discussion

Returns a list that contains the difference of the elements in the lists of as and bs. The elements are compared using the comparator function f.

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.

Parameters

f

A comparator function.

as

A list.

bs

A list.

Return Value

A new list.

Example

F.differenceBy((a, b) => a === b, [1, 2, 3], [2, 3, 4]) // [1]
F.differenceBy((a, b) => a === b, 'hello', 'world') // 'hel'

drop(n, as) curried

Gets the suffix of a list.

Discussion

Returns the suffix after dropping n elements from the list of as.

Parameters

n

A number.

as

A list.

Return Value

A new list.

Example

F.drop(2, [1, 2, 3]) // [3]
F.drop(2, 'foo') // 'o'

dropWhile(p, as) curried

Gets the suffix of a list using a predicate function.

Discussion

Returns the suffix after dropping elements from the list of as while the predicate function p is satisfied.

Parameters

p

A predicate function.

as

A list.

Return Value

A new list.

Example

F.dropWhile(F.lt(3), [1, 2, 3]) // [3]
F.dropWhile(F.neq(o), 'foo') // 'oo'

elem(a, as) curried

Determines if a value is present in a list.

Discussion

Returns true if the list of as contains the element a, false otherwise.

Parameters

a

A value.

as

A list.

Return Value

A boolean value.

Example

F.elem(0, [1, 2, 3]) // false
F.elem(1, [1, 2, 3]) // true

F.elem('a', 'foo') // false
F.elem('o', 'foo') // true

elemIndex(a, as) curried

Gets the index of the first occurance of an element in a list.

Discussion

Returns the index of the first occurance of the element a in the list of as.

Parameters

a

A value.

as

A list.

Return Value

A number or undefined if no value was found.

Example

F.elemIndex(0, [1, 2, 3]) // undefined
F.elemIndex(1, [1, 2, 3]) // 0

F.elemIndex('a', 'foo') // undefined
F.elemIndex('o', 'foo') // 1

elemIndices(a, as) curried

Gets the indices of all occurances of an element in a list.

Discussion

Returns the indices of all occurances of the element a in the list of as.

Parameters

a

A value.

as

A list.

Return Value

A number or undefined if no value was found.

Example

F.elemIndices(0, [1, 2, 3]) // []
F.elemIndices(1, [1, 2, 3]) // [0]

F.elemIndices('a', 'foo') // []
F.elemIndices('o', 'foo') // [1, 2]

empty(as)

Determines if a list is empty.

Discussion

Returns true if the list of as is empty, false otherwise.

Parameters

as

A list.

Return Value

A boolean value.

Example

F.empty([]) // true
F.empty([1, 2, 3]) // false

F.empty('') // true
F.empty('foo') // false

filter(p, as) curried

Filters a list using a predicate function.

Discussion

Returns a list that contains the elements in the list of as that satisfy the predicate function p.

Parameters

p

A predicate function.

as

A list.

Return Value

A new list.

Example

F.filter(F.gt(1), [1, 2, 3]) // [2, 3]
F.filter(F.eq('o'), 'foo') // 'oo'

find(p, as) curried

Finds an element in a list that satisfies a predicate function.

Discussion

Returns an element in the list of as that satisfies the predicate function p.

Parameters

p

A predicate function.

as

A list.

Return Value

A value or undefined if no value was found.

Example

F.find(F.gt(1), []) // undefined
F.find(F.gt(1), [1, 2, 3]) // 2

F.find(F.eq('o'), '') // undefined
F.find(F.eq('o'), 'foo') // 'o'

findIndex(p, as) curried

Finds the index of the first occurance of an element in a list that satisfies a predicate function.

Discussion

Returns the index of the first occurance of an element in the list of as that satisfies the predicate function p.

Parameters

p

A predicate function.

as

A list.

Return Value

A number or undefined if no value was found.

Example

F.findIndex(F.gt(1), []) // undefined
F.findIndex(F.gt(1), [1, 2, 3]) // 1

F.findIndex(F.eq('o'), '') // undefined
F.findIndex(F.eq('o'), 'foo') // 1

findIndices(p, as) curried

Finds the indices of all occurances of the elements in a list that satisfy a predicate function.

Discussion

Returns the indices of the elements in the list of as that satisfy the predicate function p.

Parameters

p

A predicate function.

as

A list.

Return Value

A number or undefined if no value was found.

Example

F.findIndices(F.gt(1), []) // []
F.findIndices(F.gt(1), [1, 2, 3]) // [1, 2]

F.findIndices(F.eq('o'), '') // []
F.findIndices(F.eq('o'), 'foo') // [1, 2]

fold(f, s, as) curried

Folds a list from left to right with a function.

Discussion

Returns a list that contains the elements in the list of as folded left-to-right with the binary function f and starting value s.

Parameters

f

A binary function.

s

A starting value.

as

A list.

Return Value

A value.

Example

F.fold(F.flip(F.prepend), [], [1, 2, 3]) // [3, 2, 1]
F.fold(F.flip(F.prepend), '', 'foo') // 'oof'

foldRight(f, s, as) curried

Folds a list from right to left with a function.

Discussion

Returns a list that contains the elements in the list of as folded right-to-left with the binary function f and starting value s.

Parameters

f

A binary function.

s

A starting value.

as

A list.

Return Value

A value.

Example

F.foldRight(F.append, [], [1, 2, 3]) // [3, 2, 1]
F.foldRight(F.append, '', 'foo') // 'oof'

group(as)

Groups the elements in a list.

Discussion

Returns a list that contains the elements in the list of as grouped into sublists of equal elements.

It is a special case of the groupBy function where the elements are compared using the strict equality === operator.

Parameters

as

A list.

Return Value

A new list.

Example

F.group([1, 2, 2, 3, 3, 3]) // [[1], [2, 2], [3, 3, 3]]
F.group('Mississippi') // ['M', 'i', 'ss', 'i', 'ss', 'i', 'pp', 'i']

groupBy(c, as) curried

Groups the elements in a list using a comparator function.

Discussion

Returns a list that contains the elements in the list of as grouped into sublists that satisfy the comparator function c.

The comparator function compares two elements, a and b. If the elements are both considered to be in the same group, then the comparator function should return true. Otherwise it should return false.

Parameters

c

A comparator function.

as

A list.

Return Value

A new list.

Example

F.groupBy((a, b) => a === b, [1, 2, 2, 3, 3, 3]) // [[1], [2, 2], [3, 3, 3]]

Gets the first element in a list.

Discussion

Returns the first element in the list of as.

Parameters

as

A list.

Return Value

A value or undefined if the list is empty.

Example

F.head([1, 2, 3]) // 1
F.head('foo') // 'f'

init(as)

Gets the elements before the last element in a list.

Discussion

Returns a list that contains the elements before the last element in the list of as.

Parameters

as

A list.

Return Value

A new list.

Example

F.init([1, 2, 3]) // [1, 2]
F.init('foo') // 'fo'

inits(as)

Gets all initial segments of a list.

Discussion

Returns a list that contains all initial segments of the list of as.

Parameters

as

A list.

Return Value

A new list.

Example

F.inits([1, 2, 3]) // [[], [1], [1, 2], [1, 2, 3]]
F.inits('foo') // ['', 'f', 'fo', 'foo']

intersect(as, bs) curried

Calculates the intersection of two lists.

Discussion

Returns a list that contains the intersection of the elments in the lists of as and bs.

It 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.

Parameters

as

A list.

bs

A list.

Return Value

A new list.

Example

F.intersect([1, 2, 3], [2, 3, 4]) // [2, 3]
F.intersect('hello', 'world') // 'ol'

intersectBy(f, as, bs) curried

Calculates the intersection of two lists.

Discussion

Returns a list that contains the intersection of the elments in the lists of as and bs. The elements are compared using the comparator function f.

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.

Duplicates are removed from bs, but if as contains duplicates then so will the result.

Parameters

f

A comparator function.

as

A list.

bs

A list.

Return Value

A new list.

Example

F.intersectBy((a, b) => a === b, [1, 2, 3], [2, 3, 4]) // [2, 3]
F.intersectBy((a, b) => a === b, 'hello', 'world') // 'ol'

intersperse(s, as) curried

Intersperses the elements of a list with separator.

Discussion

Returns a list that contains the elements in the list of as interspersed with the separator s.

Parameters

s

A separator.

as

A list.

Return Value

A new list.

Example

F.intersperse(4, [1, 2, 3]) // [1, 4, 2, 4, 3]
F.intersperse('-', 'foo') // 'f-o-o'

isInfixOf(as, bs) curried

Determines if a list is contained within another list.

Discussion

Returns true if the list of as is contained within the list of bs, false otherwise.

Parameters

as

A list.

bs

A list.

Return Value

A boolean value.

Example

F.isInfixOf([], [1, 2, 3]) // true
F.isInfixOf([2, 3], [1, 2, 3]) // true
F.isInfixOf([3, 2], [1, 2, 3]) // false

F.isInfixOf('', 'foo') // true
F.isInfixOf('oo', 'foo') // true
F.isInfixOf('of', 'foo') // false

isPrefixOf(as, bs) curried

Determines if a list is a prefix of another list.

Discussion

Returns true if the list of as is a prefix of the list of bs, false otherwise.

Parameters

as

A list.

bs

A list.

Return Value

A boolean value.

Example

F.isPrefixOf([], [1, 2, 3]) // true
F.isPrefixOf([1, 2], [1, 2, 3]) // true
F.isPrefixOf([2, 3], [1, 2, 3]) // false

F.isPrefixOf('', 'foo') // true
F.isPrefixOf('fo', 'foo') // true
F.isPrefixOf('oo', 'foo') // false

isSuffixOf(as, bs) curried

Determines if a list is a suffix of another list.

Discussion

Returns true if the list of as is a suffix of the list of bs, false otherwise.

Parameters

as

A list.

bs

A list.

Return Value

A boolean value.

Example

F.isSuffixOf([], [1, 2, 3]) // true
F.isSuffixOf([1, 2], [1, 2, 3]) // false
F.isSuffixOf([2, 3], [1, 2, 3]) // true

F.isSuffixOf('', 'foo') // true
F.isSuffixOf('fo', 'foo') // false
F.isSuffixOf('oo', 'foo') // true

last(as)

Gets the last element in a list.

Discussion

Returns the last element in the list of as.

Parameters

as

A list.

Return Value

A value or undefined if the list is empty.

Example

F.last([1, 2, 3]) // 3
F.last('foo') // 'o'

length(as)

Gets the length of a list.

Discussion

Returns the number of elements in the list of as.

Parameters

as

A list.

Return Value

A number.

Example

F.length([1, 2, 3]) // 3
F.length('foo') // 3

map(f, as) curried

Maps a function over a list.

Discussion

Returns a list that contains the elements in the list of as mapped with the function f.

Parameters

f

A function.

as

A list.

Return Value

A new list.

Example

F.map(F.inc, [1, 2, 3]) // [2, 3, 4]
F.map(F.toUpper, 'foo') // ['F', 'O', 'O']

maximumBy(f, as) curried

Calculates the maximum value of a list using a comparator function.

Discussion

Returns the maximum value in the list of as using the comparator function f.

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.

Parameters

f

A comparator function.

as

A list.

Return Value

A value.

Example

F.maximumBy(F.compare, [1, 2, 3]) // 3

minimumBy(f, as) curried

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`.

Discussion

Returns the minimum value in the list of as using the comparator function f.

Parameters

f

A comparator function.

as

A list.

Return Value

A value.

Example

F.minimumBy(F.compare, [1, 2, 3]) // 1

nubBy(f, as) curried

Removes duplicate elements from a list using a comparator function.

Discussion

Returns a list with all duplicate elements removed from the list of bs. The elements are compared using the comparator function f.

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.

Parameters

f

A comparator function.

as

A list.

Return Value

A new list.

Example

F.nubBy((a, b) => a === b, [1, 2, 2, 3, 3, 3]) // [1, 2, 3]

pair(a, b) curried

Creates a new ordered pair.

Discussion

Returns an ordered pair with the values a and b.

Parameters

a

A value.

b

A value.

Return Value

A new pair.

Example

F.pair(1, 2) // [1, 2]
F.pair('a', 'b') // ['a', 'b']

partition(p, as) curried

Partitions a list using a predicate function.

Discussion

Returns 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.

Parameters

p

A predicate function.

as

A list.

Return Value

A pair of lists.

Example

F.partition(F.gt(1), [1, 2, 3]) // [[2, 3], [1]]
F.partition(F.eq('o'), 'foo') // ['oo', 'f']

permutations(as)

Calculates the permutations of a list.

Discussion

Returns a list that contains all the permutations of the elements in the list of as.

Parameters

as

A list.

Return Value

A new list.

Example

F.permutations([1, 2, 3]) // [[1, 2, 3], [2, 1, 3], [3, 2, 1], [2, 3, 1], [3, 1, 2], [1, 3, 2]]
F.permutations('abc') // ['abc', 'bac', 'cba', 'bca', 'cab', 'acb']

prepend(a, bs) curried

Prepends a value to a list.

Discussion

Returns a list that contains the value a prepended to the list of bs.

Parameters

a

A value.

bs

A list.

Return Value

A new list.

Example

F.prepend(1, [2, 3]) // [1, 2, 3]
F.prepend('f', 'oo') // 'foo'

product(as)

Calculates the product of the elements in a list.

Discussion

Returns the product of the elements in the list of as.

Parameters

as

A list.

Return Value

A number.

Example

F.product([1, 2, 3]) // 6

range(a, n) curried

Creates a new array of numbers.

Discussion

Returns an array of numbers of length n starting from a.

Parameters

a

A number.

n

A number.

Return Value

A new array.

Example

F.range(1, 3) // [1, 2, 3]

remove(a, bs) curried

Removes the first occurance of an element from a list.

Discussion

Returns a list with the first occurance of the element a removed from the list of bs.

It is a special case of the removeBy function where the elements are compared using the strict equality === operator.

Parameters

a

A value.

bs

A list.

Return Value

A new list.

Example

F.remove(2, [1, 2, 3]) // [1, 3]
F.remove('f', 'foo') // 'oo'

removeBy(f, a, bs) curried

Removes the first occurance of an element from a list using a comparator function.

Discussion

Returns a list with the first occurance of the element a removed from the list of bs. The elements are compared using the comparator function f.

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.

Parameters

f

A comparator function.

a

A value.

bs

A list.

Return Value

A new list.

Example

F.removeBy((a, b) => a === b, 2, [1, 2, 3]) // [1, 3]

replicate(n, a) curried

Replicates a value.

Discussion

Returns a list of length n with a the value of every element.

Parameters

n

A number.

a

A value.

Return Value

A new list.

Example

F.replicate(3, 1) // [1, 1, 1]
F.replicate(3, 'a') // 'aaa'

sample(n, as) curried

Samples random elements from a list.

Discussion

Returns a list of n elements randomly sampled from the list of as.

Parameters

n

A number.

as

A list.

Return Value

A new list.

Example

F.sample(2, [1, 2, 3]) // [3, 1]
F.sample(2, 'abc') // 'ca'

scan(f, s, as) curried

Scans a list from left to right with a function.

Discussion

Returns a list that contains the elements in the list of as scanned left-to-right with the binary function f and starting value s.

Parameters

f

A binary function.

s

A starting value.

as

A list.

Return Value

A new list.

Example

F.fold(F.flip(F.prepend), [],  [1, 2, 3]) // [[], [1], [2, 1], [3, 2, 1]]
F.fold(F.flip(F.prepend), '',  'foo') // ['', 'f', 'of', 'oof']

scanRight(f, s, as) curried

Scans a list from right to left with a function.

Discussion

Returns a list that contains the elements in the list of as scanned right-to-left with the binary function f and starting value s.

Parameters

f

A binary function.

s

A starting value.

as

A list.

Return Value

A new list.

Example

F.foldRight(F.append, [],  [1, 2, 3]) // [[3, 2, 1], [3, 2], [3], []]
F.foldRight(F.append, '',  'foo') // ['oof', 'oo', 'o', '']

shuffle(as)

Shuffles a list.

Discussion

Returns a list that contains the elements in the list of as randomly shuffled using the Fisher-Yates algorithm.

Parameters

as

A list.

Return Value

A new list.

Example

F.shuffle([1, 2, 3]) // [2, 3, 1]
F.shuffle('abc') // 'bca'

sort(a)

Sorts a list using natural ordering.

Discussion

Returns a list that contains the elements in the list of as sorted.

Parameters

a

A list.

Return Value

A new list.

Example

F.sort([2, 3, 1]) // [1, 2, 3]
F.sort('bca') // 'abc'

sortBy(c, as) curried

Sorts a list using a comparator function.

Discussion

Returns a list that contains the elements in the list of as sorted using the comparator function c.

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.

Parameters

c

A comparator function.

as

A list.

Return Value

A new list.

Example

F.sortBy((a, b) =>
  a > b ? 1 : (a < b ? -1 : 0)
, [2, 3, 1]) // [1, 2, 3]

span(p, as) curried

Splits a list using a predicate function.

Discussion

Returns 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.

Parameters

p

A predicate function.

as

A list.

Return Value

A pair of lists.

Example

F.span(F.lt(3), [1, 2, 3]) // [[1, 2], [3]]
F.span(F.neq(o), 'foo') // ['f', 'oo']

splitAt(n, as) curried

Splits a list.

Discussion

Returns 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.

Parameters

n

A number.

as

A list.

Return Value

A pair of lists.

Example

F.splitAt(1, [1, 2, 3]) // [[1], [2, 3]]
F.splitAt(1, 'foo') // ['f', 'oo']

string(n)

Creates a new string.

Discussion

Returns an string of length n.

Parameters

n

A number.

Return Value

A new string.

Example

F.string(3) // '   '

subsequences(as)

Calculates the subsequences of a list.

Discussion

Returns a list that contains all the subsequences of the elements in the list of as.

Parameters

as

A list.

Return Value

A new list.

Example

F.subsequences([1, 2, 3]) // [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
F.subsequences('abc') // ['', 'a', 'b', 'ab', 'c', 'ac', 'bc', 'abc']

sum(as)

Calculates the sum of the elements in a list.

Discussion

Returns the sum of the elements in the list of as.

Parameters

as

A list.

Return Value

A number.

Example

F.sum([1, 2, 3]) // 6

surround(a, b, cs) curried

Discussion

Surrounds the list of cs with the values a and b.

Parameters

a

A value.

b

A value.

cs

A list.

Return Value

A new list.

Example

F.surround(0, 4, [1, 2, 3]) // [0, 1, 2, 3, 4]
F.surround('(', ')', 'foo') // '(foo)'

tail(as)

Get the elements after the first element in a list.

Discussion

Returns a list that contains the elements after the first element in the list of as.

Parameters

as

A list.

Return Value

A new list.

Example

F.tail([1, 2, 3]) // [2, 3]
F.tail('foo') // 'oo'

tails(as)

Gets all final segments of a list.

Discussion

Returns a list that contains all final segments of the list of as.

Parameters

as

A list.

Return Value

A new list.

Example

F.tails([1, 2, 3]) // [[1, 2, 3], [2, 3], [3], []]
F.tails('foo') // ['foo', 'oo', 'o', '']

take(n, as) curried

Gets the prefix of a list.

Discussion

Returns the prefix of n elements from the list of as.

Parameters

n

A number.

as

A list.

Return Value

A new list.

Example

F.take(2, [1, 2, 3]) // [1, 2]
F.take(2, 'foo') // 'fo'

takeWhile(p, as) curried

Gets the prefix of a list using a predicate function.

Discussion

Returns the prefix of elements from the list of as while the predicate function p is satisfied.

Parameters

p

A predicate function.

as

A list.

Return Value

A new list.

Example

F.takeWhile(F.lt(3), [1, 2, 3]) // [1, 2]
F.takeWhile(F.neq(o), 'foo') // 'f'

union(as, bs) curried

Calculates the union of two lists.

Discussion

Returns a list that contains the union of elements in the lists of as and bs.

It 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.

Parameters

as

A list.

bs

A list.

Return Value

A new list.

Example

F.union([1, 2, 3], [2, 3, 4]) // [1, 2, 3, 4]
F.union('hello', 'world') // 'hellowrd'

unionBy(f, as, bs) curried

Calculates the union of two lists.

Discussion

Returns a list that contains the union of elements in the lists of as and bs. The elements are compared using the comparator function f.

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.

Duplicates are removed from bs, but if as contains duplicates then so will the result.

Parameters

f

A comparator function.

as

A list.

bs

A list.

Return Value

A new list.

Example

F.unionBy((a, b) => a === b, [1, 2, 3], [2, 3, 4]) // [1, 2, 3, 4]
F.unionBy((a, b) => a === b, 'hello', 'world') // 'hellowrd'

unzip(as)

Unzips a list of pairs into a pair of lists.

Discussion

Returns the list of pairs as unzipped into a pair of lists.

Parameters

as

A list.

Return Value

A new list.

Example

F.unzip([[1, 4], [2, 5], [3, 6]]) // [[1, 2, 3], [4, 5, 6]]
F.unzip([['f', 'b'], ['o', 'a'], ['o', 'r']]) // ['foo', 'bar']

zip(as, bs) curried

Zips two lists into list of pairs.

Discussion

Returns the lists of as and bs zipped into a list of pairs.

It is a special case of the zipWith function where the elements are combined using the F.pair function.

Parameters

as

A list.

bs

A list.

Return Value

A new list.

Example

F.zip([1, 2, 3], [4, 5, 6]) // [[1, 4], [2, 5], [3, 6]]
F.zip('foo', 'bar') // [['f', 'b'], ['o', 'a'], ['o', 'r']]

zipWith(f, as, bs) curried

Zips two lists with a function.

Discussion

Returns the lists of as and bs zipped with the binary function f.

Parameters

f

A binary function.

as

A list.

bs

A list.

Return Value

A new list.

fkit.logic

Logical Functions and Combinators

This module defines logic functions.

and(a, b) curried

The logical AND operator.

Discussion

Returns true if both a and b are truthy, false otherwise.

Parameters

a

A boolean value.

b

A boolean value.

Return Value

A boolean value.

Example

F.and(true, true) // true
F.and(0, 1) // false
F.and('', 'foo') // false

branch(p, f, g, a) curried

Branches execution based on a predicate function.

Discussion

If p(a) is true then f is applied to a, otherwise g is applied to a.

Parameters

p

A predicate function.

f

A function.

g

A function.

a

A value.

Return Value

A value.

Example

function big (a) { return a + ' is a big number' }
function small (a) { return a + ' is a small number' }
var f = F.branch(F.gt(10), big, small)
f(10) // small number
f(11) // big number

not(a)

The logical NOT operator.

Discussion

Returns true if a is not truthy, false otherwise.

Parameters

a

A boolean.

Return Value

A boolean value.

Example

F.not(true) // false
F.not(false) // true

or(a, b) curried

The logical OR operator.

Discussion

Returns true if either a or b are truthy, false otherwise.

Parameters

a

A boolean value.

b

A boolean value.

Return Value

A boolean value.

Example

F.or(false, true) // true
F.or(0, 1) // true
F.or('', 'foo') // true

whereAll(ps, a) curried

Discussion

Applies the list of predicate functions ps to the value a and returns their conjunction.

Parameters

ps

A list of predicate functions.

a

A value.

Return Value

A boolean value.

Example

var ps = [F.gt(1), F.gt(2)]
F.whereAll(ps, 1) // false
F.whereAll(ps, 2) // false
F.whereAll(ps, 3) // true

whereAny(ps, a) curried

Discussion

Applies the list of predicate functions ps to the value a and returns their disjunction.

Parameters

ps

A list of predicate functions.

a

A value.

Return Value

A boolean value.

Example

var ps = [F.gt(1), F.gt(2)]
F.whereAny(ps, 1) // false
F.whereAny(ps, 2) // true
F.whereAny(ps, 3) // true

fkit.math

Yay, Numbers!

This module defines math functions.

add(a, b) curried

The addition operator.

Discussion

Returns the result of b + a.

Parameters

a

A number.

b

A number.

Return Value

A number.

between(a, b, n) curried

The between operator.

Discussion

Retruns true if the number n is between the numbers a and b (i.e. a <= n <= b), false otherwise.

Parameters

a

A number.

b

A number.

n

A number.

Return Value

A number.

clamp(a, b, n) curried

Clamps a number.

Discussion

Clamps the number n between the numbers a and b (i.e. a <= n <= b).

Parameters

a

A number.

b

A number.

n

A number.

Return Value

A number.

dec(a)

Decrements a number.

Discussion

Returns the result of a - 1.

Parameters

a

A number.

Return Value

A number.

div(a, b) curried

The division operator.

Discussion

Returns the result of b / a.

Parameters

a

A number.

b

A number.

Return Value

A number.

eq(a, b) curried

The strict equality operator.

Discussion

Returns true if the value a is strictly equal (===) to the value b, false otherwise.

Parameters

a

A value.

b

A value.

Return Value

A boolean value.

even(a)

Tests whether a value is even.

Discussion

Returns true if the value a is even, false otherwise.

Parameters

a

A number.

Return Value

A boolean value.

gt(a, b) curried

The greater than operator.

Discussion

Returns true if the value a is greater than the value b, false otherwise.

Parameters

a

A number.

b

A number.

Return Value

A boolean value.

gte(a, b) curried

The greater than or equal operator.

Discussion

Returns true if the value a is greater than or equal to the value b false otherwise.

Parameters

a

A number.

b

A number.

Return Value

A boolean value.

inc(a)

Increments a number.

Discussion

Returns the result of a + 1.

Parameters

a

A number.

Return Value

A number.

lt(a, b) curried

The less than operator.

Discussion

Returns true if the value a is less than the value b, false otherwise.

Parameters

a

A number.

b

A number.

Return Value

A boolean value.

lte(a, b) curried

The less than or equal operator.

Discussion

Returns true if the value a is less than or equal to the value b, false otherwise.

Parameters

a

A number.

b

A number.

Return Value

A boolean value.

max(a, b) curried

Determines the largest of two numbers.

Discussion

Returns the largest of the numbers a and b.

Parameters

a

A number.

b

A number.

Return Value

A number.

min(a, b) curried

Determines the smallest of two numbers.

Discussion

Returns the smallest of the numbers a and b.

Parameters

a

A number.

b

A number.

Return Value

A number.

mod(a, b) curried

The modulo operator.

Discussion

Returns the result of b % a.

Parameters

a

A number.

b

A number.

Return Value

A number.

mul(a, b) curried

The multiplication operator.

Discussion

Returns the result of b * a.

Parameters

a

A number.

b

A number.

Return Value

A number.

negate(a)

The negation operator.

Discussion

Returns the negation of the number a.

Parameters

a

A number.

Return Value

A number.

neq(a, b) curried

The strict inequality operator.

Discussion

Returns true if the value a is strictly not equal (!==) to the value b, false otherwise.

Parameters

a

A value.

b

A value.

Return Value

A boolean value.

odd(a)

Tests whether a value is odd.

Discussion

Returns true if the value a is odd, false otherwise.

Parameters

a

A number.

Return Value

A boolean value.

randomFloat(a, b) curried

Generates a random float.

Discussion

Returns a random float between a and b.

Parameters

a

A number.

b

A number.

Return Value

A number.

randomInt(a, b) curried

Generates a random integer.

Discussion

Returns a random integer between a and b.

Parameters

a

A number.

b

A number.

Return Value

A number.

sub(a, b) curried

The subtraction operator.

Discussion

Returns the result of b - a.

Parameters

a

A number.

b

A number.

Return Value

A number.

fkit.obj

Objects

This module defines operations on objects.

applyMethod(k, a, o) curried

Applies a method to a value.

Discussion

Returns the result of the method k of object o applied to the value a.

Parameters

k

A string.

a

A value.

o

An object.

Return Value

A value.

Example

var person = {sayHi: a => ['Hi', a, '!'].join(' ')}
F.applyMethod(sayHi, 'Jane', person) // Hi Jane!

applyMethod2(k, a, b, o) curried

Applies a method to two values.

Discussion

Returns the result of the method k of object o applied to the values a and b.

Parameters

k

A string.

a

A value.

b

A value.

o

An object.

Return Value

A value.

Example

var person = {sayHi: (a, b) => ['Hi', a, b, '!'].join(' ')}
F.applyMethod2(sayHi, 'Jane', 'Appleseed', person) // Hi Jane Appleseed!

applyMethod3(k, a, b, c, o) curried

Applies a method to three values.

Discussion

Returns the result of the method k of object o applied to the values a, b, and c.

Parameters

k

A string.

a

A value.

b

A value.

c

A value.

o

An object.

Return Value

A value.

Example

var person = {sayHi: (a, b, c) => ['Hi', a, b, c, '!'].join(' ')}
F.applyMethod3(sayHi, 'Ms', 'Jane', 'Appleseed', person) // Hi Ms Jane Appleseed!

copy(os)

Creates a copy of an object.

Discussion

Returns a copy of the objects in the list of os.

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.

Parameters

os

A list.

Return Value

A new object.

Example

var person = {name: 'Jane', age: 20, city: 'Melbourne'}
F.copy(person, {name: 'Steve'}) // {name: 'Steve', age: 20, city: 'Melbourne'}

get(k, o) curried

Gets a property of an object.

Discussion

Returns the property at the key k in the object o.

Parameters

k

A string.

o

An object.

Return Value

A value.

Example

var person = {name: 'Jane', age: 20, city: 'Melbourne'}
F.get('name', person) // 'Jane'

getIn(ks, o) curried

Gets a property of an object using a key path.

Discussion

Returns the property at the key path ks in the object o.

Parameters

ks

A list.

o

An object.

Return Value

A value.

Example

var person = {name: 'Jane', age: 20, address: {city: 'Melbourne', country: 'Australia'}}
F.getIn(['address', 'city'], person) // 'Melbourne'

keys(o)

Gets the keys of an object.

Discussion

Returns a list of keys for the properties of the object o.

Parameters

o

An object.

Return Value

A new list.

Example

var person = {name: 'Jane', age: 20, city: 'Melbourne'}
F.keys(person) // ['name', 'age', 'city']

omit(ks, o) curried

Omits properties of an object.

Discussion

Returns a copy of the object o without the properties in the list of ks.

Parameters

ks

A list.

o

An object.

Return Value

A new object.

Example

var person = {name: 'Jane', age: 20, city: 'Melbourne'}
F.omit(['name', 'age'], person) // {city: 'Melbourne'}

pairs(o)

Gets the key-value pairs of an object.

Discussion

Returns a list of key-value pairs for the properties of the object o.

Parameters

o

An object.

Return Value

A new list.

Example

var person = {name: 'Jane', age: 20, city: 'Melbourne'}
F.pairs(person) // [['name', 'Jane'], ['age', 20], ['city', 'Melbourne']]

pick(ks, o) curried

Picks properties of an object.

Discussion

Returns a copy of the object o with the properties in the list of ks.

Parameters

ks

A list.

o

An object.

Return Value

A new object.

Example

var person = {name: 'Jane', age: 20, city: 'Melbourne'}
F.pick(['name', 'age'], person) // {name: 'Jane', age: 20}

set(k, v, o) curried

Sets a property of an object.

Discussion

Returns a copy of the object o with the property k set to the value v.

Parameters

k

A string.

v

A value.

o

An object.

Return Value

A new object.

Example

var person = {name: 'Jane', age: 20, city: 'Melbourne'}
F.set('name', 'Steve', person) // {name: 'Steve', age: 20, city: 'Melbourne'}

update(k, f, o) curried

Updates a property of an object with a function.

Discussion

Returns a copy of the object o with the property k updated with the function f.

Parameters

k

A string.

f

A function.

o

An object.

Return Value

A new object.

Example

var person = {name: 'Jane', age: 20, city: 'Melbourne'}
F.update('age', F.inc, person) // {name: 'Jane', age: 21, city: 'Melbourne'}

values(o)

Gets the values of an object.

Discussion

Returns a list of values for the properties of the object o.

Parameters

o

An object.

Return Value

A new list.

Example

var person = {name: 'Jane', age: 20, city: 'Melbourne'}
F.values(person) // ['Jane', 20, 'Melbourne']

fkit.string

Strings

This module defines string functions.

replace(a, b, s) curried

Replaces a term in a string.

Discussion

Returns the result of replacing term a with the string b in the string s.

Parameters

a

A string or a regexp.

b

A string.

s

A string.

Return Value

A new string.

Example

F.replace('r', 'z', 'bar') // baz
F.replace(/$hello/, 'goodbye', 'hello world!') // goodbye world!

toLower(s)

Converts a string to lowercase.

Parameters

s

A string.

Return Value

A new string.

toUpper(s)

Converts a string to uppercase.

Parameters

s

A string.

Return Value

A new string.