plymio_funcio v0.1.0 Plymio.Funcio.Map.Pattern View Source
Map Patterns.
See Plymio.Funcio
for an overview and documentation terms.
Link to this section Summary
Functions
apply1_map1_funs/1
takes a value and a map/1
reduce1_map1_funs/1
takes a map/1 and reduces it to a
single map/1 function that will return either {:ok, any}
or
{:error, error}
Link to this section Types
Link to this section Functions
apply1_map1_funs/1
takes a value and a map/1.
It reduces the map/1 and the reduced map/1 to the value returning {:ok, transformed_value}
.
Each map/1 function can return {:ok, value}
, {:error, error}
or value
. In the last case, the value
is treated as {:ok, value}
and the reduce_while continues.
The reduce is halted if any function returns an {:error, error}
result, returning the result.
Examples
iex> fun1 = fn v -> v end
...> 42 |> apply1_map1_funs(fun1)
{:ok, 42}
iex> fun1 = fn v -> v + 1 end
iex> fun2 = fn v -> v * v end
iex> fun3 = fn v -> v - 1 end
...> 42 |> apply1_map1_funs([fun1, fun2, fun3])
{:ok, 1848}
iex> fun1 = fn v -> v + 1 end
iex> fun2 = fn v -> {:error, %ArgumentError{message: "value is #{v}"}} end
iex> fun3 = fn v -> v - 1 end
...> {:error, error} = 42 |> apply1_map1_funs([fun1, fun2, fun3])
...> error |> Exception.message
"value is 43"
iex> fun1 = fn k,v -> {k,v} end
...> {:error, error} = 42 |> apply1_map1_funs(fun1)
...> error |> Exception.message |> String.starts_with?("map/1 function invalid")
true
iex> fun1 = [fn k,v -> {k,v} end, fn _k,v -> v end]
...> {:error, error} = 42 |> apply1_map1_funs(fun1)
...> error |> Exception.message |> String.starts_with?("map/1 function invalid")
true
reduce1_map1_funs/1
takes a map/1 and reduces it to a
single map/1 function that will return either {:ok, any}
or
{:error, error}
.
Each function in the map/1 must be pattern1 compatible i.e. can return
either {:ok, any}
, {:error, error}
, or value
.
Examples
iex> fun1 = fn v -> v end
...> {:ok, fun} = fun1 |> reduce1_map1_funs
...> 42 |> fun.()
{:ok, 42}
iex> fun1 = fn v -> v + 1 end
...> fun2 = fn v -> {:ok, v * v} end
...> fun3 = fn v -> v - 1 end
...> {:ok, fun} = [fun1, fun2, fun3] |> reduce1_map1_funs
...> 42 |> fun.()
{:ok, 1848}
iex> fun1 = fn v -> v + 1 end
...> fun2 = fn v -> {:error, %ArgumentError{message: "value is #{v}"}} end
...> fun3 = fn v -> v - 1 end
...> {:ok, fun} = [fun1, fun2, fun3] |> reduce1_map1_funs
...> {:error, error} = 42 |> fun.()
...> error |> Exception.message
"value is 43"
iex> fun1 = fn k,v -> {k,v} end
...> {:error, error} = fun1 |> reduce1_map1_funs
...> error |> Exception.message |> String.starts_with?("map/1 function invalid")
true
iex> fun1 = [fn k,v -> {k,v} end, fn _k,v -> v end]
...> {:error, error} = fun1 |> reduce1_map1_funs
...> error |> Exception.message |> String.starts_with?("map/1 function invalid")
true