Quark v2.1.0 Quark.Compose
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.
Summary
Functions
Infix compositon operator
Function composition, from the tail of the list to the head
Function composition
Function composition, from the back of the lift 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)
Functions
Specs
(... -> any) <|> (... -> any) :: (... -> any)
Infix compositon operator
Examples
iex> sum_plus_one = fn x -> x + 1 end <|> &Enum.sum/1
iex> sum_plus_one.([1,2,3])
7
iex> add_one = &(&1 + 1)
iex> piped = [1,2,3] |> Enum.sum |> add_one.()
iex> composed = [1,2,3] |> ((add_one <|> &Enum.sum/1)).()
iex> piped == composed
true
Specs
compose([(... -> any)]) :: (... -> any)
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
Specs
compose((... -> any), (... -> any)) :: any
Function composition
Examples
iex> sum_plus_one = compose(&(&1 + 1), &Enum.sum/1)
iex> [1,2,3] |> sum_plus_one.()
7
Function composition, from the back of the lift to the front
Examples
iex> sum_plus_one = compose_forward(&(Enum.sum(&1)), &(&1 + 1))
iex> [1,2,3] |> sum_plus_one.()
7
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)])
...> [1,2,3] |> sum_plus_one.()
7