Module: Enumerable
- Included in:
- Array, Dir, Enumerator, Enumerator::Chain, Enumerator::Generator, Hash, Range, Set, Struct
- Defined in:
- mrblib/enum.rb,
mrbgems/mruby-enum-ext/mrblib/enum.rb,
mrbgems/mruby-enum-lazy/mrblib/lazy.rb,
mrbgems/mruby-enum-chain/mrblib/chain.rb,
mrbgems/mruby-enumerator/mrblib/enumerator.rb
Overview
chain.rb Enumerator::Chain class See Copyright Notice in mruby.h
Constant Summary collapse
- NONE =
Object.new
Instance Method Summary collapse
-
#all?(pat = NONE, &block) ⇒ Boolean
ISO 15.3.2.2.1 call-seq: enum.all? [{ |obj| block } ] -> true or false enum.all?(pattern) -> true or false.
-
#any?(pat = NONE, &block) ⇒ Boolean
ISO 15.3.2.2.2 call-seq: enum.any? [{ |obj| block }] -> true or false enum.any?(pattern) -> true or false.
- #chain(*args) ⇒ Object
-
#chunk(&block) ⇒ Object
call-seq: enum.chunk -> enumerator enum.chunk { |arr| block } -> enumerator.
-
#chunk_while(&block) ⇒ Object
call-seq: enum.chunk_while {|elt_before, elt_after| bool } -> an_enumerator.
-
#collect(&block) ⇒ Object
(also: #map)
Call the given block for each element which is yield by
each. -
#count(v = NONE, &block) ⇒ Object
call-seq: enum.count -> int enum.count(item) -> int enum.count { |obj| block } -> int.
-
#cycle(nv = nil, &block) ⇒ Object
call-seq: enum.cycle(n=nil) { |obj| block } -> nil enum.cycle(n=nil) -> an_enumerator.
-
#detect(ifnone = nil, &block) ⇒ Object
(also: #find)
Return the first element for which value from the block is true.
-
#drop(n) ⇒ Object
call-seq: enum.drop(n) -> array.
-
#drop_while(&block) ⇒ Object
call-seq: enum.drop_while {|arr| block } -> array enum.drop_while -> an_enumerator.
-
#each_cons(n, &block) ⇒ nil
Iterates the given block for each array of consecutive
elements. -
#each_entry(*args, &blk) ⇒ Object
call-seq: enum.each_entry { |obj| block } -> enum enum.each_entry -> an_enumerator.
-
#each_slice(n, &block) ⇒ nil
Iterates the given block for each slice of
elements. -
#each_with_index(&block) ⇒ Object
Call the given block for each element which is yield by
each. -
#each_with_object(obj, &block) ⇒ Object
call-seq: enum.each_with_object(obj) { |(*args), memo_obj| ... } -> obj enum.each_with_object(obj) -> an_enumerator.
-
#entries ⇒ Object
(also: #to_a)
Return an array of all elements which are yield by
each. - #filter_map(&blk) ⇒ Object
-
#find_all(&block) ⇒ Object
(also: #select)
Call the given block for each element which is yield by
each. -
#find_index(val = NONE, &block) ⇒ Object
call-seq: enum.find_index(value) -> int or nil enum.find_index { |obj| block } -> int or nil enum.find_index -> an_enumerator.
-
#first(*args) ⇒ Object
call-seq: enum.first -> obj or nil enum.first(n) -> an_array.
-
#flat_map(&block) ⇒ Object
(also: #collect_concat)
call-seq: enum.flat_map { |obj| block } -> array enum.collect_concat { |obj| block } -> array enum.flat_map -> an_enumerator enum.collect_concat -> an_enumerator.
-
#grep(pattern, &block) ⇒ Object
Call the given block for each element which is yield by
eachand which return value was true when invoking === withpattern. - #grep_v(pattern, &block) ⇒ Object
-
#group_by(&block) ⇒ Object
call-seq: enum.group_by {| obj | block } -> a_hash enum.group_by -> an_enumerator.
-
#hash ⇒ Object
redefine #hash 15.3.1.3.15.
-
#include?(obj) ⇒ Boolean
(also: #member?)
Return true if at least one element which is yield by
eachreturns a true value by invoking == withobj. -
#inject(*args, &block) ⇒ Object
(also: #reduce)
Call the given block for each element which is yield by
each. -
#lazy ⇒ Object
Enumerable#lazy implementation.
-
#max(&block) ⇒ Object
Return the maximum value of all elements yield by
each. -
#max_by(&block) ⇒ Object
call-seq: enum.max_by {|obj| block } -> obj enum.max_by -> an_enumerator.
-
#min(&block) ⇒ Object
Return the minimum value of all elements yield by
each. -
#min_by(&block) ⇒ Object
call-seq: enum.min_by {|obj| block } -> obj enum.min_by -> an_enumerator.
-
#minmax(&block) ⇒ Object
call-seq: enum.minmax -> [min, max] enum.minmax { |a, b| block } -> [min, max].
-
#minmax_by(&block) ⇒ Object
call-seq: enum.minmax_by { |obj| block } -> [min, max] enum.minmax_by -> an_enumerator.
-
#none?(pat = NONE, &block) ⇒ Boolean
call-seq: enum.none? [{ |obj| block }] -> true or false enum.none?(pattern) -> true or false.
-
#one?(pat = NONE, &block) ⇒ Boolean
call-seq: enum.one? [{ |obj| block }] -> true or false enum.one?(pattern) -> true or false.
-
#partition(&block) ⇒ Object
Call the given block for each element which is yield by
each. -
#reject(&block) ⇒ Object
Call the given block for each element which is yield by
each. -
#reverse_each(&block) ⇒ Object
call-seq: enum.reverse_each { |item| block } -> enum enum.reverse_each -> an_enumerator.
-
#sort(&block) ⇒ Object
Return a sorted array of all elements which are yield by
each. -
#sort_by(&block) ⇒ Object
call-seq: enum.sort_by { |obj| block } -> array enum.sort_by -> an_enumerator.
-
#sum(init = 0, &block) ⇒ Object
call-seq: enum.sum(count=1) -> numeric enum.sum(count=1)... -> numeric.
-
#take(n) ⇒ Object
call-seq: enum.take(n) -> array.
-
#take_while(&block) ⇒ Object
call-seq: enum.take_while {|arr| block } -> array enum.take_while -> an_enumerator.
-
#tally ⇒ Object
call-seq: enum.tally -> a_hash.
-
#to_h(&blk) ⇒ Object
call-seq: enum.to_h -> hash.
- #uniq(&block) ⇒ Object
-
#zip(*args, &block) ⇒ Object
use Enumerator to use infinite sequence.
Instance Method Details
#all?(pat = NONE, &block) ⇒ Boolean
ISO 15.3.2.2.1 call-seq: enum.all? [{ |obj| block } ] -> true or false enum.all?(pattern) -> true or false
Passes each element of the collection to the given block. The method
returns true if the block never returns
false or nil. If the block is not given,
Ruby adds an implicit block of { |obj| obj } which will
cause #all? to return true when none of the collection members are
false or nil.
If a pattern is supplied instead, the method returns whether
pattern === element for every collection member.
%w[ant bear cat].all? { |word| word.length >= 3 } #=> true
%w[ant bear cat].all? { |word| word.length >= 4 } #=> false
%w[ant bear cat].all?(/t/) #=> false
[1, 2i, 3.14].all?(Numeric) #=> true
[nil, true, 99].all? #=> false
26 27 28 29 30 31 32 33 |
# File 'mrblib/enum.rb', line 26 def all?(&block) if block self.each{|*val| return false unless block.call(*val)} else self.each{|*val| return false unless val.__svalue} end true end |
#any?(pat = NONE, &block) ⇒ Boolean
ISO 15.3.2.2.2 call-seq: enum.any? [{ |obj| block }] -> true or false enum.any?(pattern) -> true or false
Passes each element of the collection to the given block. The method
returns true if the block ever returns a value other
than false or nil. If the block is not
given, Ruby adds an implicit block of { |obj| obj } that
will cause #any? to return true if at least one of the collection
members is not false or nil.
If a pattern is supplied instead, the method returns whether
pattern === element for any collection member.
%w[ant bear cat].any? { |word| word.length >= 3 } #=> true
%w[ant bear cat].any? { |word| word.length >= 4 } #=> true
%w[ant bear cat].any?(/d/) #=> false
[nil, true, 99].any?(Integer) #=> true
[nil, true, 99].any? #=> true
[].any? #=> false
43 44 45 46 47 48 49 50 |
# File 'mrblib/enum.rb', line 43 def any?(&block) if block self.each{|*val| return true if block.call(*val)} else self.each{|*val| return true if val.__svalue} end false end |
#chain(*args) ⇒ Object
6 7 8 |
# File 'mrbgems/mruby-enum-chain/mrblib/chain.rb', line 6 def chain(*args) Enumerator::Chain.new(self, *args) end |
#chunk(&block) ⇒ Object
call-seq: enum.chunk -> enumerator enum.chunk { |arr| block } -> enumerator
Each element in the returned enumerator is a 2-element array consisting of:
- A value returned by the block.
- An array ("chunk") containing the element for which that value was returned, and all following elements for which the block returned the same value:
So that:
- Each block return value that is different from its predecessor begins a new chunk.
- Each block return value that is the same as its predecessor continues the same chunk.
Example:
e = (0..10).chunk {|i| (i / 3).floor } # => #<Enumerator: ...>
# The enumerator elements.
e.next # => [0, [0, 1, 2]]
e.next # => [1, [3, 4, 5]]
e.next # => [2, [6, 7, 8]]
e.next # => [3, [9, 10]]
You can use the special symbol :_alone to force an element into its own separate chuck:
a = [0, 0, 1, 1]
e = a.chunk{|i| i.even? ? :_alone : true }
e.to_a # => [[:_alone, [0]], [:_alone, [0]], [true, [1, 1]]]
You can use the special symbol :_separator or nil
to force an element to be ignored (not included in any chunk):
a = [0, 0, -1, 1, 1]
e = a.chunk{|i| i < 0 ? :_separator : true }
e.to_a # => [[true, [0, 0]], [true, [1, 1]]]
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 |
# File 'mrbgems/mruby-enumerator/mrblib/enumerator.rb', line 745 def chunk(&block) return to_enum :chunk unless block enum = self Enumerator.new do |y| last_value, arr = nil, [] enum.each do |element| value = block.call(element) case value when :_alone y.yield [last_value, arr] if arr.size > 0 y.yield [value, [element]] last_value, arr = nil, [] when :_separator, nil y.yield [last_value, arr] if arr.size > 0 last_value, arr = nil, [] when last_value arr << element else raise 'symbols beginning with an underscore are reserved' if value.is_a?(Symbol) && value.to_s[0] == '_' y.yield [last_value, arr] if arr.size > 0 last_value, arr = value, [element] end end y.yield [last_value, arr] if arr.size > 0 end end |
#chunk_while(&block) ⇒ Object
call-seq: enum.chunk_while {|elt_before, elt_after| bool } -> an_enumerator
Creates an enumerator for each chunked elements. The beginnings of chunks are defined by the block.
This method splits each chunk using adjacent elements,
elt_before and elt_after,
in the receiver enumerator.
This method split chunks between elt_before and elt_after where
the block returns false.
The block is called the length of the receiver enumerator minus one.
The result enumerator yields the chunked elements as an array.
So each method can be called as follows:
enum.chunk_while { |elt_before, elt_after| bool }.each { |ary| ... }
Other methods of the Enumerator class and Enumerable module,
such as to_a, map, etc., are also usable.
For example, one-by-one increasing subsequence can be chunked as follows:
a = [1,2,4,9,10,11,12,15,16,19,20,21]
b = a.chunk_while {|i, j| i+1 == j }
p b.to_a #=> [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]]
c = b.map {|a| a.length < 3 ? a : "#{a.first}-#{a.last}" }
p c #=> [[1, 2], [4], "9-12", [15, 16], "19-21"]
d = c.join(",")
p d #=> "1,2,4,9-12,15,16,19-21"
Increasing (non-decreasing) subsequence can be chunked as follows:
a = [0, 9, 2, 2, 3, 2, 7, 5, 9, 5]
p a.chunk_while {|i, j| i <= j }.to_a
#=> [[0, 9], [2, 2, 3], [2, 7], [5, 9], [5]]
Adjacent evens and odds can be chunked as follows: (Enumerable#chunk is another way to do it.)
a = [7, 5, 9, 2, 0, 7, 9, 4, 2, 0]
p a.chunk_while {|i, j| i.even? == j.even? }.to_a
#=> [[7, 5, 9], [2, 0], [7, 9], [4, 2, 0]]
Enumerable#slice_when does the same, except splitting when the block
returns true instead of false.
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 |
# File 'mrbgems/mruby-enumerator/mrblib/enumerator.rb', line 823 def chunk_while(&block) enum = self Enumerator.new do |y| n = 0 last_value, arr = nil, [] enum.each do |element| if n > 0 unless block.call(last_value, element) y.yield arr arr = [] end end arr.push(element) n += 1 last_value = element end y.yield arr if arr.size > 0 end end |
#collect(&block) ⇒ Object Also known as: map
Call the given block for each element
which is yield by each. Append all
values of each block together and
return this value.
ISO 15.3.2.2.3
59 60 61 62 63 64 65 |
# File 'mrblib/enum.rb', line 59 def collect(&block) return to_enum :collect unless block ary = [] self.each{|*val| ary.push(block.call(*val))} ary end |
#count(v = NONE, &block) ⇒ Object
call-seq: enum.count -> int enum.count(item) -> int enum.count { |obj| block } -> int
Returns the number of items in enum through enumeration.
If an argument is given, the number of items in enum that
are equal to item are counted. If a block is given, it
counts the number of elements yielding a true value.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 238 def count(v=NONE, &block) count = 0 if block self.each do |*val| count += 1 if block.call(*val) end else if NONE.equal?(v) self.each { count += 1 } else self.each do |*val| count += 1 if val.__svalue == v end end end count end |
#cycle(nv = nil, &block) ⇒ Object
call-seq: enum.cycle(n=nil) { |obj| block } -> nil enum.cycle(n=nil) -> an_enumerator
Calls block for each element of enum repeatedly n
times or forever if none or nil is given. If a non-positive
number is given or the collection is empty, does nothing. Returns
nil if the loop has finished without getting interrupted.
Enumerable#cycle saves elements in an internal array so changes to enum after the first pass have no effect.
If no block is given, an enumerator is returned instead.
a = ["a", "b", "c"]
a.cycle { |x| puts x } # print, a, b, c, a, b, c,.. forever.
a.cycle(2) { |x| puts x } # print, a, b, c, a, b, c.
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 653 def cycle(nv = nil, &block) return to_enum(:cycle, nv) unless block n = nil if nv.nil? n = -1 else n = nv.__to_int return nil if n <= 0 end ary = [] each do |*i| ary.push(i) yield(*i) end return nil if ary.empty? while n < 0 || 0 < (n -= 1) ary.each do |i| yield(*i) end end nil end |
#detect(ifnone = nil, &block) ⇒ Object Also known as: find
Return the first element for which
value from the block is true. If no
object matches, calls ifnone and
returns its result. Otherwise returns
nil.
ISO 15.3.2.2.4
75 76 77 78 79 80 81 82 83 84 |
# File 'mrblib/enum.rb', line 75 def detect(ifnone=nil, &block) return to_enum :detect, ifnone unless block self.each{|*val| if block.call(*val) return val.__svalue end } ifnone.call unless ifnone.nil? end |
#drop(n) ⇒ Object
call-seq: enum.drop(n) -> array
Drops first n elements from enum, and returns rest elements in an array.
a = [1, 2, 3, 4, 5, 0]
a.drop(3) #=> [4, 5, 0]
15 16 17 18 19 20 21 22 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 15 def drop(n) n = n.__to_int raise ArgumentError, "attempt to drop negative size" if n < 0 ary = [] self.each {|*val| n == 0 ? ary << val.__svalue : n -= 1 } ary end |
#drop_while(&block) ⇒ Object
call-seq: enum.drop_while {|arr| block } -> array enum.drop_while -> an_enumerator
Drops elements up to, but not including, the first element for
which the block returns nil or false and returns an array
containing the remaining elements.
If no block is given, an enumerator is returned instead.
a = [1, 2, 3, 4, 5, 0]
a.drop_while {|i| i < 3 } #=> [3, 4, 5, 0]
38 39 40 41 42 43 44 45 46 47 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 38 def drop_while(&block) return to_enum :drop_while unless block ary, state = [], false self.each do |*val| state = true if !state and !block.call(*val) ary << val.__svalue if state end ary end |
#each_cons(n, &block) ⇒ nil
Iterates the given block for each array of consecutive
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 114 def each_cons(n, &block) n = n.__to_int raise ArgumentError, "invalid size" if n <= 0 return to_enum(:each_cons,n) unless block ary = [] n = n.to_i self.each do |*val| ary.shift if ary.size == n ary << val.__svalue block.call(ary.dup) if ary.size == n end nil end |
#each_entry(*args, &blk) ⇒ Object
call-seq: enum.each_entry { |obj| block } -> enum enum.each_entry -> an_enumerator
Calls block once for each element in self, passing that element as a parameter, converting multiple values from yield to an array.
If no block is given, an enumerator is returned instead.
class Foo
include Enumerable
def each
yield 1
yield 1, 2
yield
end
end
Foo.new.each_entry{ |o| p o }
produces:
1
[1, 2]
nil
911 912 913 914 915 916 917 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 911 def each_entry(*args, &blk) return to_enum(:each_entry) unless blk self.each do |*a| yield a.__svalue end return self end |
#each_slice(n, &block) ⇒ nil
Iterates the given block for each slice of
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 142 def each_slice(n, &block) n = n.__to_int raise ArgumentError, "invalid slice size" if n <= 0 return to_enum(:each_slice,n) unless block ary = [] n = n.to_i self.each do |*val| ary << val.__svalue if ary.size == n block.call(ary) ary = [] end end block.call(ary) unless ary.empty? nil end |
#each_with_index(&block) ⇒ Object
Call the given block for each element
which is yield by each. Pass an
index to the block which starts at 0
and increase by 1 for each element.
ISO 15.3.2.2.5
93 94 95 96 97 98 99 100 101 102 |
# File 'mrblib/enum.rb', line 93 def each_with_index(&block) return to_enum :each_with_index unless block i = 0 self.each{|*val| block.call(val.__svalue, i) i += 1 } self end |
#each_with_object(obj, &block) ⇒ Object
call-seq: enum.each_with_object(obj) { |(*args), memo_obj| ... } -> obj enum.each_with_object(obj) -> an_enumerator
Iterates the given block for each element with an arbitrary object given, and returns the initially given object.
If no block is given, returns an enumerator.
(1..10).each_with_object([]) { |i, a| a << i*2 }
#=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
596 597 598 599 600 601 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 596 def each_with_object(obj, &block) return to_enum(:each_with_object, obj) unless block self.each {|*val| block.call(val.__svalue, obj) } obj end |
#entries ⇒ Object Also known as: to_a
Return an array of all elements which
are yield by each.
ISO 15.3.2.2.6
109 110 111 112 113 114 115 116 |
# File 'mrblib/enum.rb', line 109 def entries ary = [] self.each{|*val| # __svalue is an internal method ary.push val.__svalue } ary end |
#filter_map(&blk) ⇒ Object
819 820 821 822 823 824 825 826 827 828 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 819 def filter_map(&blk) return to_enum(:filter_map) unless blk ary = [] self.each do |x| x = blk.call(x) ary.push x if x end ary end |
#find_all(&block) ⇒ Object Also known as: select
Call the given block for each element
which is yield by each. Return an array
which contains all elements whose block
value was true.
ISO 15.3.2.2.8
131 132 133 134 135 136 137 138 139 |
# File 'mrblib/enum.rb', line 131 def find_all(&block) return to_enum :find_all unless block ary = [] self.each{|*val| ary.push(val.__svalue) if block.call(*val) } ary end |
#find_index(val = NONE, &block) ⇒ Object
call-seq: enum.find_index(value) -> int or nil enum.find_index { |obj| block } -> int or nil enum.find_index -> an_enumerator
Compares each entry in enum with value or passes
to block. Returns the index for the first for which the
evaluated value is non-false. If no object matches, returns
nil
If neither block nor argument is given, an enumerator is returned instead.
(1..10).find_index { |i| i % 5 == 0 and i % 7 == 0 } #=> nil
(1..100).find_index { |i| i % 5 == 0 and i % 7 == 0 } #=> 34
(1..100).find_index(50) #=> 49
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 699 def find_index(val=NONE, &block) return to_enum(:find_index, val) if !block && NONE.equal?(val) idx = 0 if block self.each do |*e| return idx if block.call(*e) idx += 1 end else self.each do |*e| return idx if e.__svalue == val idx += 1 end end nil end |
#first(*args) ⇒ Object
call-seq: enum.first -> obj or nil enum.first(n) -> an_array
Returns the first element, or the first n elements, of the enumerable.
If the enumerable is empty, the first form returns nil, and the
second form returns an empty array.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 205 def first(*args) case args.length when 0 self.each do |*val| return val.__svalue end return nil when 1 i = args[0].__to_int raise ArgumentError, "attempt to take negative size" if i < 0 ary = [] return ary if i == 0 self.each do |*val| ary << val.__svalue i -= 1 break if i == 0 end ary else raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0..1)" end end |
#flat_map(&block) ⇒ Object Also known as: collect_concat
call-seq: enum.flat_map { |obj| block } -> array enum.collect_concat { |obj| block } -> array enum.flat_map -> an_enumerator enum.collect_concat -> an_enumerator
Returns a new array with the concatenated results of running block once for every element in enum.
If no block is given, an enumerator is returned instead.
[1, 2, 3, 4].flat_map { |e| [e, -e] } #=> [1, -1, 2, -2, 3, -3, 4, -4]
[[1, 2], [3, 4]].flat_map { |e| e + [100] } #=> [1, 2, 100, 3, 4, 100]
270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 270 def flat_map(&block) return to_enum :flat_map unless block ary = [] self.each do |*e| e2 = block.call(*e) if e2.respond_to? :each e2.each {|e3| ary.push(e3) } else ary.push(e2) end end ary end |
#grep(pattern, &block) ⇒ Object
Call the given block for each element
which is yield by each and which return
value was true when invoking === with
pattern. Return an array with all
elements or the respective block values.
ISO 15.3.2.2.9
149 150 151 152 153 154 155 156 157 158 |
# File 'mrblib/enum.rb', line 149 def grep(pattern, &block) ary = [] self.each{|*val| sv = val.__svalue if pattern === sv ary.push((block)? block.call(*val): sv) end } ary end |
#grep_v(pattern, &block) ⇒ Object
832 833 834 835 836 837 838 839 840 841 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 832 def grep_v(pattern, &block) ary = [] self.each{|*val| sv = val.__svalue unless pattern === sv ary.push((block)? block.call(*val): sv) end } ary end |
#group_by(&block) ⇒ Object
call-seq: enum.group_by {| obj | block } -> a_hash enum.group_by -> an_enumerator
Returns a hash, which keys are evaluated result from the block, and values are arrays of elements in enum corresponding to the key.
(1..6).group_by {|i| i%3} #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
171 172 173 174 175 176 177 178 179 180 181 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 171 def group_by(&block) return to_enum :group_by unless block h = {} self.each do |*val| key = block.call(*val) sv = val.__svalue h.key?(key) ? (h[key] << sv) : (h[key] = [sv]) end h end |
#hash ⇒ Object
redefine #hash 15.3.1.3.15
344 345 346 347 348 349 350 351 352 |
# File 'mrblib/enum.rb', line 344 def hash h = 12347 i = 0 self.each do |e| h = __update_hash(h, i, e.hash) i += 1 end h end |
#include?(obj) ⇒ Boolean Also known as: member?
Return true if at least one element which
is yield by each returns a true value
by invoking == with obj. Otherwise return
false.
ISO 15.3.2.2.10
167 168 169 170 171 172 |
# File 'mrblib/enum.rb', line 167 def include?(obj) self.each{|*val| return true if val.__svalue == obj } false end |
#inject(*args, &block) ⇒ Object Also known as: reduce
Call the given block for each element
which is yield by each. Return value
is the sum of all block values. Pass
to each block the current sum and the
current element.
ISO 15.3.2.2.11
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'mrblib/enum.rb', line 182 def inject(*args, &block) raise ArgumentError, "too many arguments" if args.size > 2 if Symbol === args[-1] sym = args[-1] block = ->(x,y){x.__send__(sym,y)} args.pop end if args.empty? flag = true # no initial argument result = nil else flag = false result = args[0] end self.each{|*val| val = val.__svalue if flag # push first element as initial flag = false result = val else result = block.call(result, val) end } result end |
#lazy ⇒ Object
Enumerable#lazy implementation
Enumerable#lazy returns an instance of Enumerator::Lazy. You can use it just like as normal Enumerable object, except these methods act as 'lazy':
- map collect
- select find_all
- reject
- grep
- grep_v
- drop
- drop_while
- take_while
- flat_map collect_concat
- zip
19 20 21 |
# File 'mrbgems/mruby-enum-lazy/mrblib/lazy.rb', line 19 def lazy Enumerator::Lazy.new(self) end |
#max(&block) ⇒ Object
Return the maximum value of all elements
yield by each. If no block is given <=>
will be invoked to define this value. If
a block is given it will be used instead.
ISO 15.3.2.2.13
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'mrblib/enum.rb', line 223 def max(&block) flag = true # 1st element? result = nil self.each{|*val| val = val.__svalue if flag # 1st element result = val flag = false else if block result = val if block.call(val, result) > 0 else result = val if (val <=> result) > 0 end end } result end |
#max_by(&block) ⇒ Object
call-seq: enum.max_by {|obj| block } -> obj enum.max_by -> an_enumerator
Returns the object in enum that gives the maximum value from the given block.
If no block is given, an enumerator is returned instead.
%w[albatross dog horse].max_by {|x| x.length } #=> "albatross"
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 298 def max_by(&block) return to_enum :max_by unless block first = true max = nil max_cmp = nil self.each do |*val| if first max = val.__svalue max_cmp = block.call(*val) first = false else if (cmp = block.call(*val)) > max_cmp max = val.__svalue max_cmp = cmp end end end max end |
#min(&block) ⇒ Object
Return the minimum value of all elements
yield by each. If no block is given <=>
will be invoked to define this value. If
a block is given it will be used instead.
ISO 15.3.2.2.14
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'mrblib/enum.rb', line 250 def min(&block) flag = true # 1st element? result = nil self.each{|*val| val = val.__svalue if flag # 1st element result = val flag = false else if block result = val if block.call(val, result) < 0 else result = val if (val <=> result) < 0 end end } result end |
#min_by(&block) ⇒ Object
call-seq: enum.min_by {|obj| block } -> obj enum.min_by -> an_enumerator
Returns the object in enum that gives the minimum value from the given block.
If no block is given, an enumerator is returned instead.
%w[albatross dog horse].min_by {|x| x.length } #=> "dog"
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 332 def min_by(&block) return to_enum :min_by unless block first = true min = nil min_cmp = nil self.each do |*val| if first min = val.__svalue min_cmp = block.call(*val) first = false else if (cmp = block.call(*val)) < min_cmp min = val.__svalue min_cmp = cmp end end end min end |
#minmax(&block) ⇒ Object
call-seq: enum.minmax -> [min, max] enum.minmax { |a, b| block } -> [min, max]
Returns two elements array which contains the minimum and the
maximum value in the enumerable. The first form assumes all
objects implement Comparable; the second uses the
block to return a <=> b.
a = %w(albatross dog horse)
a.minmax #=> ["albatross", "horse"]
a.minmax { |a, b| a.length <=> b.length } #=> ["dog", "albatross"]
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 368 def minmax(&block) max = nil min = nil first = true self.each do |*val| if first val = val.__svalue max = val min = val first = false else val = val.__svalue if block max = val if block.call(val, max) > 0 min = val if block.call(val, min) < 0 else max = val if (val <=> max) > 0 min = val if (val <=> min) < 0 end end end [min, max] end |
#minmax_by(&block) ⇒ Object
call-seq: enum.minmax_by { |obj| block } -> [min, max] enum.minmax_by -> an_enumerator
Returns a two element array containing the objects in enum that correspond to the minimum and maximum values respectively from the given block.
If no block is given, an enumerator is returned instead.
%w(albatross dog horse).minmax_by { |x| x.length } #=> ["dog", "albatross"]
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 406 def minmax_by(&block) return to_enum :minmax_by unless block max = nil max_cmp = nil min = nil min_cmp = nil first = true self.each do |*val| if first max = min = val.__svalue max_cmp = min_cmp = block.call(*val) first = false else if (cmp = block.call(*val)) > max_cmp max = val.__svalue max_cmp = cmp end if (cmp = block.call(*val)) < min_cmp min = val.__svalue min_cmp = cmp end end end [min, max] end |
#none?(pat = NONE, &block) ⇒ Boolean
call-seq: enum.none? [{ |obj| block }] -> true or false enum.none?(pattern) -> true or false
Passes each element of the collection to the given block. The method
returns true if the block never returns true
for all elements. If the block is not given, none? will return
true only if none of the collection members is true.
If a pattern is supplied instead, the method returns whether
pattern === element for none of the collection members.
%w(ant bear cat).none? { |word| word.length == 5 } #=> true
%w(ant bear cat).none? { |word| word.length >= 4 } #=> false
%w{ant bear cat}.none?(/d/) #=> true
[1, 3.14, 42].none?(Float) #=> false
[].none? #=> true
[nil, false].none? #=> true
[nil, true].none? #=> false
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 455 def none?(pat=NONE, &block) if !NONE.equal?(pat) self.each do |*val| return false if pat === val.__svalue end elsif block self.each do |*val| return false if block.call(*val) end else self.each do |*val| return false if val.__svalue end end true end |
#one?(pat = NONE, &block) ⇒ Boolean
call-seq: enum.one? [{ |obj| block }] -> true or false enum.one?(pattern) -> true or false
Passes each element of the collection to the given block. The method
returns true if the block returns true
exactly once. If the block is not given, one? will return
true only if exactly one of the collection members is
true.
If a pattern is supplied instead, the method returns whether
pattern === element for exactly one collection member.
%w(ant bear cat).one? { |word| word.length == 4 } #=> true
%w(ant bear cat).one? { |word| word.length > 4 } #=> false
%w(ant bear cat).one? { |word| word.length < 4 } #=> false
%w{ant bear cat}.one?(/t/) #=> false
[nil, true, 99].one? #=> false
[nil, true, false].one? #=> true
[ nil, true, 99 ].one?(Integer) #=> true
[].one? #=> false
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 495 def one?(pat=NONE, &block) count = 0 if !NONE.equal?(pat) self.each do |*val| count += 1 if pat === val.__svalue return false if count > 1 end elsif block self.each do |*val| count += 1 if block.call(*val) return false if count > 1 end else self.each do |*val| count += 1 if val.__svalue return false if count > 1 end end count == 1 ? true : false end |
#partition(&block) ⇒ Object
Call the given block for each element
which is yield by each. Return an
array which contains two arrays. The
first array contains all elements
whose block value was true. The second
array contains all elements whose
block value was false.
ISO 15.3.2.2.16
286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'mrblib/enum.rb', line 286 def partition(&block) return to_enum :partition unless block ary_T = [] ary_F = [] self.each{|*val| if block.call(*val) ary_T.push(val.__svalue) else ary_F.push(val.__svalue) end } [ary_T, ary_F] end |
#reject(&block) ⇒ Object
Call the given block for each element
which is yield by each. Return an
array which contains only the elements
whose block value was false.
ISO 15.3.2.2.17
308 309 310 311 312 313 314 315 316 |
# File 'mrblib/enum.rb', line 308 def reject(&block) return to_enum :reject unless block ary = [] self.each{|*val| ary.push(val.__svalue) unless block.call(*val) } ary end |
#reverse_each(&block) ⇒ Object
call-seq: enum.reverse_each { |item| block } -> enum enum.reverse_each -> an_enumerator
Builds a temporary array and traverses that array in reverse order.
If no block is given, an enumerator is returned instead.
(1..3).reverse_each { |v| p v }
produces:
3
2
1
621 622 623 624 625 626 627 628 629 630 631 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 621 def reverse_each(&block) return to_enum :reverse_each unless block ary = self.to_a i = ary.size - 1 while i>=0 block.call(ary[i]) i -= 1 end self end |
#sort(&block) ⇒ Object
Return a sorted array of all elements
which are yield by each. If no block
is given <=> will be invoked on each
element to define the order. Otherwise
the given block will be used for
sorting.
ISO 15.3.2.2.19
333 334 335 |
# File 'mrblib/enum.rb', line 333 def sort(&block) self.map{|*val| val.__svalue}.sort(&block) end |
#sort_by(&block) ⇒ Object
call-seq: enum.sort_by { |obj| block } -> array enum.sort_by -> an_enumerator
Sorts enum using a set of keys generated by mapping the values in enum through the given block.
If no block is given, an enumerator is returned instead.
192 193 194 195 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 192 def sort_by(&block) return to_enum :sort_by unless block self.to_a.sort_by(&block) end |
#sum(init = 0, &block) ⇒ Object
call-seq: enum.sum(count=1) -> numeric enum.sum(count=1)... -> numeric
Returns the sum of elements. For example, [e1, e2, e3].sum returns e1 + e2 + e3. If a block is given, each element is processed by the block, e.g [e1, e2, e3].sumEnumerable._1_1.m gives e1.m + e2.m + e3.m.
870 871 872 873 874 875 876 877 878 879 880 881 882 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 870 def sum(init=0,&block) result=init if block self.each do |e| result += block.call(e) end else self.each do |e| result += e end end result end |
#take(n) ⇒ Object
call-seq: enum.take(n) -> array
Returns first n elements from enum.
a = [1, 2, 3, 4, 5, 0]
a.take(3) #=> [1, 2, 3]
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 58 def take(n) n = n.__to_int i = n.to_i raise ArgumentError, "attempt to take negative size" if i < 0 ary = [] return ary if i == 0 self.each do |*val| ary << val.__svalue i -= 1 break if i == 0 end ary end |
#take_while(&block) ⇒ Object
call-seq: enum.take_while {|arr| block } -> array enum.take_while -> an_enumerator
Passes elements to the block until the block returns nil or false,
then stops iterating and returns an array of all prior elements.
If no block is given, an enumerator is returned instead.
a = [1, 2, 3, 4, 5, 0]
a.take_while {|i| i < 3 } #=> [1, 2]
85 86 87 88 89 90 91 92 93 94 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 85 def take_while(&block) return to_enum :take_while unless block ary = [] self.each do |*val| return ary unless block.call(*val) ary << val.__svalue end ary end |
#tally ⇒ Object
call-seq: enum.tally -> a_hash
Tallys the collection. Returns a hash where the keys are the elements and the values are numbers of elements in the collection that correspond to the key.
["a", "b", "c", "b"].tally #=> {"a"=>1, "b"=>2, "c"=>1}
852 853 854 855 856 857 858 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 852 def tally hash = {} self.each do |x| hash[x] = (hash[x]||0)+1 end hash end |
#to_h(&blk) ⇒ Object
call-seq: enum.to_h -> hash
Returns the result of interpreting enum as a list of [key, value] pairs.
%i[hello world].each_with_index.to_h
# => {:hello => 0, :world => 1}
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 783 def to_h(&blk) h = {} if blk self.each do |v| v = blk.call(v) raise TypeError, "wrong element type #{v.class} (expected Array)" unless v.is_a? Array raise ArgumentError, "element has wrong array length (expected 2, was #{v.size})" if v.size != 2 h[v[0]] = v[1] end else self.each do |*v| v = v.__svalue raise TypeError, "wrong element type #{v.class} (expected Array)" unless v.is_a? Array raise ArgumentError, "element has wrong array length (expected 2, was #{v.size})" if v.size != 2 h[v[0]] = v[1] end end h end |
#uniq(&block) ⇒ Object
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 803 def uniq(&block) hash = {} if block self.each do|*v| v = v.__svalue hash[block.call(v)] ||= v end else self.each do|*v| v = v.__svalue hash[v] ||= v end end hash.values end |
#zip(*args, &block) ⇒ Object
use Enumerator to use infinite sequence
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 |
# File 'mrbgems/mruby-enum-ext/mrblib/enum.rb', line 744 def zip(*arg, &block) result = block ? nil : [] arg = arg.map do |a| unless a.respond_to?(:to_a) raise TypeError, "wrong argument type #{a.class} (must respond to :to_a)" end a.to_a end i = 0 self.each do |*val| a = [] a.push(val.__svalue) idx = 0 while idx < arg.size a.push(arg[idx][i]) idx += 1 end i += 1 if result.nil? block.call(a) else result.push(a) end end result end |