Witchcraft.Extend (Witchcraft v1.0.4) 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

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]

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

Specs

colink() :: (t() -> any())

Specs

t() :: any()

Link to this section Functions

Specs

compose_colink(colink(), colink()) :: (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]
Link to this function

curried_extend(data, colink)

View Source

Specs

curried_extend(t(), (... -> any())) :: 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]
Link to this function

curried_peel(colink, data)

View Source

Specs

curried_peel(t(), (... -> any())) :: 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]

Specs

extend(t(), colink()) :: t()

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]

Specs

nest(t()) :: t()

Wrap some nestable data structure in another layer of itself

Examples

iex> nest([1, 2, 3])
[[1, 2, 3], [2, 3], [3]]

Specs

peel(colink(), t()) :: t()

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]
Link to this function

pipe_compose_colink(f, g)

View Source

Specs

pipe_compose_colink(colink(), colink()) :: (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]