Quark v2.3.0 Quark.Compose View Source

Function composition is taking two functions, and joining them together to create a new function. For example:

Examples

iex> sum_plus_one = compose([&(&1 + 1), &Enum.sum/1])
...> sum_plus_one.([1,2,3])
7

In this case, we have joined Enum.sum with a function that adds one, to create a new function that takes a list, sums it, and adds one.

Note that composition normally applies from right to left, though Quark provides the opposite in the form of *_forward functions.

Link to this section Summary

Functions

Infix compositon operator

Infix “forward” compositon operator

Function composition, from the tail of the list to the head

Function composition

Function composition, from the back of the list to the front

Compose functions, from the head of the list of functions. The is the reverse order versus what one would normally expect (left-to-right rather than right-to-left)

Compose functions, from the head of the list of functions. The is the reverse order versus what one would normally expect (left-to-right rather than right-to-left)

Link to this section Functions

Link to this function g <|> f View Source
(... -> any) <|> (... -> any) :: (... -> any)

Infix compositon operator

Examples

iex> sum_plus_one = fn x -> x + 1 end <|> &Enum.sum/1
...> sum_plus_one.([1,2,3])
7

iex> add_one  = &(&1 + 1)
...> piped    = [1, 2, 3] |> Enum.sum() |> add_one.()
...> composed = [1, 2, 3] |> ((add_one <|> &Enum.sum/1)).()
...> piped == composed
true
Link to this function f <~> g View Source
(... -> any) <~> (... -> any) :: (... -> any)

Infix “forward” compositon operator

Examples

iex> sum_plus_one = (&Enum.sum/1) <~> fn x -> x + 1 end
...> sum_plus_one.([1, 2, 3])
7

iex> x200 = (&(&1 * 2)) <~> (&(&1 * 10)) <~> (&(&1 * 10))
...> x200.(5)
1000

iex> add_one  = &(&1 + 1)
...> piped    = [1, 2, 3] |> Enum.sum() |> add_one.()
...> composed = [1, 2, 3] |> ((&Enum.sum/1) <~> add_one).()
...> piped == composed
true

Function composition, from the tail of the list to the head

Examples

iex> sum_plus_one = compose([&(&1 + 1), &Enum.sum/1])
...> [1,2,3] |> sum_plus_one.()
7
Link to this function compose(func_list) View Source
compose([(... -> any)]) :: (... -> any)
Link to this function compose(g, f) View Source
compose((... -> any), (... -> any)) :: any

Function composition

Examples

iex> sum_plus_one = compose(&(&1 + 1), &Enum.sum/1)
...> [1, 2, 3] |> sum_plus_one.()
7

Function composition, from the back of the list to the front

Examples

iex> sum_plus_one = compose_forward(&Enum.sum/1, &(&1 + 1))
...> [1, 2, 3] |> sum_plus_one.()
7
Link to this function compose_forward(f, g) View Source
compose_forward((... -> any), (... -> any)) :: (... -> any)

Compose functions, from the head of the list of functions. The is the reverse order versus what one would normally expect (left-to-right rather than right-to-left).

Examples

iex> sum_plus_one = compose_list([&(&1 + 1), &Enum.sum/1]) …> sum_plus_one.([1, 2, 3]) 7

Link to this function compose_list(func_list) View Source
compose_list([(... -> any)]) :: (... -> any)

Compose functions, from the head of the list of functions. The is the reverse order versus what one would normally expect (left-to-right rather than right-to-left).

Examples

iex> sum_plus_one = compose_list_forward([&Enum.sum/1, &(&1 + 1)])
...> sum_plus_one.([1, 2, 3])
7
Link to this function compose_list_forward(func_list) View Source
compose_list_forward([(... -> any)]) :: (... -> any)