Brex.Result v0.4.0 Brex.Result.Mappers View Source
Tools for combining Enum
and result tuples.
Link to this section Summary
Types
The type Enum.t/0
does not accept any arguments. This is a workaround to express the type of
an enumerable with elements restricted to a particular type.
Functions
Applies the function to each element of the enumerable until an error occurs.
No guarentee on the order of evaluation. (But usually backwards for lists.)
Only takes a function that returns :ok | {:error, value}
.
Given an enumerable of plain values,
it returns {:ok, processed enum}
or the first error
.
Equivalent to traverse or mapM in Haskell.
Binds the function to each tuple in the enum.
Given an enum of plain values, an initial value, and a reducing function,
it returns the first error
or reduced result.
Link to this section Types
enumerable(x)
View Source
enumerable(x) :: [x] | Enum.t()
enumerable(x) :: [x] | Enum.t()
The type Enum.t/0
does not accept any arguments. This is a workaround to express the type of
an enumerable with elements restricted to a particular type.
p()
View Source
p() :: Brex.Result.Base.p()
p() :: Brex.Result.Base.p()
s()
View Source
s() :: Brex.Result.Base.s()
s() :: Brex.Result.Base.s()
s(x)
View Source
s(x) :: Brex.Result.Base.s(x)
s(x) :: Brex.Result.Base.s(x)
t()
View Source
t() :: Brex.Result.Base.t()
t() :: Brex.Result.Base.t()
t(x)
View Source
t(x) :: Brex.Result.Base.t(x)
t(x) :: Brex.Result.Base.t(x)
Link to this section Functions
each_while_success(ms, f)
View Source
(since 0.2.0)
each_while_success(enumerable(a), (a -> p())) :: p() when a: var
each_while_success(enumerable(a), (a -> p())) :: p() when a: var
Applies the function to each element of the enumerable until an error occurs.
No guarentee on the order of evaluation. (But usually backwards for lists.)
Only takes a function that returns :ok | {:error, value}
.
Examples:
iex> [1, 2, 3, 4]
...> |> each_while_success(fn x -> if x < 3, do: :ok, else: {:error, :too_big} end)
{:error, :too_big}
iex> [1, 2, 3, 4]
...> |> each_while_success(fn x -> if x < 5, do: :ok, else: {:error, :too_big} end)
:ok
map_while_success(l, f)
View Source
(since 0.1.0)
map_while_success(enumerable(a), (a -> t(b))) :: s(enumerable(b))
when a: var
map_while_success(enumerable(a), (a -> t(b))) :: s(enumerable(b)) when a: var
Given an enumerable of plain values,
it returns {:ok, processed enum}
or the first error
.
Equivalent to traverse or mapM in Haskell.
Examples:
iex> [1, 2, 3, 4]
...> |> map_while_success(fn x -> if x == 3 || x == 1, do: {:error, x}, else: {:ok, x} end)
{:error, 1}
iex> map_while_success([1, 2, 3, 4], fn x -> {:ok, x + 2} end)
{:ok, [3, 4, 5, 6]}
map_with_bind(l, f)
View Source
(since 0.1.0)
map_with_bind(enumerable(s(a)), (a -> s(b))) :: enumerable(s(b))
when a: var
map_with_bind(enumerable(s(a)), (a -> s(b))) :: enumerable(s(b)) when a: var
Binds the function to each tuple in the enum.
Example:
iex> [{:ok, 1}, {:ok, 2}, {:error, 3}, {:ok, 4}]
...> |> map_with_bind(fn x -> if x == 2, do: {:error, x*6}, else: {:ok, x*6} end)
[{:ok, 6}, {:error, 12}, {:error, 3}, {:ok, 24}]
reduce_while_success(ms, b, f)
View Source
(since 0.3.0)
reduce_while_success(enumerable(a), b, (a, b -> t(b))) :: t(b)
when b: var, a: var
reduce_while_success(enumerable(a), b, (a, b -> t(b))) :: t(b) when b: var, a: var
Given an enum of plain values, an initial value, and a reducing function,
it returns the first error
or reduced result.
Examples:
iex> [1, 2, 3, 4]
...> |> reduce_while_success(100, &{:ok, &1 + &2})
{:ok, 110}
iex> [1, 2, 3, 4]
...> |> reduce_while_success(100, fn x, acc ->
...> if x > 2 do
...> {:error, x}
...> else
...> {:ok, x + acc}
...> end
...> end)
{:error, 3}