REnum.Ruby (REnum v0.8.0)

Summarized all of Ruby's Enumerable functions. If a function with the same name already exists in Elixir, that is not implemented. Also, the function that returns Enumerator in Ruby is customized each behavior on the characteristics. Defines all of here functions when use REnum.Ruby.

Link to this section Summary

Functions

Returns an list of all non-nil elements.

When called with positive integer argument n and a function, calls the block with each element, then does so again, until it has done so n times; returns given enumerable When called with a function and n is nil, returns Stream cycled forever.

Calls the function with each successive overlapped n-list of elements; returns given enumerable.

Calls the given function with each element, returns given enumerable

Returns Stream given enumerable sliced by each amount.

Calls the given function with each element, returns given enumerable.

Returns the first element.

Returns leading elements.

Returns elements selected by a given pattern or function.

Calls the function with each matching element and returned.

Returns elements rejected by a given pattern or function.

Calls the function with each unmatching element and returned.

Returns Stream, which redefines most Enumerable functions to postpone enumeration and enumerate values only on an as-needed basis.

Returns true if enumerable does not include truthy value; false otherwise.

Returns whether no element meets a given criterion.

Return true if enumerable has only one truthy element; false otherwise.

Returns true if exactly one element meets a specified criterion; false otherwise.

Calls the function with each element, but in reverse order; returns given enumerable.

With argument pattern, returns an elements that uses the pattern to partition elements into lists (“slices”). An element ends the current slice if element matches pattern. With a function, returns an elements that uses the function to partition elements into list. An element ends the current slice if its function return is a truthy value.

With argument pattern, returns an elements that uses the pattern to partition elements into lists (“slices”). An element begins a new slice if element matches pattern. (or if it is the first element). With a function, returns an elements that uses the function to partition elements into list. An element ends the current slice if its function return is a truthy value.

The returned elements uses the function to partition elements into lists (“slices”). It calls the function with each element and its successor. Begins a new slice if and only if the function returns a truthy value. &1 is current_element and &2 is next_element in function arguments.

Returns a map each of whose entries is the key-value pair formed from one of those list.

The function is called with each element. The function should return a 2-element tuple which becomes a key-value pair in the returned map.

Link to this section Types

Link to this type

type_enumerable()

Specs

type_enumerable() :: Enumerable.t()
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

chain(enumerables)

See Stream.concat/1.

Link to this function

chain(first, second)

See Stream.concat/2.

Link to this function

collect(enumerable, func)

See Enum.map/2.

Link to this function

collect_concat(enumerable, func)

See Enum.flat_map/2.

Link to this function

compact(enumerable)

Specs

Returns an list of all non-nil elements.

Examples

iex> REnum.compact([1, nil, 2, 3])
[1, 2, 3]

iex> REnum.compact(%{
...>        :truthy => true,
...>        false => false,
...>        nil => nil,
...>        :map => %{key: :value}
...>      })
%{
  :truthy => true,
  false => false,
  :map => %{key: :value}
}
Link to this function

cycle(enumerable, n, func)

Specs

When called with positive integer argument n and a function, calls the block with each element, then does so again, until it has done so n times; returns given enumerable When called with a function and n is nil, returns Stream cycled forever.

Examples

iex> REnum.cycle(["a", "b"], 2, &IO.puts(&1))
# a
# b
# a
# b
["a", "b"]

iex> REnum.cycle(%{a: 1, b: 2}, nil, &IO.inspect(&1)) |> Enum.take(2)
# {:a, 1}
# {:b, 2}
# {:a, 1}
# {:b, 2}
[:ok, :ok]
Link to this function

detect(enumerable, func)

See Enum.find/2.

Link to this function

detect(enumerable, default, func)

See Enum.find/3.

Link to this function

each_cons(enumerable, n, func)

Specs

Calls the function with each successive overlapped n-list of elements; returns given enumerable.

Examples

iex> ["a", "b", "c", "d", "e"]
iex> |> REnum.each_cons(3, &IO.inspect(&1))
# ["a", "b", "c"]
# ["b", "c", "d"]
# ["c", "d", "e"]
["a", "b", "c", "d", "e"]

iex> %{a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}
iex> |> REnum.each_cons(4, &IO.inspect(&1))
# [a: 1, b: 2, c: 3, d: 4]
# [b: 2, c: 3, d: 4, e: 5]
# [c: 3, d: 4, e: 5, f: 6]
%{a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}
Link to this function

each_entry(enumerable, func)

Specs

each_entry(type_enumerable(), function()) :: type_enumerable()

Calls the given function with each element, returns given enumerable:

Examples

iex> ["a", "b", "c"]
iex> |> REnum.each_entry(&IO.inspect(&1))
# "a"
# "b"
# "c"
["a", "b", "c"]

iex> %{a: 1, b: 2}
iex> |> REnum.each_entry(&IO.inspect(&1))
# {:a, 1}
# {:b, 2}
%{a: 1, b: 2}
Link to this function

each_slice(enumerable, amount)

Specs

Returns Stream given enumerable sliced by each amount.

Examples

iex> ["a", "b", "c", "d", "e"]
iex> |> REnum.each_slice(2)
iex> |> Enum.to_list()
[["a", "b"], ["c", "d"], ["e"]]

iex> %{a: 1, b: 2, c: 3}
iex> |> REnum.each_slice(2)
iex> |> Enum.to_list()
[[a: 1, b: 2], [c: 3]]
Link to this function

each_slice(enumerable, start_index, amount_or_func)

Specs

Calls the given function with each element, returns given enumerable.

Examples

iex> ["a", "b", "c", "d", "e"]
iex> |> REnum.each_slice(2, &IO.inspect(&1))
# ["a", "b"]
# ["c", "d"]
# ["e"]
:ok

iex> %{a: 1, b: 2, c: 3}
iex> |> REnum.each_slice(2, &IO.inspect(&1))
# [a: 1, b: 2]
# [c: 3]
:ok
Link to this function

each_with_index(enumerable)

See Enum.with_index/1.

Link to this function

each_with_index(enumerable, func)

See Enum.with_index/2.

Link to this function

each_with_object(enumerable, collectable, func)

See Enum.reduce/3.

Link to this function

entries(enumerable)

See REnum.Ruby.to_a/1.

Link to this function

find_all(enumerable, func)

See Enum.filter/2.

Link to this function

first(enumerable)

Specs

first(type_enumerable()) :: any()

Returns the first element.

Examples

iex> REnum.first([1, 2, 3])
1

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

first(enumerable, n)

Specs

Returns leading elements.

Examples

iex> REnum.first([1, 2, 3], 2)
[1, 2]

iex> REnum.first(%{a: 1, b: 2}, 2)
[{:a, 1}, {:b, 2}]
Link to this function

grep(enumerable, func)

Specs

Returns elements selected by a given pattern or function.

Examples

iex> ["foo", "bar", "car", "moo"]
iex> |> REnum.grep(~r/ar/)
["bar", "car"]

iex> 1..10
iex> |> REnum.grep(3..8)
[3, 4, 5, 6, 7, 8]
Link to this function

grep(enumerable, pattern, func)

Specs

Calls the function with each matching element and returned.

Examples

iex> ["foo", "bar", "car", "moo"]
iex> |> REnum.grep(~r/ar/, &String.upcase(&1))
["BAR", "CAR"]

iex> 1..10
iex> |> REnum.grep(3..8, &to_string(&1))
["3", "4", "5", "6", "7", "8"]
Link to this function

grep_v(enumerable, pattern)

Specs

Returns elements rejected by a given pattern or function.

Examples

iex> ["foo", "bar", "car", "moo"]
iex> |> REnum.grep_v(~r/ar/)
["foo", "moo"]

iex> 1..10
iex> |> REnum.grep_v(3..8)
[1, 2, 9, 10]
Link to this function

grep_v(enumerable, pattern, func)

Specs

Calls the function with each unmatching element and returned.

Examples

iex> ["foo", "bar", "car", "moo"]
iex> |> REnum.grep_v(~r/ar/, &String.upcase(&1))
["FOO", "MOO"]

iex> 1..10
iex> |> REnum.grep_v(3..8, &to_string(&1))
["1", "2", "9", "10"]
Link to this function

include?(enumerable, element)

See Enum.member?/2.

Link to this function

inject(enumerable, func)

See Enum.reduce/2.

Link to this function

inject(enumerable, acc, func)

See Enum.reduce/3.

Link to this function

lazy(enumerable)

Specs

Returns Stream, which redefines most Enumerable functions to postpone enumeration and enumerate values only on an as-needed basis.

Examples

iex> [1, 2, 3]
iex> |> REnum.lazy()
iex> |> REnum.to_list()
[1, 2, 3]
Link to this function

minmax(enumerable)

See Enum.min_max/1.

Link to this function

minmax(enumerable, func)

See Enum.min_max/2.

Link to this function

minmax_by(enumerable, func)

See Enum.min_max_by/2.

Link to this function

minmax_by(enumerable, func1, func2)

See Enum.min_max_by/3.

Link to this function

minmax_by(enumerable, func1, func2, func3)

See Enum.min_max_by/4.

Link to this function

none?(enumerable)

Specs

none?(type_enumerable()) :: boolean()

Returns true if enumerable does not include truthy value; false otherwise.

Examples

iex> REnum.none?(1..4)
false

iex> REnum.none?([nil, false])
true

iex> REnum.none?([foo: 0, bar: 1])
false
Link to this function

none?(enumerable, pattern_or_func)

Specs

Returns whether no element meets a given criterion.

Examples

iex> REnum.none?(1..4, &(&1 < 1))
true

iex> REnum.none?(%{foo: 0, bar: 1, baz: 2}, fn {_, v} -> v < 0 end)
true

iex> REnum.none?(1..4, 5)
true

iex> REnum.none?(1..4, 2..3)
false
Link to this function

one?(enumerable)

Specs

one?(type_enumerable()) :: boolean()

Return true if enumerable has only one truthy element; false otherwise.

Examples

iex> REnum.one?([1, nil, false])
true

iex> REnum.one?(1..4)
false
Link to this function

one?(enumerable, pattern_or_func)

Specs

Returns true if exactly one element meets a specified criterion; false otherwise.

Examples

iex> REnum.one?(1..4, 1..2)
false

iex> REnum.one?(1..4, &(&1 < 2))
true

iex> REnum.one?(1..4, 1)
true
Link to this function

reverse_each(enumerable, func)

Specs

reverse_each(type_enumerable(), function()) :: type_enumerable()

Calls the function with each element, but in reverse order; returns given enumerable.

Examples

iex> REnum.reverse_each([1, 2, 3], &IO.inspect(&1))
# 3
# 2
# 1
[1, 2, 3]
Link to this function

select(enumerable, func)

See Enum.filter/2.

Link to this function

slice_after(enumerable, func)

Specs

With argument pattern, returns an elements that uses the pattern to partition elements into lists (“slices”). An element ends the current slice if element matches pattern. With a function, returns an elements that uses the function to partition elements into list. An element ends the current slice if its function return is a truthy value.

Examples

iex> [0, 2, 4, 1, 2, 4, 5, 3, 1, 4, 2]
iex> |> REnum.slice_after(&(rem(&1, 2) == 0))
[[0], [2], [4], [1, 2], [4], [5, 3, 1, 4], [2]]

iex> ["a", "b", "c"]
iex> |> REnum.slice_after(~r/b/)
[["a", "b"], ["c"]]
Link to this function

slice_before(enumerable, func)

Specs

With argument pattern, returns an elements that uses the pattern to partition elements into lists (“slices”). An element begins a new slice if element matches pattern. (or if it is the first element). With a function, returns an elements that uses the function to partition elements into list. An element ends the current slice if its function return is a truthy value.

Examples

iex> [0, 2, 4, 1, 2, 4, 5, 3, 1, 4, 2]
iex> |> REnum.slice_before(&(rem(&1, 2) == 0))
[[0], [2], [4, 1], [2], [4, 5, 3, 1], [4], [2]]

iex> ["a", "b", "c"]
iex> |> REnum.slice_before(~r/b/)
[["a"], ["b", "c"]]
Link to this function

slice_when(enumerable, func)

Specs

The returned elements uses the function to partition elements into lists (“slices”). It calls the function with each element and its successor. Begins a new slice if and only if the function returns a truthy value. &1 is current_element and &2 is next_element in function arguments.

Examples

iex> [1, 2, 4, 9, 10, 11, 12, 15, 16, 19, 20, 21]
iex> |> REnum.slice_when(&(&1 + 1 != &2))
[[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]]
Link to this function

tally(enumerable)

See Enum.frequencies/1.

Link to this function

to_a(enumerables)

See Enum.to_list/1.

Link to this function

to_h(enumerable)

Specs

to_h(type_enumerable()) :: map()

Returns a map each of whose entries is the key-value pair formed from one of those list.

Examples

iex> REnum.to_h([[:a, 1], [:b, 2]])
%{a: 1, b: 2}

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

to_h(enumerable, func)

Specs

to_h(type_enumerable(), function()) :: map()

The function is called with each element. The function should return a 2-element tuple which becomes a key-value pair in the returned map.

Examples

iex> REnum.to_h([[:a, 1], [:b, 2]], fn el ->
...>     {REnum.at(el, 0), REnum.at(el, 1)}
...>   end)
%{a: 1, b: 2}

iex>  REnum.to_h(%{a: 1, b: 2}, fn {key, value} -> {key, value * 2} end)
%{a: 2, b: 4}
Link to this function

to_l(enumerables)

See Enum.to_list/1.