REnum.ActiveSupport (REnum v0.8.0)

Summarized all of Enumerable functions in Rails.ActiveSupport. If a function with the same name already exists in Elixir, that is not implemented. Defines all of here functions when use ActiveSupport.

Link to this section Summary

Functions

Returns a new enumerable without the blank items. Uses RUtils.blank? for determining if an item is blank.

The negative of the Enum.member?. Returns +true+ if the collection does not include the object.

Returns enumerable excluded the specified elements.

Returns a list where the order has been set to that provided in the series, based on the key of the elements in the original enumerable.

Returns enumerable included the specified elements.

Converts an enumerable to a mao, using the function result or key as the key and the element as the value.

Convert an enumerable to a map, using the element as the key and the function result or given value as the value.

Returns true if the enumerable has more than 1 element.

Returns true if the enumerable has more than 1 element matched given function result or pattern.

Calculates the maximum from the extracted elements.

Calculates the minimum from the extracted elements.

Extract the given key from the first element in the enumerable.

Extract the given key from each element in the enumerable.

Returns the sole item in the enumerable. If there are no items, or more than one item, raises SoleItemExpectedError.

Link to this section Types

Link to this type

type_enumerable()

Specs

type_enumerable() :: Enumerable.t()
Link to this type

type_map_list()

Specs

type_map_list() :: [struct() | map()]
Link to this type

type_pattern()

Specs

type_pattern() :: number() | String.t() | Range.t() | Regex.t()

Link to this section Functions

Link to this function

compact_blank(enumerable)

Specs

compact_blank(type_enumerable()) :: type_enumerable()

Returns a new enumerable without the blank items. Uses RUtils.blank? for determining if an item is blank.

Examples

iex> [1, "", nil, 2, " ", [], %{}, false, true]
...> |> REnum.compact_blank()
[1, 2, true]

iex> %{a: "", b: 1, c: nil, d: [], e: false, f: true}
...> |> REnum.compact_blank()
%{
  b: 1,
  f: true
}
Link to this function

exclude?(enumerable, element)

Specs

exclude?(type_enumerable(), any()) :: boolean()

The negative of the Enum.member?. Returns +true+ if the collection does not include the object.

Examples

iex> REnum.exclude?([2], 1)
true

iex> REnum.exclude?([2], 2)
false
Link to this function

excluding(enumerable, elements)

Specs

Returns enumerable excluded the specified elements.

Examples

iex> REnum.excluding(1..5, [1, 5])
[2, 3, 4]

iex> REnum.excluding(%{foo: 1, bar: 2, baz: 3}, [:bar])
%{foo: 1, baz: 3}
Link to this function

in_order_of(enumerable, key, series)

Specs

in_order_of(type_map_list(), atom(), list()) :: list()

Returns a list where the order has been set to that provided in the series, based on the key of the elements in the original enumerable.

Examples

iex> payments = [
...>   %Payment{dollars: 5, cents: 99},
...>   %Payment{dollars: 10, cents: 0},
...>   %Payment{dollars: 0, cents: 5}
...> ]
iex> REnum.in_order_of(payments, :cents, [0, 5])
[
  %Payment{cents: 0, dollars: 10},
  %Payment{cents: 5, dollars: 0}
]
Link to this function

including(enumerable, elements)

Specs

including(type_enumerable(), type_enumerable()) :: list()

Returns enumerable included the specified elements.

Examples

iex> REnum.including([1, 2, 3], [4, 5])
[1, 2, 3, 4, 5]

iex> REnum.including(1..3, 4..6)
[1, 2, 3, 4, 5, 6]

iex> REnum.including(%{foo: 1, bar: 2, baz: 3}, %{hoge: 4, page: 5})
[
  {:bar, 2},
  {:baz, 3},
  {:foo, 1},
  {:hoge, 4},
  {:page, 5}
]
Link to this function

index_by(enumerable, key)

Specs

index_by(type_map_list(), function() | atom()) :: map()

Converts an enumerable to a mao, using the function result or key as the key and the element as the value.

Examples

iex> payments = [
...>   %Payment{dollars: 5, cents: 99},
...>   %Payment{dollars: 10, cents: 0},
...>   %Payment{dollars: 0, cents: 5}
...> ]
iex> REnum.index_by(payments, fn el -> el.cents end)
%{
  0 => %Payment{cents: 0, dollars: 10},
  5 => %Payment{cents: 5, dollars: 0},
  99 => %Payment{cents: 99, dollars: 5}
}
iex> REnum.index_by(payments, :cents)
%{
  0 => %Payment{cents: 0, dollars: 10},
  5 => %Payment{cents: 5, dollars: 0},
  99 => %Payment{cents: 99, dollars: 5}
}
Link to this function

index_with(keys, func)

Specs

index_with([any()], function()) :: map()

Convert an enumerable to a map, using the element as the key and the function result or given value as the value.

Examples

iex> payments = [
...>   %Payment{dollars: 5, cents: 99},
...>   %Payment{dollars: 10, cents: 0},
...>   %Payment{dollars: 0, cents: 5}
...> ]
iex> REnum.index_with(payments, fn el -> el.cents end)
%{
  %Payment{cents: 0, dollars: 10} => 0,
  %Payment{cents: 5, dollars: 0} => 5,
  %Payment{cents: 99, dollars: 5} => 99
}

iex> REnum.index_with(~w(a b c), 3)
%{"a" => 3, "b" => 3, "c" => 3}
Link to this function

many?(enumerable)

Specs

many?(type_enumerable()) :: boolean()

Returns true if the enumerable has more than 1 element.

Examples

iex>  REnum.many?([])
false

iex> REnum.many?([1])
false

iex> REnum.many?([1, 2])
true

iex>  REnum.many?(%{})
false

iex> REnum.many?(%{a: 1})
false

iex> REnum.many?(%{a: 1, b: 2})
true
Link to this function

many?(enumerable, pattern_or_func)

Specs

Returns true if the enumerable has more than 1 element matched given function result or pattern.

Examples

iex>  REnum.many?([1, 2, 3], &(&1 < 2))
false

iex> REnum.many?([1, 2, 3], &(&1 < 3))
true

iex> REnum.many?(["bar", "baz", "foo"], "bar")
false

iex>   REnum.many?(["bar", "baz", "foo"], ~r/a/)
true
Link to this function

maximum(map_list, key)

Specs

maximum(type_map_list(), atom()) :: any()

Calculates the maximum from the extracted elements.

Examples

iex> payments = [
...>   %Payment{dollars: 5, cents: 99},
...>   %Payment{dollars: 10, cents: 0},
...>   %Payment{dollars: 0, cents: 5}
...> ]
iex> REnum.maximum(payments, :cents)
99
iex> REnum.maximum(payments, :dollars)
10
iex> REnum.maximum([], :dollars)
nil
Link to this function

minimum(map_list, key)

Specs

minimum(type_map_list(), atom()) :: any()

Calculates the minimum from the extracted elements.

Examples

iex> payments = [
...>   %Payment{dollars: 5, cents: 99},
...>   %Payment{dollars: 10, cents: 0},
...>   %Payment{dollars: 0, cents: 5}
...> ]
iex> REnum.minimum(payments, :cents)
0
iex> REnum.minimum(payments, :dollars)
0
iex> REnum.minimum([], :dollars)
nil
Link to this function

pick(map_list, keys)

Specs

pick(type_map_list(), [atom()] | atom()) :: any()

Extract the given key from the first element in the enumerable.

Examples

iex> payments = [
...>   %Payment{dollars: 5, cents: 99},
...>   %Payment{dollars: 10, cents: 0},
...>   %Payment{dollars: 0, cents: 5}
...> ]
iex> REnum.pick(payments, [:dollars, :cents])
[5, 99]
iex> REnum.pick(payments, :dollars)
5
iex> REnum.pick([], :dollars)
nil
Link to this function

pluck(map_list, keys)

Specs

pluck(type_map_list(), [atom()] | atom()) :: [any()]

Extract the given key from each element in the enumerable.

Examples

iex> payments = [
...>   %Payment{dollars: 5, cents: 99},
...>   %Payment{dollars: 10, cents: 0},
...>   %Payment{dollars: 0, cents: 5}
...> ]
iex> REnum.pluck(payments, [:dollars, :cents])
[[5, 99], [10, 0], [0, 5]]
iex> REnum.pluck(payments, :dollars)
[5, 10, 0]
iex> REnum.pluck([], :dollars)
[]
Link to this function

sole(enumerable)

Specs

sole(type_enumerable()) :: boolean()

Returns the sole item in the enumerable. If there are no items, or more than one item, raises SoleItemExpectedError.

Examples

iex> REnum.sole([1])
1

iex> REnum.sole([])
** (SoleItemExpectedError) no item found
Link to this function

without(enumerable, elements)

See REnum.ActiveSupport.excluding/2.