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
type_enumerable()
Specs
type_enumerable() :: Enumerable.t()
type_pattern()
Specs
Link to this section Functions
chain(enumerables)
See Stream.concat/1
.
chain(first, second)
See Stream.concat/2
.
collect(enumerable, func)
See Enum.map/2
.
collect_concat(enumerable, func)
See Enum.flat_map/2
.
compact(enumerable)
Specs
compact(type_enumerable()) :: type_enumerable()
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}
}
cycle(enumerable, n, func)
Specs
cycle(type_enumerable(), non_neg_integer(), function()) :: Stream | type_enumerable()
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]
detect(enumerable, func)
See Enum.find/2
.
detect(enumerable, default, func)
See Enum.find/3
.
each_cons(enumerable, n, func)
Specs
each_cons(type_enumerable(), integer(), function()) :: type_enumerable()
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}
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}
each_slice(enumerable, amount)
Specs
each_slice(type_enumerable(), non_neg_integer()) :: type_enumerable() | atom()
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]]
each_slice(enumerable, start_index, amount_or_func)
Specs
each_slice(type_enumerable(), non_neg_integer(), function() | non_neg_integer()) :: atom()
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
each_with_index(enumerable)
See Enum.with_index/1
.
each_with_index(enumerable, func)
See Enum.with_index/2
.
each_with_object(enumerable, collectable, func)
See Enum.reduce/3
.
entries(enumerable)
See REnum.Ruby.to_a/1
.
find_all(enumerable, func)
See Enum.filter/2
.
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}
first(enumerable, n)
Specs
first(type_enumerable(), non_neg_integer()) :: type_enumerable()
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}]
grep(enumerable, func)
Specs
grep(type_enumerable(), function() | type_pattern()) :: type_enumerable()
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]
grep(enumerable, pattern, func)
Specs
grep(type_enumerable(), function() | type_pattern(), function()) :: type_enumerable()
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"]
grep_v(enumerable, pattern)
Specs
grep_v(type_enumerable(), function() | type_pattern()) :: type_enumerable()
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]
grep_v(enumerable, pattern, func)
Specs
grep_v(type_enumerable(), function() | type_pattern(), function()) :: type_enumerable()
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"]
include?(enumerable, element)
See Enum.member?/2
.
inject(enumerable, func)
See Enum.reduce/2
.
inject(enumerable, acc, func)
See Enum.reduce/3
.
lazy(enumerable)
Specs
lazy(type_enumerable()) :: type_enumerable()
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]
minmax(enumerable)
See Enum.min_max/1
.
minmax(enumerable, func)
See Enum.min_max/2
.
minmax_by(enumerable, func)
See Enum.min_max_by/2
.
minmax_by(enumerable, func1, func2)
See Enum.min_max_by/3
.
minmax_by(enumerable, func1, func2, func3)
See Enum.min_max_by/4
.
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
none?(enumerable, pattern_or_func)
Specs
none?(type_enumerable(), function() | type_pattern()) :: boolean()
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
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
one?(enumerable, pattern_or_func)
Specs
one?(type_enumerable(), function() | type_pattern()) :: boolean()
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
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]
select(enumerable, func)
See Enum.filter/2
.
slice_after(enumerable, func)
Specs
slice_after(type_enumerable(), function() | type_pattern()) :: type_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.
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"]]
slice_before(enumerable, func)
Specs
slice_before(type_enumerable(), function() | type_pattern()) :: type_enumerable()
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"]]
slice_when(enumerable, func)
Specs
slice_when(type_enumerable(), function() | type_pattern()) :: type_enumerable()
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]]
tally(enumerable)
See Enum.frequencies/1
.
to_a(enumerables)
See Enum.to_list/1
.
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}
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}
to_l(enumerables)
See Enum.to_list/1
.