A transformation function.
A function where the input type is the same as the output.
// uppercase :: String -> String
const uppercase = (str) => str.toUpperCase()
// decrement :: Number -> Number
const decrement = (x) => x - 1
A pair of transformations between 2 types of objects that is structural in nature and no data is lost.
For example, 2D coordinates could be stored as an array [2,3]
or object {x: 2, y: 3}
.
// Providing functions to convert in both directions makes them isomorphic.
const pairToCoords = (pair) => ({x: pair[0], y: pair[1]})
const coordsToPair = (coords) => [coords.x, coords.y]
coordsToPair(pairToCoords([1, 2])) // [1, 2]
pairToCoords(coordsToPair({x: 1, y: 2})) // {x: 1, y: 2}
A homomorphism is just a structure preserving map. In fact, a functor is just a homomorphism between categories as it preserves the original category’s structure under the mapping.
A.of(f).ap(A.of(x)) == A.of(f(x))
Either.of(_.toUpper).ap(Either.of("oreos")) == Either.of(_.toUpper("oreos"))
A reduceRight
function that applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.
const sum = xs => xs.reduceRight((acc, x) => acc + x, 0)
sum([1, 2, 3, 4, 5]) // 15
An unfold
function. An unfold
is the opposite of fold
(reduce
). It generates a list from a single value.
const unfold = (f, seed) => {
function go(f, seed, acc) {
const res = f(seed);
return res ? go(f, res[1], acc.concat([res[0]])) : acc;
}
return go(f, seed, [])
}
const countDown = n => unfold((n) => {
return n <= 0 ? undefined : [n, n - 1]
}, n)
countDown(5) // [5, 4, 3, 2, 1]
The combination of anamorphism and catamorphism.
A function just like reduceRight
. However, there’s a difference:
In paramorphism, your reducer’s arguments are the current value, the reduction of all previous values, and the list of values that formed that reduction.
// Obviously not safe for lists containing `undefined`,
// but good enough to make the point.
const para = (reducer, accumulator, elements) => {
if (elements.length === 0)
return accumulator
const head = elements[0]
const tail = elements.slice(1)
return reducer(head, tail, para(reducer, accumulator, tail))
}
const suffixes = list => para(
(x, xs, suffxs) => [xs, ... suffxs],
[],
list
)
suffixes([1, 2, 3, 4, 5]) // [[2, 3, 4, 5], [3, 4, 5], [4, 5], [5], []]
The third parameter in the reducer (in the above example, [x, ... xs]
) is kind of like having a history of what got you to your current acc value.
it’s the opposite of paramorphism, just as anamorphism is the opposite of catamorphism. Whereas with paramorphism, you combine with access to the accumulator and what has been accumulated, apomorphism lets you unfold
with the potential to return early.