Witchcraft v1.0.0-beta.2 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 chaining 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

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

The same as extend/2, but with the colinking function curried

extend/2 with arguments flipped

Link to this section Types

Link to this section Functions

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)
...> |> reverse_extend([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)
...> |> reverse_extend([1, 2, 3])
[6400, 6900, 7400]
Link to this function curried_extend(data, colink) View Source
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]

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]]

Examples

iex> fn xs -> List.first(xs) * 10 end
...> |> pipe_compose_colink(fn ys -> List.first(ys) - 2 end)
...> |> reverse_extend([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)
...> |> reverse_extend([1, 2, 3])
[41, 91, 141]
Link to this function reverse_curried_extend(colink, data) View Source
reverse_curried_extend(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
...> |> reverse_curried_extend([1, 2, 3])
[10, 20, 30]

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
...> |> reverse_extend([1, 2, 3])
[10, 20, 30]