Witchcraft v1.0.0-beta Witchcraft.Traversable.Proto protocol View Source
Protocol for the Elixir.Witchcraft.Traversable
type class
For this type class’s API, please refer to Elixir.Witchcraft.Traversable
Link to this section Summary
Functions
Convert elements to actions, and then evaluate the actions from left-to-right, and accumulate the results
Link to this section Types
Link to this section Functions
Convert elements to actions, and then evaluate the actions from left-to-right, and accumulate the results.
For a version without accumulation, see then_traverse/2
.
Examples
iex> traverse([1, 2, 3], fn x -> {x, x * 2, x * 10} end)
{6, 12, [10, 20, 30]}
iex> traverse({1, 2, 3}, fn x -> [x] end)
[{1, 2, 3}]
iex> traverse({1, 2, 3}, fn x -> [x, x * 5, x * 10] end)
[
{1, 2, 3},
{1, 2, 15},
{1, 2, 30}
]
iex> traverse([1, 2, 3], fn x -> [x, x * 5, x * 10] end)
[
#
[1, 2, 3], [1, 2, 15], [1, 2, 30],
[1, 10, 3], [1, 10, 15], [1, 10, 30],
[1, 20, 3], [1, 20, 15], [1, 20, 30],
#
[5, 2, 3], [5, 2, 15], [5, 2, 30],
[5, 10, 3], [5, 10, 15], [5, 10, 30],
[5, 20, 3], [5, 20, 15], [5, 20, 30],
#
[10, 2, 3], [10, 2, 15], [10, 2, 30],
[10, 10, 3], [10, 10, 15], [10, 10, 30],
[10, 20, 3], [10, 20, 15], [10, 20, 30]
]
traverse([1, 2, 3], fn x -> %Algae.Maybe.Just{just: x} end)
#=> %Algae.Maybe.Just{just: [1, 2, 3]}
traverse(%Algae.Maybe.Just{just: 4}, fn x -> [x, x * 10] end)
#=> [
# %Algae.Maybe.Just{just: 4},
# %Algae.Maybe.Just{just: 40}
# ]
traverse([1, 2, 3], fn x ->
if is_even(x) do
%Algae.Maybe.Just{just: x}
else
%Algae.Maybe.Nothing{}
end
end)
#=> %Algae.Maybe.Nothing{}