summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-enum-ext
diff options
context:
space:
mode:
Diffstat (limited to 'mrbgems/mruby-enum-ext')
-rw-r--r--mrbgems/mruby-enum-ext/mrblib/enum.rb68
-rw-r--r--mrbgems/mruby-enum-ext/test/enum.rb22
2 files changed, 88 insertions, 2 deletions
diff --git a/mrbgems/mruby-enum-ext/mrblib/enum.rb b/mrbgems/mruby-enum-ext/mrblib/enum.rb
index d0e125850..1849ff4cd 100644
--- a/mrbgems/mruby-enum-ext/mrblib/enum.rb
+++ b/mrbgems/mruby-enum-ext/mrblib/enum.rb
@@ -267,4 +267,72 @@ module Enumerable
ary
end
alias collect_concat flat_map
+
+ ##
+ # call-seq:
+ # enum.max_by {|obj| block } -> obj
+ # enum.max_by -> an_enumerator
+ #
+ # Returns the object in <i>enum</i> 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"
+
+ def max_by(&block)
+ return to_enum :max_by unless block_given?
+
+ 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
+
+ ##
+ # call-seq:
+ # enum.min_by {|obj| block } -> obj
+ # enum.min_by -> an_enumerator
+ #
+ # Returns the object in <i>enum</i> 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"
+
+ def min_by(&block)
+ return to_enum :min_by unless block_given?
+
+ 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
end
diff --git a/mrbgems/mruby-enum-ext/test/enum.rb b/mrbgems/mruby-enum-ext/test/enum.rb
index 9523b497e..ba0851582 100644
--- a/mrbgems/mruby-enum-ext/test/enum.rb
+++ b/mrbgems/mruby-enum-ext/test/enum.rb
@@ -47,11 +47,21 @@ assert("Enumerable#sort_by") do
end
assert("Enumerable#first") do
- a = [1, 2, 3]
+ a = Object.new
+ a.extend Enumerable
+ def a.each
+ yield 1
+ yield 2
+ yield 3
+ end
assert_equal 1, a.first
assert_equal [1, 2], a.first(2)
assert_equal [1, 2, 3], a.first(10)
- assert_nil [].first
+ a = Object.new
+ a.extend Enumerable
+ def a.each
+ end
+ assert_nil a.first
end
assert("Enumerable#count") do
@@ -66,3 +76,11 @@ assert("Enumerable#flat_map") do
assert_equal [1, -1, 2, -2, 3, -3, 4, -4], [1, 2, 3, 4].flat_map { |e| [e, -e] }
assert_equal [1, 2, 100, 3, 4, 100], [[1, 2], [3, 4]].flat_map { |e| e + [100] }
end
+
+assert("Enumerable#max_by") do
+ assert_equal "albatross", %w[albatross dog horse].max_by { |x| x.length }
+end
+
+assert("Enumerable#min_by") do
+ assert_equal "dog", %w[albatross dog horse].min_by { |x| x.length }
+end