Dx.Defd.Result (dx v0.3.5)
View SourceCollection of functions to work with Dx-internal result types.
A result is either:
{:error, e}if an error occurred{:not_loaded, data_reqs}if the result could not be determined without loading more data{:ok, result}otherwiseresultcan also be aDx.Scopeor aDx.Defd.Fn
Data loading
In general, {:not_loaded, all_reqs} only ever returns data requirements that are really needed.
Example using all
For example, using all?/1 with 3 conditions A, B and C, where
iex> [
...> {:ok, true}, # A
...> {:not_loaded, [1]}, # B
...> {:ok, false}, # C
...> ]
...> |> Dx.Defd.Result.all?()
{:ok, false}The overall result is {:ok, false}.
While B would need more data to be loaded, C can already determined and is false,
so and any additional data loaded will not change that.
Example using find
Another example, using find/1 with 5 conditions A, B, C, D and E, where
iex> [
...> {:ok, false}, # A
...> {:not_loaded, [1]}, # B
...> {:not_loaded, [2]}, # C
...> {:ok, true}, # D
...> {:not_loaded, [3]}, # E
...> ]
...> |> Dx.Defd.Result.find()
{:not_loaded, [1, 2]}The overall result is {:not_loaded, data_reqs1 + data_reqs2}.
While D can already be determined and is {:ok, true}, B and C come first and need more data
to be loaded, so they can be determined and returned if either is {:ok, true} first.
All data requirements that might be needed are returned together in the result (those of B and C),
while those of E can be ruled out, as D already returns {:ok, true} and comes first.
Summary
Functions
Returns {:ok, true} if fun evaluates to {:ok, true} for all elements in enum.
Otherwise, returns {:not_loaded, data_reqs} if any yield that.
Otherwise, returns {:ok, false}.
Returns {:ok, true} if fun evaluates to {:ok, true} for any element in enum.
Otherwise, returns {:not_loaded, data_reqs} if any yields that.
Otherwise, returns {:ok, false}.
Returns {:ok, mapped_results} if all elements map to {:ok, result}.
Otherwise, returns {:error, e} on error, or {:not_loaded, data_reqs} with all data requirements.
Returns {:ok, elem} for the first elem for which fun evaluates to {:ok, true}.
If any elements before that return {:not_loaded, data_reqs}, returns all of them combined as {:not_loaded, ...}.
Otherwise, returns {:ok, default}.
Returns {:ok, mapped_results} if all elements map to {:ok, result}.
Otherwise, returns {:error, e} on error, or {:not_loaded, data_reqs} with all data requirements.
Applies the mapper function to each element and reduces the mapped results
using acc and fun as long as the mapped results are {:ok, _} tuples.
Returns {:ok, reduced_acc} if all elements map to {:ok, result}.
Otherwise, returns {:error, e} on error, or {:not_loaded, data_reqs} with all data requirements.
Wraps a value in an :ok result.
When given {:ok, value}, runs fun on value and returns the result.
Otherwise, returns first argument as is.
When given {:ok, value}, runs fun on value and returns {:ok, new_value}.
Otherwise, returns first argument as is.
Functions
Returns {:ok, true} if fun evaluates to {:ok, true} for all elements in enum.
Otherwise, returns {:not_loaded, data_reqs} if any yield that.
Otherwise, returns {:ok, false}.
Examples
iex> [
...> {:ok, true},
...> {:not_loaded, []},
...> {:ok, false},
...> ]
...> |> Dx.Defd.Result.all?()
{:ok, false}
iex> [
...> {:ok, true},
...> {:not_loaded, []},
...> {:ok, true},
...> ]
...> |> Dx.Defd.Result.all?()
{:not_loaded, []}
iex> [
...> {:ok, true},
...> {:ok, true},
...> ]
...> |> Dx.Defd.Result.all?()
{:ok, true}
Returns {:ok, true} if fun evaluates to {:ok, true} for any element in enum.
Otherwise, returns {:not_loaded, data_reqs} if any yields that.
Otherwise, returns {:ok, false}.
Examples
iex> [
...> {:ok, true},
...> {:not_loaded, []},
...> {:ok, false},
...> ]
...> |> Dx.Defd.Result.any?()
{:ok, true}
iex> [
...> {:ok, false},
...> {:not_loaded, []},
...> {:ok, false},
...> ]
...> |> Dx.Defd.Result.any?()
{:not_loaded, []}
iex> [
...> {:ok, false},
...> {:ok, false},
...> ]
...> |> Dx.Defd.Result.any?()
{:ok, false}
Returns {:ok, mapped_results} if all elements map to {:ok, result}.
Otherwise, returns {:error, e} on error, or {:not_loaded, data_reqs} with all data requirements.
Examples
iex> [
...> {:ok, 1},
...> {:ok, 2},
...> {:ok, 3},
...> ]
...> |> Dx.Defd.Result.collect_reverse()
{:ok, [3, 2, 1]}
iex> [
...> {:ok, 1},
...> {:not_loaded, [:x]},
...> {:ok, 3},
...> {:not_loaded, [:y]},
...> ]
...> |> Dx.Defd.Result.collect_reverse()
{:not_loaded, [:y, :x]}
iex> [
...> {:ok, 1},
...> {:error, :x},
...> {:ok, 3},
...> {:not_loaded, [:y]},
...> ]
...> |> Dx.Defd.Result.collect_reverse()
{:error, :x}
Returns {:ok, elem} for the first elem for which fun evaluates to {:ok, true}.
If any elements before that return {:not_loaded, data_reqs}, returns all of them combined as {:not_loaded, ...}.
Otherwise, returns {:ok, default}.
Examples
iex> [
...> {:ok, true},
...> {:not_loaded, []},
...> {:ok, false},
...> ]
...> |> Dx.Defd.Result.find()
{:ok, {:ok, true}}
iex> [
...> {:ok, false},
...> {:not_loaded, [1]},
...> {:not_loaded, [2]},
...> {:ok, true},
...> {:not_loaded, [3]},
...> ]
...> |> Dx.Defd.Result.find()
{:not_loaded, [1, 2]}
iex> [
...> {:ok, false},
...> {:ok, false},
...> ]
...> |> Dx.Defd.Result.find()
{:ok, nil}
iex> [
...> false,
...> false,
...> ]
...> |> Dx.Defd.Result.find(&{:ok, not &1})
{:ok, false}
Returns {:ok, mapped_results} if all elements map to {:ok, result}.
Otherwise, returns {:error, e} on error, or {:not_loaded, data_reqs} with all data requirements.
Examples
iex> [
...> {:ok, 1},
...> {:ok, 2},
...> {:ok, 3},
...> ]
...> |> Dx.Defd.Result.map()
{:ok, [1, 2, 3]}
iex> [
...> {:ok, 1},
...> {:not_loaded, [:x]},
...> {:ok, 3},
...> {:not_loaded, [:y]},
...> ]
...> |> Dx.Defd.Result.map()
{:not_loaded, [:x, :y]}
iex> [
...> {:ok, 1},
...> {:error, :x},
...> {:ok, 3},
...> {:not_loaded, [:y]},
...> ]
...> |> Dx.Defd.Result.map()
{:error, :x}
Applies the mapper function to each element and reduces the mapped results
using acc and fun as long as the mapped results are {:ok, _} tuples.
Returns {:ok, reduced_acc} if all elements map to {:ok, result}.
Otherwise, returns {:error, e} on error, or {:not_loaded, data_reqs} with all data requirements.
Examples
iex> [
...> {:ok, 1},
...> {:ok, 2},
...> {:ok, 3},
...> ]
...> |> Dx.Defd.Result.map_then_reduce_ok(& &1, [], &[&1 | &2])
{:ok, [3, 2, 1]}
iex> [
...> {:ok, 1},
...> {:not_loaded, [:x]},
...> {:ok, 3},
...> {:not_loaded, [:y]},
...> ]
...> |> Dx.Defd.Result.map_then_reduce_ok(& &1, [], &[&1 | &2])
{:not_loaded, [:x, :y]}
iex> [
...> {:ok, 1},
...> {:error, :x},
...> {:ok, 3},
...> {:not_loaded, [:y]},
...> ]
...> |> Dx.Defd.Result.map_then_reduce_ok(& &1, [], &[&1 | &2])
{:error, :x}
Wraps a value in an :ok result.
When given {:ok, value}, runs fun on value and returns the result.
Otherwise, returns first argument as is.
When given {:ok, value}, runs fun on value and returns {:ok, new_value}.
Otherwise, returns first argument as is.