- 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-enum-ext/mrblib/enum.rb
r331 r439 14 14 15 15 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 17 17 raise ArgumentError, "attempt to drop negative size" if n < 0 18 18 19 n = n.to_int20 19 ary = [] 21 20 self.each {|*val| n == 0 ? ary << val.__svalue : n -= 1 } … … 58 57 59 58 def take(n) 60 raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)61 i = n.to_i nt59 n = n.__to_int 60 i = n.to_i 62 61 raise ArgumentError, "attempt to take negative size" if i < 0 63 62 ary = [] … … 114 113 115 114 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 117 116 raise ArgumentError, "invalid size" if n <= 0 118 117 119 118 return to_enum(:each_cons,n) unless block 120 119 ary = [] 121 n = n.to_i nt120 n = n.to_i 122 121 self.each do |*val| 123 122 ary.shift if ary.size == n … … 142 141 143 142 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 145 144 raise ArgumentError, "invalid slice size" if n <= 0 146 145 147 146 return to_enum(:each_slice,n) unless block 148 147 ary = [] 149 n = n.to_i nt148 n = n.to_i 150 149 self.each do |*val| 151 150 ary << val.__svalue … … 202 201 } 203 202 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! 207 204 end 208 205 ary.collect{|e,i| orig[i]} 209 206 end 210 207 211 NONE = Object.new212 208 ## 213 209 # call-seq: … … 226 222 return nil 227 223 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 231 225 raise ArgumentError, "attempt to take negative size" if i < 0 232 226 ary = [] … … 452 446 # call-seq: 453 447 # enum.none? [{ |obj| block }] -> true or false 448 # enum.none?(pattern) -> true or false 454 449 # 455 450 # Passes each element of the collection to the given block. The method … … 458 453 # <code>true</code> only if none of the collection members is true. 459 454 # 455 # If a pattern is supplied instead, the method returns whether 456 # <code>pattern === element</code> for none of the collection members. 457 # 460 458 # %w(ant bear cat).none? { |word| word.length == 5 } #=> true 461 459 # %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 462 462 # [].none? #=> true 463 463 # [nil, false].none? #=> true 464 464 # [nil, true].none? #=> false 465 465 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 468 472 self.each do |*val| 469 473 return false if block.call(*val) … … 480 484 # call-seq: 481 485 # enum.one? [{ |obj| block }] -> true or false 486 # enum.one?(pattern) -> true or false 482 487 # 483 488 # Passes each element of the collection to the given block. The method … … 487 492 # true. 488 493 # 494 # If a pattern is supplied instead, the method returns whether 495 # <code>pattern === element</code> for exactly one collection member. 496 # 489 497 # %w(ant bear cat).one? { |word| word.length == 4 } #=> true 490 498 # %w(ant bear cat).one? { |word| word.length > 4 } #=> false 491 499 # %w(ant bear cat).one? { |word| word.length < 4 } #=> false 500 # %w{ant bear cat}.one?(/t/) #=> false 492 501 # [nil, true, 99].one? #=> false 493 502 # [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) 497 507 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 499 514 self.each do |*val| 500 515 count += 1 if block.call(*val) … … 511 526 end 512 527 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 513 593 ## 514 594 # call-seq: … … 525 605 # 526 606 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) 530 608 return to_enum(:each_with_object, obj) unless block 531 609 … … 592 670 n = -1 593 671 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 601 673 return nil if n <= 0 602 674 end … … 657 729 # call-seq: 658 730 # enum.zip(arg, ...) -> an_array_of_array 731 # enum.zip(arg, ...) { |arr| block } -> nil 659 732 # 660 733 # Takes one element from <i>enum</i> and merges corresponding … … 663 736 # count of arguments. The length of the resulting sequence will be 664 737 # <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 671 764 i = 0 672 765 self.each do |*val| … … 678 771 idx += 1 679 772 end 680 ary.push(a)681 773 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 684 781 end 685 782 … … 695 792 # 696 793 697 def to_h 794 def to_h(&blk) 698 795 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 704 810 end 705 811 h 706 812 end 707 813 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 710 858 end 711 859 end
Note:
See TracChangeset
for help on using the changeset viewer.