The core functions of FP
Function/signature/aka |
Description (book location) |
---|---|
Return T → P<T> point, lift, pure | Takes a value of type T and “lifts” it into a “structure” with the given T as its “inner value” (sections 4.3 and 8.3). |
Map F<T> → (T → R) → F<R> fMap, project, lift; Select in LINQ | Takes an F<T> (a functor over T; that is, a structure whose inner value(s) have type T) and a function T → R, and returns an F<R> whose inner value(s) are the result of applying the given function to the inner value(s) of the given functor (section 4.1). |
Apply A<T → R> → A<T> → A<R> | Takes an applicative functor A<T → R> wrapping a function, and an A<T> wrapping an argument, and returns an A<R> wrapping the result of applying the wrapped function to the wrapped argument (section 8.1). |
Bind M<T> → (T → M<R>) → M<R> flatMap, chain, collect; SelectMany in LINQ | Takes an M<T> (a monad over T) and a function T → M<R>, extracts the inner value(s) from the given monad, and applies the given function to it (sections 4.3 and 8.3). |
Flatten M<M<T>> → M<T> join | Flattens a nested monad; if Bind is defined, Flatten can be defined by calling Bind with the identity function; vice versa, if Flatten is defined, Bind can be defined by composing Flatten with Map. |
Where M<T> → (T → bool) → M<T> filter | Takes a structure M<T> and a predicate over T, and returns a structure M<T> wrapping all the values in the given structure that satisfy the predicate (section 4.4). |
Aggregate F<T> → Acc → (Acc → T → Acc) → Acc fold, foldLeft, reduce | Takes a foldable structure of Ts, an initial accumulator value, and a reducer function. The reducer is a binary function that takes the accumulator and a single value T and yields a new accumulator. Aggregate traverses the structure with the given reducer, giving it each T in the foldable and the accumulator computed so far (section 7.6). |
Traverse Tr<T> → (T → A<R>) → A<Tr<R>> mapM | Takes a traversable structure Tr<T> and a function that takes a T and returns an applicative A<R>, and traverses the traversable with the given function, collecting the results in an A<Tr<R>> (in contrast with Map, which would produce a Tr<A<R>> instead) (section 13.2). |