Module: Immutable::Enumerable
- Includes:
- Enumerable
- Defined in:
- lib/immutable/enumerable.rb
Overview
Helper module for immutable-ruby’s sequential collections
Classes including ‘Immutable::Enumerable` must implement:
-
‘#each` (just like `::Enumerable`).
-
‘#select`, which takes a block, and returns an instance of the same class
with only the items for which the block returns a true value
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare with ‘other`, and return 0, 1, or -1 if it is (respectively) equal to, greater than, or less than this collection.
-
#==(other) ⇒ Boolean
Return true if ‘other` contains the same elements, in the same order.
-
#compact ⇒ Object
Return a new collection with all ‘nil` elements removed.
-
#each_index(&block) ⇒ Object
Yield all integers from 0 up to, but not including, the number of items in this collection.
-
#grep(pattern, &block) ⇒ Object
Search the collection for elements which are ‘#===` to `item`.
-
#grep_v(pattern, &block) ⇒ Object
Search the collection for elements which are not ‘#===` to `item`.
-
#group_by(&block) ⇒ Object
Groups the collection into sub-collections by the result of yielding them to the block.
-
#inspect ⇒ Object
Convert this collection to a programmer-readable ‘String` representation.
-
#join(separator = $,) ⇒ Object
Convert all the elements into strings and join them together, separated by ‘separator`.
-
#partition ⇒ Object
Return 2 collections, the first containing all the elements for which the block evaluates to true, the second containing the rest.
- #pretty_print(pp) ⇒ Object
-
#product ⇒ Object
Multiply all the items (presumably numeric) in this collection together.
-
#reject ⇒ Object
(also: #delete_if)
Return a new collection with all the elements for which the block returns false.
-
#sort_by(&block) ⇒ Object
Rubinius implements Enumerable#sort_by using Enumerable#map Because we do our own, custom implementations of #map, that doesn’t work well.
-
#sum ⇒ Object
Add up all the items (presumably numeric) in this collection.
-
#to_set ⇒ Object
Convert this collection to a Set.
Methods included from Enumerable
Instance Method Details
#<=>(other) ⇒ Object
Compare with ‘other`, and return 0, 1, or -1 if it is (respectively) equal to, greater than, or less than this collection.
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/immutable/enumerable.rb', line 93 def <=>(other) return 0 if self.equal?(other) enum1, enum2 = self.to_enum, other.to_enum loop do item1 = enum1.next item2 = enum2.next comp = (item1 <=> item2) return comp if comp != 0 end size1, size2 = self.size, other.size return 0 if size1 == size2 size1 > size2 ? 1 : -1 end |
#==(other) ⇒ Boolean
Return true if ‘other` contains the same elements, in the same order.
109 110 111 |
# File 'lib/immutable/enumerable.rb', line 109 def ==(other) self.eql?(other) || other.respond_to?(:to_ary) && to_ary.eql?(other.to_ary) end |
#compact ⇒ Object
Return a new collection with all ‘nil` elements removed.
21 22 23 |
# File 'lib/immutable/enumerable.rb', line 21 def compact select { |item| !item.nil? } end |
#each_index(&block) ⇒ Object
Yield all integers from 0 up to, but not including, the number of items in this collection. For collections which provide indexed access, these are all the valid, non-negative indices into the collection.
45 46 47 48 49 |
# File 'lib/immutable/enumerable.rb', line 45 def each_index(&block) return enum_for(:each_index) unless block_given? 0.upto(size-1, &block) self end |
#grep(pattern, &block) ⇒ Object
Search the collection for elements which are ‘#===` to `item`. Yield them to the optional code block if provided, and return them as a new collection.
27 28 29 30 31 |
# File 'lib/immutable/enumerable.rb', line 27 def grep(pattern, &block) result = select { |item| pattern === item } result = result.map(&block) if block_given? result end |
#grep_v(pattern, &block) ⇒ Object
Search the collection for elements which are not ‘#===` to `item`. Yield them to the optional code block if provided, and return them as a new collection.
36 37 38 39 40 |
# File 'lib/immutable/enumerable.rb', line 36 def grep_v(pattern, &block) result = select { |item| !(pattern === item) } result = result.map(&block) if block_given? result end |
#group_by(&block) ⇒ Object
Groups the collection into sub-collections by the result of yielding them to the block. Returns a Hash where the keys are return values from the block, and the values are sub-collections (of the same type as this one).
87 88 89 |
# File 'lib/immutable/enumerable.rb', line 87 def group_by(&block) group_by_with(self.class.empty, &block) end |
#inspect ⇒ Object
Convert this collection to a programmer-readable ‘String` representation.
132 133 134 135 136 |
# File 'lib/immutable/enumerable.rb', line 132 def inspect result = "#{self.class}[" each_with_index { |obj, i| result << ', ' if i > 0; result << obj.inspect } result << "]" end |
#join(separator = $,) ⇒ Object
Convert all the elements into strings and join them together, separated by ‘separator`. By default, the `separator` is `$,`, the global default string separator, which is normally `nil`.
116 117 118 119 120 121 122 123 124 |
# File 'lib/immutable/enumerable.rb', line 116 def join(separator = $,) result = "" if separator each_with_index { |obj, i| result << separator if i > 0; result << obj.to_s } else each { |obj| result << obj.to_s } end result end |
#partition ⇒ Object
Return 2 collections, the first containing all the elements for which the block evaluates to true, the second containing the rest.
63 64 65 66 67 |
# File 'lib/immutable/enumerable.rb', line 63 def partition return enum_for(:partition) if not block_given? a,b = super [self.class.new(a), self.class.new(b)].freeze end |
#pretty_print(pp) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/immutable/enumerable.rb', line 139 def pretty_print(pp) pp.group(1, "#{self.class}[", "]") do pp.breakable '' pp.seplist(self) { |obj| obj.pretty_print(pp) } end end |
#product ⇒ Object
Multiply all the items (presumably numeric) in this collection together.
52 53 54 |
# File 'lib/immutable/enumerable.rb', line 52 def product reduce(1, &:*) end |
#reject ⇒ Object Also known as: delete_if
Return a new collection with all the elements for which the block returns false.
14 15 16 17 |
# File 'lib/immutable/enumerable.rb', line 14 def reject return enum_for(:reject) if not block_given? select { |item| !yield(item) } end |
#sort_by(&block) ⇒ Object
Rubinius implements Enumerable#sort_by using Enumerable#map Because we do our own, custom implementations of #map, that doesn’t work well
155 156 157 158 |
# File 'lib/immutable/enumerable.rb', line 155 def sort_by(&block) result = to_a result.frozen? ? result.sort_by(&block) : result.sort_by!(&block) end |
#sum ⇒ Object
Add up all the items (presumably numeric) in this collection.
57 58 59 |
# File 'lib/immutable/enumerable.rb', line 57 def sum reduce(0, &:+) end |