Class: Enumerator::Lazy
- Defined in:
- lib/core/facets/enumerator/lazy/squeeze.rb,
lib/core/facets/array/indexes.rb
Instance Method Summary collapse
-
#indexes(*args) ⇒ Object
(also: #index_all)
Returns an enumerator of indexes of all object in receiver such that the object is == to obj.
-
#squeeze(*limited_to) ⇒ Object
Enumerator::Lazy version of Enumerable#squeeze.
Instance Method Details
#indexes(*args) ⇒ Object Also known as: index_all
Returns an enumerator of indexes of all object in receiver such that the object is == to obj.
If a block is given instead of an argument, returns the indexes of all objects for which the block returns true.
If neither a block nor argument is given, an Enumerator for all indexes is returned.
enum = (42 .. Float::INFINITY).lazy.indexes
#=> #<Enumerator::Lazy: ...>
enum.next #=> 0
enum.next #=> 1
enum.first(5) #=> [0, 1, 2, 3, 4]
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/core/facets/array/indexes.rb', line 97 def indexes(*args) case args.length when 0 if block_given? each_with_index.lazy.select {|el, i| yield(el) }.map {|el, i| i } else each_with_index.lazy. map {|el, i| i }.to_enum end when 1 other = args.first each_with_index.lazy. select {|el, i| el == other }.map {|el, i| i } else raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0..1)" end end |
#squeeze(*limited_to) ⇒ Object
Enumerator::Lazy version of Enumerable#squeeze. Note: you must use Ruby 2.0+ or require ‘backports’. For now, you have to require ‘facets/enumerator/lazy/squeeze’ explicitly.
Examples
[1,2,2,3,3,2,1].lazy.squeeze.to_a #=> [1,2,3,2,1]
[1,2,2,3,3,2,1].lazy.squeeze(*[3]).to_a #=> [1,2,2,3,2,1]
Returns Enumerator::Lazy.
CREDIT: T. Yamada
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/core/facets/enumerator/lazy/squeeze.rb', line 18 def squeeze(*limited_to) first = true cur = nil self.class.new(self) do |y,v| if !limited_to.empty? && !limited_to.include?(v) y << v elsif first || cur != v y << v first = false cur = v end end end |