Skip to content

compose

Create a composition of functions

78 bytes

Usage

In a composition of functions, each function is given the next function as an argument and must call it to continue executing.

import * as _ from 'radashi'
const useZero = (fn: any) => () => fn(0)
const objectize = (fn: any) => (num: any) => fn({ num })
const increment =
(fn: any) =>
({ num }: any) =>
fn({ num: num + 1 })
const returnArg = (arg: any) => (args: any) => args[arg]
const composed = _.compose(
useZero,
objectize,
increment,
increment,
returnArg('num'),
)
composed() // => 2

This can be a little jarring if you haven’t seen it before. Here’s a broken down composition. It’s equivalent to the code above.

const decomposed = useZero(objectize(increment(increment(_.returnArg('num')))))
decomposed() // => 2