mrbgems/mruby-enum-chain/README.md
This mrbgem provides the Enumerator::Chain class, which allows you to chain multiple enumerable objects together, treating them as a single, continuous enumerable.
This is useful when you have multiple collections (e.g., arrays, ranges, or other enumerators) and you want to iterate over all of them sequentially without first concatenating them into a single, larger collection.
There are three main ways to create an Enumerator::Chain instance:
Enumerable#chainYou can call the chain method on any object that includes the Enumerable module.
a = [1, 2, 3]
b = (4..6)
c = {foo: 7, bar: 8}.each_key # Enumerator for keys
chained_enum = a.chain(b, c)
chained_enum.to_a # => [1, 2, 3, 4, 5, 6, :foo, :bar]
Enumerator#+You can use the + operator on an Enumerator instance to chain it with another enumerable object.
enum1 = [1, 2].each
enum2 = %w(a b).each
chained_enum = enum1 + enum2
chained_enum.to_a # => [1, 2, "a", "b"]
# You can chain multiple times
enum3 = (10..11).each
chained_enum_2 = enum1 + enum2 + enum3
chained_enum_2.to_a # => [1, 2, "a", "b", 10, 11]
Enumerator::Chain.newYou can directly instantiate Enumerator::Chain by passing enumerable objects to its constructor.
arr = [10, 20]
rng = (30..31)
chained_enum = Enumerator::Chain.new(arr, rng)
chained_enum.to_a # => [10, 20, 30, 31]
each(&block): Iterates through each element of the chained enumerables in the order they were added. Returns an enumerator if no block is given.
size: Returns the total number of elements in all chained enumerables. If any of the chained enumerables do not respond to size (e.g., an infinite enumerator or one with an unknown size), this method will return nil.
([1, 2].chain([3, 4])).size # => 4
([1, 2].chain( (1..Float::INFINITY) )).size # => nil
rewind: Rewinds all of the chained enumerables that respond to the rewind method. This resets the iteration state to the beginning.
e = [1,2].chain(3..4)
e.next # => 1
e.next # => 2
e.next # => 3
e.rewind
e.next # => 1
+(other): Creates a new Enumerator::Chain by appending another enumerable to the current chain.
MIT License