- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
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
Note:
See TracChangeset
for help on using the changeset viewer.