plymio_funcio v0.1.0 Plymio.Funcio.Map.Utility View Source

Map Utility Functions.

See Plymio.Funcio for overview and documentation terms.

Link to this section Summary

Functions

reduce_map1_funs/1 takes one or more map/1 functions, validates them, and reduces them into a single map/1 function for use with e.g. Enum.map/2

validate_map1_fun/1 validates the argument is an arity 1 function, returning {:ok, fun} or {:error, error}

validate_map1_funs/1 validates the argument is a list of arity 1 functions, returning {:ok, funs} or {:error, error}

Link to this section Types

Link to this section Functions

Link to this function normalise_map1_funs(funs) View Source
normalise_map1_funs(any()) :: {:ok, [fun1_map()]} | {:error, error()}

normalise_map1_funs/1 calls Plymio.Fontais.Utility.list_wrap_flat_just/1 on the argument and then calls validate_map1_funs/1, returning {:ok, funs}.

Examples

iex> fun1 = fn v -> v end
...> {:ok, funs} = fun1 |> normalise_map1_funs
...> funs |> Enum.all?(&(is_function(&1,1)))
true

iex> fun1 = [fn v -> v end]
...> {:ok, funs} = fun1 |> normalise_map1_funs
...> funs |> Enum.all?(&(is_function(&1,1)))
true

iex> fun1 = [fn v -> v + 1 end, fn v -> v * v end, fn v -> v - 1 end]
...> {:ok, funs} = fun1 |> normalise_map1_funs
...> funs |> Enum.all?(&(is_function(&1,1)))
true

iex> fun1 = [fn v -> v end, fn _k,v -> v end]
...> {:error, error} = fun1 |> normalise_map1_funs
...> error |> Exception.message |> String.starts_with?("map/1 function invalid")
true
Link to this function reduce_map1_funs(funs) View Source
reduce_map1_funs(any()) :: {:ok, fun1_map()} | {:error, error()}

reduce_map1_funs/1 takes one or more map/1 functions, validates them, and reduces them into a single map/1 function for use with e.g. Enum.map/2.

Examples

iex> fun1 = fn v -> v end
...> {:ok, fun2} = fun1 |> reduce_map1_funs
...> 42 |> fun2.()
42

iex> fun1 = [fn v -> v + 5 end, fn v -> v - 11 end, fn v -> v * v end]
...> {:ok, fun2} = fun1 |> reduce_map1_funs
...> 42 |> fun2.()
1296

iex> fun1 = fn k,v -> {k,v} end
...> {:error, error} = fun1 |> reduce_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 |> reduce_map1_funs
...> error |> Exception.message |> String.starts_with?("map/1 function invalid")
true
Link to this function reduce_or_nil_map1_funs(funs) View Source
reduce_or_nil_map1_funs(any()) ::
  {:ok, fun1_map()} | {:ok, nil} | {:error, error()}

reduce_map1_funs/1 takes a map/1.

It calls Plymio.Fontais.Utility.list_wrap_flat_just/1 on the argument and, if there are no functions (i..e empty list) it returns {:ok, nil}.

If there are functions, it calls reduce_map_funs/1.

Examples

iex> fun1 = fn v -> v * v end
...> {:ok, fun2} = fun1 |> reduce_or_nil_map1_funs
...> 3 |> fun2.()
9

iex> fun1 = [nil]
...> fun1 |> reduce_or_nil_map1_funs
{:ok, nil}

iex> fun1 = [fn v -> v + 5 end, fn v -> v - 11 end, fn v -> v * v end]
...> {:ok, fun2} = fun1 |> reduce_or_nil_map1_funs
...> 42 |> fun2.()
1296
Link to this function reduce_or_passthru_map1_funs(funs) View Source
reduce_or_passthru_map1_funs(any()) :: {:ok, fun1_map()} | {:error, error()}

reduce_map1_funs/1 takes a map/1.

It calls Plymio.Fontais.Utility.list_wrap_flat_just/1 on the argument and, if there are no functions (i..e empty list) it returns {:ok, fn v -> v end}.

If there are functions, it calls reduce_map_funs/1.

Examples

iex> fun1 = fn v -> v * v end
...> {:ok, fun2} = fun1 |> reduce_or_passthru_map1_funs
...> 3 |> fun2.()
9

iex> fun1 = nil
...> {:ok, fun2} = fun1 |> reduce_or_passthru_map1_funs
...> 3 |> fun2.()
3

iex> fun1 = [fn v -> v + 5 end, fn v -> v - 11 end, fn v -> v * v end]
...> {:ok, fun2} = fun1 |> reduce_or_passthru_map1_funs
...> 42 |> fun2.()
1296
Link to this function validate_map1_fun(fun) View Source
validate_map1_fun(any()) :: {:ok, (any() -> any())} | {:error, error()}

validate_map1_fun/1 validates the argument is an arity 1 function, returning {:ok, fun} or {:error, error}.

Examples

iex> fun1 = fn v -> v end
...> {:ok, fun2} = fun1 |> validate_map1_fun
...> fun2 |> is_function(1)
true

iex> fun1 = fn _k,v -> v end
...> {:error, error} = fun1 |> validate_map1_fun
...> error |> Exception.message |> String.starts_with?("map/1 function invalid")
true

iex> fun1 = 42
...> {:error, error} = fun1 |> validate_map1_fun
...> error |> Exception.message
"map/1 function invalid, got: 42"
Link to this function validate_map1_funs(funs) View Source
validate_map1_funs(any()) :: {:ok, [fun1_map()]} | {:error, error()}

validate_map1_funs/1 validates the argument is a list of arity 1 functions, returning {:ok, funs} or {:error, error}.

Examples

iex> fun1 = fn v -> v end
...> {:error, error} = fun1 |> validate_map1_funs
...> error |> Exception.message |> String.starts_with?("map/1 functions invalid")
true

iex> fun1 = [fn v -> v end]
...> {:ok, fun2} = fun1 |> validate_map1_funs
...> fun2 |> Enum.all?(&(is_function(&1,1)))
true

iex> fun1 = [fn v -> v end, fn _k,v -> v end]
...> {:error, error} = fun1 |> validate_map1_funs
...> error |> Exception.message |> String.starts_with?("map/1 function invalid")
true