Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

Location:
EcnlProtoTool/trunk/mruby-2.1.1
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-enum-ext/mrblib/enum.rb

    r331 r439  
    1414
    1515  def drop(n)
    16     raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
     16    n = n.__to_int
    1717    raise ArgumentError, "attempt to drop negative size" if n < 0
    1818
    19     n = n.to_int
    2019    ary = []
    2120    self.each {|*val| n == 0 ? ary << val.__svalue : n -= 1 }
     
    5857
    5958  def take(n)
    60     raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
    61     i = n.to_int
     59    n = n.__to_int
     60    i = n.to_i
    6261    raise ArgumentError, "attempt to take negative size" if i < 0
    6362    ary = []
     
    114113
    115114  def each_cons(n, &block)
    116     raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
     115    n = n.__to_int
    117116    raise ArgumentError, "invalid size" if n <= 0
    118117
    119118    return to_enum(:each_cons,n) unless block
    120119    ary = []
    121     n = n.to_int
     120    n = n.to_i
    122121    self.each do |*val|
    123122      ary.shift if ary.size == n
     
    142141
    143142  def each_slice(n, &block)
    144     raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
     143    n = n.__to_int
    145144    raise ArgumentError, "invalid slice size" if n <= 0
    146145
    147146    return to_enum(:each_slice,n) unless block
    148147    ary = []
    149     n = n.to_int
     148    n = n.to_i
    150149    self.each do |*val|
    151150      ary << val.__svalue
     
    202201    }
    203202    if ary.size > 1
    204       __sort_sub__(ary, ::Array.new(ary.size), 0, 0, ary.size - 1) do |a,b|
    205         a <=> b
    206       end
     203      ary.sort!
    207204    end
    208205    ary.collect{|e,i| orig[i]}
    209206  end
    210207
    211   NONE = Object.new
    212208  ##
    213209  # call-seq:
     
    226222      return nil
    227223    when 1
    228       n = args[0]
    229       raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
    230       i = n.to_int
     224      i = args[0].__to_int
    231225      raise ArgumentError, "attempt to take negative size" if i < 0
    232226      ary = []
     
    452446  #  call-seq:
    453447  #     enum.none? [{ |obj| block }]   -> true or false
     448  #     enum.none?(pattern)            -> true or false
    454449  #
    455450  #  Passes each element of the collection to the given block. The method
     
    458453  #  <code>true</code> only if none of the collection members is true.
    459454  #
     455  #  If a pattern is supplied instead, the method returns whether
     456  #  <code>pattern === element</code> for none of the collection members.
     457  #
    460458  #     %w(ant bear cat).none? { |word| word.length == 5 } #=> true
    461459  #     %w(ant bear cat).none? { |word| word.length >= 4 } #=> false
     460  #     %w{ant bear cat}.none?(/d/)                        #=> true
     461  #     [1, 3.14, 42].none?(Float)                         #=> false
    462462  #     [].none?                                           #=> true
    463463  #     [nil, false].none?                                 #=> true
    464464  #     [nil, true].none?                                  #=> false
    465465
    466   def none?(&block)
    467     if block
     466  def none?(pat=NONE, &block)
     467    if pat != NONE
     468      self.each do |*val|
     469        return false if pat === val.__svalue
     470      end
     471    elsif block
    468472      self.each do |*val|
    469473        return false if block.call(*val)
     
    480484  #  call-seq:
    481485  #    enum.one? [{ |obj| block }]   -> true or false
     486  #    enum.one?(pattern)            -> true or false
    482487  #
    483488  # Passes each element of the collection to the given block. The method
     
    487492  # true.
    488493  #
     494  # If a pattern is supplied instead, the method returns whether
     495  # <code>pattern === element</code> for exactly one collection member.
     496  #
    489497  #    %w(ant bear cat).one? { |word| word.length == 4 }  #=> true
    490498  #    %w(ant bear cat).one? { |word| word.length > 4 }   #=> false
    491499  #    %w(ant bear cat).one? { |word| word.length < 4 }   #=> false
     500  #    %w{ant bear cat}.one?(/t/)                         #=> false
    492501  #    [nil, true, 99].one?                               #=> false
    493502  #    [nil, true, false].one?                            #=> true
    494   #
    495 
    496   def one?(&block)
     503  #    [ nil, true, 99 ].one?(Integer)                    #=> true
     504  #    [].one?                                            #=> false
     505
     506  def one?(pat=NONE, &block)
    497507    count = 0
    498     if block
     508    if pat!=NONE
     509      self.each do |*val|
     510        count += 1 if pat === val.__svalue
     511        return false if count > 1
     512      end
     513    elsif block
    499514      self.each do |*val|
    500515        count += 1 if block.call(*val)
     
    511526  end
    512527
     528  # ISO 15.3.2.2.1
     529  #  call-seq:
     530  #     enum.all? [{ |obj| block } ]   -> true or false
     531  #     enum.all?(pattern)             -> true or false
     532  #
     533  #  Passes each element of the collection to the given block. The method
     534  #  returns <code>true</code> if the block never returns
     535  #  <code>false</code> or <code>nil</code>. If the block is not given,
     536  #  Ruby adds an implicit block of <code>{ |obj| obj }</code> which will
     537  #  cause #all? to return +true+ when none of the collection members are
     538  #  +false+ or +nil+.
     539  #
     540  #  If a pattern is supplied instead, the method returns whether
     541  #  <code>pattern === element</code> for every collection member.
     542  #
     543  #     %w[ant bear cat].all? { |word| word.length >= 3 } #=> true
     544  #     %w[ant bear cat].all? { |word| word.length >= 4 } #=> false
     545  #     %w[ant bear cat].all?(/t/)                        #=> false
     546  #     [1, 2i, 3.14].all?(Numeric)                       #=> true
     547  #     [nil, true, 99].all?                              #=> false
     548  #
     549  def all?(pat=NONE, &block)
     550    if pat != NONE
     551      self.each{|*val| return false unless pat === val.__svalue}
     552    elsif block
     553      self.each{|*val| return false unless block.call(*val)}
     554    else
     555      self.each{|*val| return false unless val.__svalue}
     556    end
     557    true
     558  end
     559
     560  # ISO 15.3.2.2.2
     561  #  call-seq:
     562  #     enum.any? [{ |obj| block }]   -> true or false
     563  #     enum.any?(pattern)            -> true or false
     564  #
     565  #  Passes each element of the collection to the given block. The method
     566  #  returns <code>true</code> if the block ever returns a value other
     567  #  than <code>false</code> or <code>nil</code>. If the block is not
     568  #  given, Ruby adds an implicit block of <code>{ |obj| obj }</code> that
     569  #  will cause #any? to return +true+ if at least one of the collection
     570  #  members is not +false+ or +nil+.
     571  #
     572  #  If a pattern is supplied instead, the method returns whether
     573  #  <code>pattern === element</code> for any collection member.
     574  #
     575  #     %w[ant bear cat].any? { |word| word.length >= 3 } #=> true
     576  #     %w[ant bear cat].any? { |word| word.length >= 4 } #=> true
     577  #     %w[ant bear cat].any?(/d/)                        #=> false
     578  #     [nil, true, 99].any?(Integer)                     #=> true
     579  #     [nil, true, 99].any?                              #=> true
     580  #     [].any?                                           #=> false
     581  #
     582  def any?(pat=NONE, &block)
     583    if pat != NONE
     584      self.each{|*val| return true if pat === val.__svalue}
     585    elsif block
     586      self.each{|*val| return true if block.call(*val)}
     587    else
     588      self.each{|*val| return true if val.__svalue}
     589    end
     590    false
     591  end
     592
    513593  ##
    514594  #  call-seq:
     
    525605  #
    526606
    527   def each_with_object(obj=nil, &block)
    528     raise ArgumentError, "wrong number of arguments (0 for 1)" if obj.nil?
    529 
     607  def each_with_object(obj, &block)
    530608    return to_enum(:each_with_object, obj) unless block
    531609
     
    592670      n = -1
    593671    else
    594       unless nv.respond_to?(:to_int)
    595         raise TypeError, "no implicit conversion of #{nv.class} into Integer"
    596       end
    597       n = nv.to_int
    598       unless n.kind_of?(Integer)
    599         raise TypeError, "no implicit conversion of #{nv.class} into Integer"
    600       end
     672      n = nv.__to_int
    601673      return nil if n <= 0
    602674    end
     
    657729  #  call-seq:
    658730  #     enum.zip(arg, ...)                  -> an_array_of_array
     731  #     enum.zip(arg, ...) { |arr| block }  -> nil
    659732  #
    660733  #  Takes one element from <i>enum</i> and merges corresponding
     
    663736  #  count of arguments.  The length of the resulting sequence will be
    664737  #  <code>enum#size</code>.  If the size of any argument is less than
    665   #  <code>enum#size</code>, <code>nil</code> values are supplied.
    666   #
    667 
    668   def zip(*arg)
    669     ary = []
    670     arg = arg.map{|a|a.to_a}
     738  #  <code>enum#size</code>, <code>nil</code> values are supplied. If
     739  #  a block is given, it is invoked for each output array, otherwise
     740  #  an array of arrays is returned.
     741  #
     742  #     a = [ 4, 5, 6 ]
     743  #     b = [ 7, 8, 9 ]
     744  #
     745  #     a.zip(b)                 #=> [[4, 7], [5, 8], [6, 9]]
     746  #     [1, 2, 3].zip(a, b)      #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
     747  #     [1, 2].zip(a, b)         #=> [[1, 4, 7], [2, 5, 8]]
     748  #     a.zip([1, 2], [8])       #=> [[4, 1, 8], [5, 2, nil], [6, nil, nil]]
     749  #
     750  #     c = []
     751  #     a.zip(b) { |x, y| c << x + y }  #=> nil
     752  #     c                               #=> [11, 13, 15]
     753  #
     754
     755  def zip(*arg, &block)
     756    result = block ? nil : []
     757    arg = arg.map do |a|
     758      unless a.respond_to?(:to_a)
     759        raise TypeError, "wrong argument type #{a.class} (must respond to :to_a)"
     760      end
     761      a.to_a
     762    end
     763
    671764    i = 0
    672765    self.each do |*val|
     
    678771        idx += 1
    679772      end
    680       ary.push(a)
    681773      i += 1
    682     end
    683     ary
     774      if result.nil?
     775        block.call(a)
     776      else
     777        result.push(a)
     778      end
     779    end
     780    result
    684781  end
    685782
     
    695792  #
    696793
    697   def to_h
     794  def to_h(&blk)
    698795    h = {}
    699     self.each do |*v|
    700       v = v.__svalue
    701       raise TypeError, "wrong element type #{v.class} (expected Array)" unless v.is_a? Array
    702       raise ArgumentError, "element has wrong array length (expected 2, was #{v.size})" if v.size != 2
    703       h[v[0]] = v[1]
     796    if blk
     797      self.each do |v|
     798        v = blk.call(v)
     799        raise TypeError, "wrong element type #{v.class} (expected Array)" unless v.is_a? Array
     800        raise ArgumentError, "element has wrong array length (expected 2, was #{v.size})" if v.size != 2
     801        h[v[0]] = v[1]
     802      end
     803    else
     804      self.each do |*v|
     805        v = v.__svalue
     806        raise TypeError, "wrong element type #{v.class} (expected Array)" unless v.is_a? Array
     807        raise ArgumentError, "element has wrong array length (expected 2, was #{v.size})" if v.size != 2
     808        h[v[0]] = v[1]
     809      end
    704810    end
    705811    h
    706812  end
    707813
    708   def nil.to_h
    709     {}
     814  def uniq(&block)
     815    hash = {}
     816    if block
     817      self.each do|*v|
     818        v = v.__svalue
     819        hash[block.call(v)] ||= v
     820      end
     821    else
     822      self.each do|*v|
     823        v = v.__svalue
     824        hash[v] ||= v
     825      end
     826    end
     827    hash.values
     828  end
     829
     830  def filter_map(&blk)
     831    return to_enum(:filter_map) unless blk
     832
     833    ary = []
     834    self.each do |x|
     835      x = blk.call(x)
     836      ary.push x if x
     837    end
     838    ary
     839  end
     840
     841  alias filter select
     842
     843  ##
     844  # call-seq:
     845  #   enum.tally -> a_hash
     846  #
     847  # Tallys the collection.  Returns a hash where the keys are the
     848  # elements and the values are numbers of elements in the collection
     849  # that correspond to the key.
     850  #
     851  #    ["a", "b", "c", "b"].tally #=> {"a"=>1, "b"=>2, "c"=>1}
     852  def tally
     853    hash = {}
     854    self.each do |x|
     855      hash[x] = (hash[x]||0)+1
     856    end
     857    hash
    710858  end
    711859end
Note: See TracChangeset for help on using the changeset viewer.