Changeset 439 for EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-enumerator
- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 3 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-enumerator/mrbgem.rake
r321 r439 3 3 spec.author = 'mruby developers' 4 4 spec.add_dependency('mruby-fiber', :core => 'mruby-fiber') 5 spec.add_dependency 'mruby-enum-ext', :core => 'mruby-enum-ext'6 5 spec.summary = 'Enumerator class' 7 6 end -
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-enumerator/mrblib/enumerator.rb
r331 r439 90 90 91 91 ## 92 # @overload initialize(size = nil, &block)93 92 # @overload initialize(obj, method = :each, *args) 94 93 # … … 110 109 # p fib.take(10) # => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] 111 110 # 112 def initialize(obj=nil, meth=:each, *args, &block) 113 if block_given? 111 # In the second, deprecated, form, a generated Enumerator iterates over the 112 # given object using the given method with the given arguments passed. This 113 # form is left only for internal use. 114 # 115 # Use of this form is discouraged. Use Kernel#enum_for or Kernel#to_enum 116 # instead. 117 def initialize(obj=NONE, meth=:each, *args, &block) 118 if block 114 119 obj = Generator.new(&block) 115 els e116 raise ArgumentError unless obj120 elsif obj == NONE 121 raise ArgumentError, "wrong number of arguments (given 0, expected 1+)" 117 122 end 118 123 119 124 @obj = obj 120 125 @meth = meth 121 @args = args .dup126 @args = args 122 127 @fib = nil 123 128 @dst = nil … … 126 131 @stop_exc = false 127 132 end 128 attr_accessor :obj, :meth, :args , :fib129 private :obj, :meth, :args,:fib133 attr_accessor :obj, :meth, :args 134 attr_reader :fib 130 135 131 136 def initialize_copy(obj) … … 152 157 # +offset+:: the starting index to use 153 158 # 154 def with_index(offset=0) 155 return to_enum :with_index, offset unless block_given? 156 offset = if offset.nil? 157 0 158 elsif offset.respond_to?(:to_int) 159 offset.to_int 159 def with_index(offset=0, &block) 160 return to_enum :with_index, offset unless block 161 162 if offset.nil? 163 offset = 0 160 164 else 161 raise TypeError, "no implicit conversion of #{offset.class} into Integer"165 offset = offset.__to_int 162 166 end 163 167 … … 165 169 enumerator_block_call do |*i| 166 170 n += 1 167 yieldi.__svalue, n171 block.call i.__svalue, n 168 172 end 169 173 end … … 210 214 # # => foo:2 211 215 # 212 def with_object(object )213 return to_enum(:with_object, object) unless block _given?216 def with_object(object, &block) 217 return to_enum(:with_object, object) unless block 214 218 215 219 enumerator_block_call do |i| 216 yield[i,object]220 block.call [i,object] 217 221 end 218 222 object … … 220 224 221 225 def inspect 222 return "#<#{self.class}: uninitialized>" unless @obj223 224 226 if @args && @args.size > 0 225 227 args = @args.join(", ") 226 "#<#{self.class}: #{@obj }:#{@meth}(#{args})>"228 "#<#{self.class}: #{@obj.inspect}:#{@meth}(#{args})>" 227 229 else 228 "#<#{self.class}: #{@obj }:#{@meth}>"230 "#<#{self.class}: #{@obj.inspect}:#{@meth}>" 229 231 end 230 232 end … … 242 244 # === Examples 243 245 # 244 # "Hello, world!".scan(/\w+/) #=> ["Hello", "world"] 245 # "Hello, world!".to_enum(:scan, /\w+/).to_a #=> ["Hello", "world"] 246 # "Hello, world!".to_enum(:scan).each(/\w+/).to_a #=> ["Hello", "world"] 246 # Array.new(3) #=> [nil, nil, nil] 247 # Array.new(3) { |i| i } #=> [0, 1, 2] 248 # Array.to_enum(:new, 3).to_a #=> [0, 1, 2] 249 # Array.to_enum(:new).each(3).to_a #=> [0, 1, 2] 247 250 # 248 251 # obj = Object.new … … 278 281 obj.args = args 279 282 end 280 return obj unless block _given?283 return obj unless block 281 284 enumerator_block_call(&block) 282 285 end … … 539 542 class Yielder 540 543 def initialize(&block) 541 raise LocalJumpError, "no block given" unless block _given?544 raise LocalJumpError, "no block given" unless block 542 545 543 546 @proc = block … … 553 556 end 554 557 end 558 559 ## 560 # call-seq: 561 # Enumerator.produce(initial = nil) { |val| } -> enumerator 562 # 563 # Creates an infinite enumerator from any block, just called over and 564 # over. Result of the previous iteration is passed to the next one. 565 # If +initial+ is provided, it is passed to the first iteration, and 566 # becomes the first element of the enumerator; if it is not provided, 567 # first iteration receives +nil+, and its result becomes first 568 # element of the iterator. 569 # 570 # Raising StopIteration from the block stops an iteration. 571 # 572 # Examples of usage: 573 # 574 # Enumerator.produce(1, &:succ) # => enumerator of 1, 2, 3, 4, .... 575 # 576 # Enumerator.produce { rand(10) } # => infinite random number sequence 577 # 578 # ancestors = Enumerator.produce(node) { |prev| node = prev.parent or raise StopIteration } 579 # enclosing_section = ancestors.find { |n| n.type == :section } 580 def Enumerator.produce(init=NONE, &block) 581 raise ArgumentError, "no block given" if block.nil? 582 Enumerator.new do |y| 583 if init == NONE 584 val = nil 585 else 586 val = init 587 y.yield(val) 588 end 589 begin 590 while true 591 y.yield(val = block.call(val)) 592 end 593 rescue StopIteration 594 # do nothing 595 end 596 end 597 end 555 598 end 556 599 … … 560 603 # obj.to_enum(method = :each, *args) -> enum 561 604 # obj.enum_for(method = :each, *args) -> enum 562 # obj.to_enum(method = :each, *args) {|*args| block} -> enum563 # obj.enum_for(method = :each, *args){|*args| block} -> enum564 605 # 565 606 # Creates a new Enumerator which will enumerate by calling +method+ on 566 607 # +obj+, passing +args+ if any. 567 #568 # If a block is given, it will be used to calculate the size of569 # the enumerator without the need to iterate it (see Enumerator#size).570 608 # 571 609 # === Examples … … 586 624 # a generic Enumerable, in case no block is passed. 587 625 # 588 # Here is such an example , with parameter passing and a sizing block:626 # Here is such an example with parameter passing: 589 627 # 590 628 # module Enumerable … … 593 631 # raise ArgumentError, "#{n} is negative!" if n < 0 594 632 # unless block_given? 595 # return to_enum(__method__, n) do # __method__ is :repeat here 596 # sz = size # Call size and multiply by n... 597 # sz * n if sz # but return nil if size itself is nil 598 # end 633 # return to_enum(__method__, n) # __method__ is :repeat here 599 634 # end 600 635 # each do |*val| … … 613 648 Enumerator.new self, meth, *args 614 649 end 615 alias :enum_for :to_enum650 alias enum_for to_enum 616 651 end 617 652 618 653 module Enumerable 619 654 # use Enumerator to use infinite sequence 620 def zip(*arg) 621 ary = [] 622 arg = arg.map{|a|a.each} 623 i = 0 624 self.each do |*val| 625 a = [] 626 a.push(val.__svalue) 627 idx = 0 628 while idx < arg.size 629 begin 630 a.push(arg[idx].next) 631 rescue StopIteration 632 a.push(nil) 655 def zip(*args, &block) 656 args = args.map do |a| 657 if a.respond_to?(:each) 658 a.to_enum(:each) 659 else 660 raise TypeError, "wrong argument type #{a.class} (must respond to :each)" 661 end 662 end 663 664 result = block ? nil : [] 665 666 each do |*val| 667 tmp = [val.__svalue] 668 args.each do |arg| 669 v = if arg.nil? 670 nil 671 else 672 begin 673 arg.next 674 rescue StopIteration 675 nil 676 end 633 677 end 634 idx += 1678 tmp.push(v) 635 679 end 636 ary.push(a) 637 i += 1 638 end 639 ary 680 if result.nil? 681 block.call(tmp) 682 else 683 result.push(tmp) 684 end 685 end 686 687 result 640 688 end 641 689 end -
EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-enumerator/test/enumerator.rb
r331 r439 7 7 end 8 8 9 assert 'Enumerator' do 9 def assert_take(exp, enumerator) 10 result = [] 11 n = exp.size 12 enumerator.each do |v| 13 result << v 14 n -= 1 15 break if n == 0 16 end if n > 0 17 assert_equal exp, result 18 end 19 20 assert 'Enumerator.class' do 10 21 assert_equal Class, Enumerator.class 11 22 end 12 23 13 assert 'Enumerator ' do24 assert 'Enumerator.superclass' do 14 25 assert_equal Object, Enumerator.superclass 15 26 end … … 20 31 assert_equal [[:x,1],[:y,2]], {x:1, y:2}.each.map{|i| i}.sort 21 32 assert_equal [1,2,3], @obj.to_enum(:foo, 1,2,3).to_a 22 assert_equal [1,2,3], Enumerator.new(@obj, :foo, 1,2,3).to_a 23 assert_equal [1,2,3], Enumerator.new { |y| i = 0; loop { y << (i += 1) } }.take(3) 33 assert_take [1,2,3], Enumerator.new { |y| i = 0; loop { y << (i += 1) } } 24 34 assert_raise(ArgumentError) { Enumerator.new } 25 enum = @obj.to_enum26 assert_raise(NoMethodError) { enum.each {} }27 35 28 36 # examples … … 34 42 end 35 43 end 36 assert_ equal fib.take(10), [1,1,2,3,5,8,13,21,34,55]44 assert_take [1,1,2,3,5,8,13,21,34,55], fib 37 45 end 38 46 … … 54 62 @obj.to_enum(:foo, 1, 2, 3).with_index(10).with_index(20) { |*i| a << i } 55 63 assert_equal [[[1, 10], 20], [[2, 11], 21], [[3, 12], 22]], a 56 end57 58 assert 'Enumerator#with_index nonnum offset' do59 s = Object.new60 def s.to_int; 1 end61 assert_equal([[1,1],[2,2],[3,3]], @obj.to_enum(:foo, 1, 2, 3).with_index(s).to_a)62 64 end 63 65 … … 100 102 assert 'Enumerator#inspect' do 101 103 e = (0..10).each 102 assert_equal("#<Enumerator: 0..10:each>", e.inspect) 103 e = Enumerator.new("FooObject", :foo, 1) 104 assert_equal("#<Enumerator: FooObject:foo(1)>", e.inspect) 105 e = Enumerator.new("FooObject", :foo, 1, 2, 3) 106 assert_equal("#<Enumerator: FooObject:foo(1, 2, 3)>", e.inspect) 104 assert_equal('#<Enumerator: 0..10:each>', e.inspect) 105 e = 'FooObject'.enum_for(:foo, 1) 106 assert_equal('#<Enumerator: "FooObject":foo(1)>', e.inspect) 107 e = 'FooObject'.enum_for(:foo, 1, 2, 3) 108 assert_equal('#<Enumerator: "FooObject":foo(1, 2, 3)>', e.inspect) 109 e = nil.enum_for(:to_s) 110 assert_equal('#<Enumerator: nil:to_s>', e.inspect) 107 111 end 108 112 … … 426 430 427 431 assert 'Kernel#to_enum' do 432 e = nil 428 433 assert_equal Enumerator, [].to_enum.class 429 assert_raise(ArgumentError){ nil.to_enum } 434 assert_nothing_raised { e = [].to_enum(:_not_implemented_) } 435 assert_raise(NoMethodError) { e.first } 430 436 end 431 437 … … 510 516 assert 'Hash#select' do 511 517 h = {1=>2,3=>4,5=>6} 512 hret = h.select.with_index {|a, b| a[1] == 4}518 hret = h.select.with_index {|a,_b| a[1] == 4} 513 519 assert_equal({3=>4}, hret) 514 520 assert_equal({1=>2,3=>4,5=>6}, h) … … 517 523 assert 'Hash#select!' do 518 524 h = {1=>2,3=>4,5=>6} 519 hret = h.select!.with_index {|a, b| a[1] == 4}525 hret = h.select!.with_index {|a,_b| a[1] == 4} 520 526 assert_equal h, hret 521 527 assert_equal({3=>4}, h) … … 524 530 assert 'Hash#reject' do 525 531 h = {1=>2,3=>4,5=>6} 526 hret = h.reject.with_index {|a, b| a[1] == 4}532 hret = h.reject.with_index {|a,_b| a[1] == 4} 527 533 assert_equal({1=>2,5=>6}, hret) 528 534 assert_equal({1=>2,3=>4,5=>6}, h) … … 531 537 assert 'Hash#reject!' do 532 538 h = {1=>2,3=>4,5=>6} 533 hret = h.reject!.with_index {|a, b| a[1] == 4}539 hret = h.reject!.with_index {|a,_b| a[1] == 4} 534 540 assert_equal h, hret 535 541 assert_equal({1=>2,5=>6}, h) … … 545 551 assert_equal [1,2,3,4,5], c 546 552 end 553 554 assert 'Enumerable#zip' do 555 assert_equal [[1, 10], [2, 11], [3, 12]], [1,2,3].zip(10..Float::INFINITY) 556 557 ret = [] 558 assert_equal nil, [1,2,3].zip(10..Float::INFINITY) { |i| ret << i } 559 assert_equal [[1, 10], [2, 11], [3, 12]], ret 560 561 assert_raise(TypeError) { [1].zip(1) } 562 end 563 564 assert 'Enumerator.produce' do 565 assert_raise(ArgumentError) { Enumerator.produce } 566 567 # Without initial object 568 passed_args = [] 569 enum = Enumerator.produce {|obj| passed_args << obj; (obj || 0).succ } 570 assert_equal Enumerator, enum.class 571 assert_take [1, 2, 3], enum 572 assert_equal [nil, 1, 2], passed_args 573 574 # With initial object 575 passed_args = [] 576 enum = Enumerator.produce(1) {|obj| passed_args << obj; obj.succ } 577 assert_take [1, 2, 3], enum 578 assert_equal [1, 2], passed_args 579 580 # Raising StopIteration 581 words = %w[The quick brown fox jumps over the lazy dog] 582 enum = Enumerator.produce { words.shift or raise StopIteration } 583 assert_equal %w[The quick brown fox jumps over the lazy dog], enum.to_a 584 585 # Raising StopIteration 586 object = [[[["abc", "def"], "ghi", "jkl"], "mno", "pqr"], "stuv", "wxyz"] 587 enum = Enumerator.produce(object) {|obj| 588 obj.respond_to?(:first) or raise StopIteration 589 obj.first 590 } 591 assert_nothing_raised { 592 assert_equal [ 593 [[[["abc", "def"], "ghi", "jkl"], "mno", "pqr"], "stuv", "wxyz"], 594 [[["abc", "def"], "ghi", "jkl"], "mno", "pqr"], 595 [["abc", "def"], "ghi", "jkl"], 596 ["abc", "def"], 597 "abc", 598 ], enum.to_a 599 } 600 end
Note:
See TracChangeset
for help on using the changeset viewer.