Relate v0.2.0 Relate View Source

Relate implements relational operators on Elixir enumerables.

Functions take two enumerables containing objects as well as one or two arguments to specify how the value to use as a basis for joining will be determined.

If either fki1 or fki2 are functions, the function will be called with the object as an argument on e1 or e2, respectively. If the value is an atom, the object will be treated as a map and the join value will be the value of the key associated with the atom. If the value is a non-negative integer, the object will be treated as a tuple and the value will be treated as an index into it.

If fki2 is not specified or is nil or false, the same accessor will be used on both e1 and e2.

Link to this section Summary

Functions

Return an enumerable of tuples containing all elements of e1 and e2 which yield the same value when fki1 and fki2 are applied, respectively. If fki2 is nil or false, fki1 will be used to make comparisons on both enumerables.

Return an enumerable of tuples containing all elements of e1 and e2 which yield the same value when fki1 and fki2 are applied, respectively. If fki2 is nil or false, fki1 will be used to make comparisons on both enumerables. Additionally, a tuple of the form {i, nil} will be returned for every element of e1 that did not match on any element in e2.

Return an enumerable of tuples containing all elements of e1 and e2 which yield the same value when fki1 and fki2 are applied, respectively. If fki2 is nil or false, fki1 will be used to make comparisons on both enumerables. Additionally, a tuple of the form {nil, i} or {i, nil} will be returned for every element of e2 that did not match on any element in e1 and vice-versa.

Return an enumerable of tuples containing all elements of e1 and e2 which yield the same value when fki1 and fki2 are applied, respectively. If fki2 is nil or false, fki1 will be used to make comparisons on both enumerables. Additionally, a tuple of the form {nil, i} will be returned for every element of e2 that did not match on any element in e1.

Link to this section Functions

Link to this function

inner_join(e1, e2, fki1, fki2 \\ nil) View Source

Return an enumerable of tuples containing all elements of e1 and e2 which yield the same value when fki1 and fki2 are applied, respectively. If fki2 is nil or false, fki1 will be used to make comparisons on both enumerables.

Examples

iex> Relate.inner_join([%{k: 0, v: "zero"}, %{k: 1, v: "one"}],
...>                   [%{k: 1, v: "i"}, %{k: 2, v: "ii"}],
...>                   :k, :k)
[{%{k: 1, v: "one"}, %{k: 1, v: "i"}}]

iex> Relate.inner_join([%{k: 0, v: "zero"}, %{k: 1, v: "one"}],
...>                   [%{k: 1, v: "i"}, %{k: 2, v: "ii"}],
...>                   :k)  # NOTE: only one key function
[{%{k: 1, v: "one"}, %{k: 1, v: "i"}}]
Link to this function

left_join(e1, e2, fki1, fki2 \\ nil) View Source

Return an enumerable of tuples containing all elements of e1 and e2 which yield the same value when fki1 and fki2 are applied, respectively. If fki2 is nil or false, fki1 will be used to make comparisons on both enumerables. Additionally, a tuple of the form {i, nil} will be returned for every element of e1 that did not match on any element in e2.

Examples

iex> Relate.left_join([%{k: 0, v: "zero"}, %{k: 1, v: "one"}],
...>                  [%{k: 1, v: "i"}, %{k: 2, v: "ii"}],
...>                  :k, :k)
[{%{k: 0, v: "zero"}, nil}, {%{k: 1, v: "one"}, %{k: 1, v: "i"}}]

iex> Relate.left_join([%{k: 0, v: "zero"}, %{k: 1, v: "one"}],
...>                  [%{k: 1, v: "i"}, %{k: 2, v: "ii"}],
...>                  :k)  # NOTE: only one key function
[{%{k: 0, v: "zero"}, nil}, {%{k: 1, v: "one"}, %{k: 1, v: "i"}}]
Link to this function

outer_join(e1, e2, fki1, fki2 \\ nil) View Source

Return an enumerable of tuples containing all elements of e1 and e2 which yield the same value when fki1 and fki2 are applied, respectively. If fki2 is nil or false, fki1 will be used to make comparisons on both enumerables. Additionally, a tuple of the form {nil, i} or {i, nil} will be returned for every element of e2 that did not match on any element in e1 and vice-versa.

Examples

iex> Relate.outer_join([%{k: 0, v: "zero"}, %{k: 1, v: "one"}],
...>                   [%{k: 1, v: "i"}, %{k: 2, v: "ii"}],
...>                   :k, :k)
[{%{k: 0, v: "zero"}, nil}, {%{k: 1, v: "one"}, %{k: 1, v: "i"}}, {nil, %{k: 2, v: "ii"}}]

iex> Relate.outer_join([%{k: 0, v: "zero"}, %{k: 1, v: "one"}],
...>                   [%{k: 1, v: "i"}, %{k: 2, v: "ii"}],
...>                   :k)  # NOTE: only one key function
[{%{k: 0, v: "zero"}, nil}, {%{k: 1, v: "one"}, %{k: 1, v: "i"}}, {nil, %{k: 2, v: "ii"}}]
Link to this function

right_join(e1, e2, fki1, fki2 \\ nil) View Source

Return an enumerable of tuples containing all elements of e1 and e2 which yield the same value when fki1 and fki2 are applied, respectively. If fki2 is nil or false, fki1 will be used to make comparisons on both enumerables. Additionally, a tuple of the form {nil, i} will be returned for every element of e2 that did not match on any element in e1.

Examples

iex> Relate.right_join([%{k: 0, v: "zero"}, %{k: 1, v: "one"}],
...>                   [%{k: 1, v: "i"}, %{k: 2, v: "ii"}],
...>                   :k, :k)
[{%{k: 1, v: "one"}, %{k: 1, v: "i"}}, {nil, %{k: 2, v: "ii"}}]

iex> Relate.right_join([%{k: 0, v: "zero"}, %{k: 1, v: "one"}],
...>                   [%{k: 1, v: "i"}, %{k: 2, v: "ii"}],
...>                   :k)  # NOTE: only one key function
[{%{k: 1, v: "one"}, %{k: 1, v: "i"}}, {nil, %{k: 2, v: "ii"}}]