Witchcraft v1.0.0 Witchcraft.Extend View Source
Extend
is essentially “coChain
”, meaning that it reverses the relationships
in Chain
.
Instead of a flattening operation, we have nest
which wraps the data in
an additional layer of itsef.
Instead of a chain
ing function that acts on raw data and wraps it,
we have extend
which unwraps data, may modify it, and returns the unwrapped value
Type Class
An instance of Witchcraft.Extend
must also implement Witchcraft.Functor
,
and define Witchcraft.Extend.nest/1
.
Functor [map/2]
↓
Extend [nest/1]
Link to this section Summary
Functions
Examples
The same as extend/2
, but with the colinking function curried
The same as extend/2
, but with the colinking function curried
Similar to Witchcraft.Chain.chain/2
, except that it reverses the input and output
types of the colinking function
Wrap some nestable data structure in another layer of itself
extend/2
with arguments flipped
pipe_colink/2
with functions curried
Link to this section Types
Link to this section Functions
compose_colink(Witchcraft.Extend.colink, Witchcraft.Extend.colink) :: (Witchcraft.Extend.t -> any)
Examples
iex> composed =
...> fn xs -> List.first(xs) * 10 end
...> |> compose_colink(fn ys -> List.first(ys) - 10 end)
...>
...> extend([1, 2, 3], composed)
[-90, -80, -70]
iex> fn xs -> List.first(xs) * 10 end
...> |> compose_colink(fn ys -> List.first(ys) - 10 end)
...> |> compose_colink(fn zs -> List.first(zs) * 50 end)
...> |> peel([1, 2, 3])
[400, 900, 1400]
iex> fn xs -> List.first(xs) * 10 end
...> |> compose_colink(fn ys -> List.first(ys) - 10 end)
...> |> compose_colink(fn zs -> List.first(zs) * 50 end)
...> |> compose_colink(fn zs -> List.first(zs) + 12 end)
...> |> peel([1, 2, 3])
[6400, 6900, 7400]
curried_extend(Witchcraft.Extend.t, (... -> any)) :: Witchcraft.Extend.t
The same as extend/2
, but with the colinking function curried.
Examples
iex> [1, 2, 3]
...> |> curried_extend(fn(list, coeff) -> List.first(list) * coeff end)
...> |> extend(fn(funs) -> List.first(funs).(10) end)
[10, 20, 30]
curried_peel(Witchcraft.Extend.t, (... -> any)) :: Witchcraft.Extend.t
The same as extend/2
, but with the colinking function curried.
Examples
iex> fn(list) -> List.first(list) * 10 end
...> |> curried_peel([1, 2, 3])
[10, 20, 30]
Similar to Witchcraft.Chain.chain/2
, except that it reverses the input and output
types of the colinking function.
Examples
Chain:
iex> Witchcraft.Chain.chain([1, 2, 3], fn x -> [x * 10] end)
[10, 20, 30]
Extend:
iex> extend([1, 2, 3], fn list -> List.first(list) * 10 end)
[10, 20, 30]
Wrap some nestable data structure in another layer of itself
Examples
iex> nest([1, 2, 3])
[[1, 2, 3], [2, 3], [3]]
extend/2
with arguments flipped.
Makes piping composed colinks easier (see compose_colink/2
and pipe_compose_colink/2
).
Examples
iex> fn list -> List.first(list) * 10 end
...> |> peel([1, 2, 3])
[10, 20, 30]
pipe_compose_colink(Witchcraft.Extend.colink, Witchcraft.Extend.colink) :: (Witchcraft.Extend.t -> any)
pipe_colink/2
with functions curried.
Examples
iex> fn xs -> List.first(xs) * 10 end
...> |> pipe_compose_colink(fn ys -> List.first(ys) - 2 end)
...> |> peel([1, 2, 3])
[8, 18, 28]
iex> composed =
...> fn xs -> List.first(xs) * 10 end
...> |> pipe_compose_colink(fn ys -> List.first(ys) - 2 end)
...> |> pipe_compose_colink(fn zs -> List.first(zs) * 5 end)
...>
...> extend([1, 2, 3], composed)
[40, 90, 140]
iex> fn xs -> List.first(xs) * 10 end
...> |> pipe_compose_colink(fn ys -> List.first(ys) - 2 end)
...> |> pipe_compose_colink(fn zs -> List.first(zs) * 5 end)
...> |> pipe_compose_colink(fn zs -> List.first(zs) + 1 end)
...> |> peel([1, 2, 3])
[41, 91, 141]