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
import _
_
from 'radashi'
const
const useZero: (fn: any) => () => any
useZero
= (
fn: any
fn
: any) => () =>
fn: any
fn
(0)
const
const objectize: (fn: any) => (num: any) => any
objectize
= (
fn: any
fn
: any) => (
num: any
num
: any) =>
fn: any
fn
({
num: any
num
})
const
const increment: (fn: any) => ({ num }: any) => any
increment
=
(
fn: any
fn
: any) =>
({
num: any
num
}: any) =>
fn: any
fn
({
num: any
num
:
num: any
num
+ 1 })
const
const returnArg: (arg: any) => (args: any) => any
returnArg
= (
arg: any
arg
: any) => (
args: any
args
: any) =>
args: any
args
[
arg: any
arg
]
const
const composed: () => any
composed
=
import _
_
.
compose<any, [], [num: any], [any], any, [any], any, [args: any], any, any>(f1: (next: (num: any) => any) => () => any, f2: (next: (args_0: any) => any) => (num: any) => any, f3: (next: (args_0: any) => any) => (args_0: any) => any, f4: (next: (args: any) => any) => (args_0: any) => any, last: (args: any) => any): () => any (+8 overloads)
export compose

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

@seehttps://radashi.js.org/reference/curry/compose

@example

const myComposedFunc = compose(
(x) => x + 5,
(x) => x * 2,
)
myComposedFunc(0)
// => 5

@version12.1.0

compose
(
const useZero: (fn: any) => () => any
useZero
,
const objectize: (fn: any) => (num: any) => any
objectize
,
const increment: (fn: any) => ({ num }: any) => any
increment
,
const increment: (fn: any) => ({ num }: any) => any
increment
,
const returnArg: (arg: any) => (args: any) => any
returnArg
('num'),
)
const composed: () => any
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