View Source Moar.Enum (Moar v1.33.0)

Enum-related functions.

Link to this section Summary

Functions

Like Enum.at/3 but raises if index is out of bounds.

Removes nil elements from enum.

Returns the indices of elements in enum, using fun for comparisons (defaulting to Kernel.==/2)

Returns the first item of enum, or raises if it is empty.

Converts an enum into a map of maps indexed by the return value of index_fun. See also the similar map-specific Moar.Map.index_by/2.

Like Enum.into but accepts nil as the first argument

Returns true if the value is a map or a keyword list. This uses standard Elixir functions for determining if a term is a map or a keyword, and therefore counts an empty list as a keyword list. See also is_map_or_nonempty_keyword/1.

Like is_map_or_keyword/1 but returns false if the term is an empty list.

Sorts enum case-insensitively. Uses Enum.sort_by/3 under the hood.

Sorts enum case-insensitively by mapper function. Uses Enum.sort_by/3 under the hood.

Returns a list of elements at the given indices, in the given order. If :all is given instead of a list of indices, the entire enum is returned.

Returns :tid fields from enumerable.

Link to this section Functions

Link to this function

at!(enum, index, default \\ nil)

View Source
@spec at!(Enum.t(), integer() | [integer()], any()) :: any()

Like Enum.at/3 but raises if index is out of bounds.

@spec compact(Enum.t()) :: Enum.t()

Removes nil elements from enum.

Link to this function

find_indices(enum, elements, fun \\ &Kernel.==/2)

View Source
@spec find_indices(Enum.t(), [any()], (any(), any() -> boolean())) :: [integer()]

Returns the indices of elements in enum, using fun for comparisons (defaulting to Kernel.==/2)

iex> Moar.Enum.find_indices(~w[apple banana cherry], ~w[cherry apple])
[2, 0]

iex> Moar.Enum.find_indices(~w[apple banana cherry], ~w[CHERRY APPLE], fn a, b ->
...>   String.downcase(a) == String.downcase(b)
...> end)
[2, 0]
@spec first!(Enum.t()) :: any()

Returns the first item of enum, or raises if it is empty.

Link to this function

index_by(enum, index_fun)

View Source
@spec index_by(Enum.t(), (any() -> any())) :: map()

Converts an enum into a map of maps indexed by the return value of index_fun. See also the similar map-specific Moar.Map.index_by/2.

iex> Moar.Enum.index_by([%{name: "Alice", tid: "alice"}, %{name: "Billy", tid: "billy"}], & &1.tid)
%{"alice" => %{name: "Alice", tid: "alice"}, "billy" => %{name: "Billy", tid: "billy"}}
Link to this function

into!(struct, enumerable)

View Source
@spec into!(nil | Enum.t(), Enum.t()) :: Enum.t()

Like Enum.into but accepts nil as the first argument

Link to this function

is_map_or_keyword(value)

View Source
@spec is_map_or_keyword(any()) :: boolean()

Returns true if the value is a map or a keyword list. This uses standard Elixir functions for determining if a term is a map or a keyword, and therefore counts an empty list as a keyword list. See also is_map_or_nonempty_keyword/1.

This cannot be used as a guard because it uses Keyword.keyword? under the hood. Also, because of that, it might scan an entire list to see if it's a keyword list, so it might be expensive.

Link to this function

is_map_or_nonempty_keyword(value)

View Source
@spec is_map_or_nonempty_keyword(any()) :: boolean()

Like is_map_or_keyword/1 but returns false if the term is an empty list.

@spec isort(Enum.t()) :: Enum.t()

Sorts enum case-insensitively. Uses Enum.sort_by/3 under the hood.

@spec isort_by(Enum.t(), (any() -> any())) :: Enum.t()

Sorts enum case-insensitively by mapper function. Uses Enum.sort_by/3 under the hood.

@spec take_at(Enum.t(), integer() | [integer()] | :all) :: any()

Returns a list of elements at the given indices, in the given order. If :all is given instead of a list of indices, the entire enum is returned.

iex> Moar.Enum.take_at(["A", "B", "C"], [0, 2])
["A", "C"]

iex> Moar.Enum.take_at(["A", "B", "C"], [2, 0])
["C", "A"]

iex> Moar.Enum.take_at(["A", "B", "C"], :all)
["A", "B", "C"]
@spec tids(Enum.t()) :: list()

Returns :tid fields from enumerable.

This unusual function exists because the authors of Moar use tids (test IDs) extensively in tests.