Module: Enumerable
- Defined in:
- lib/nuggets/enumerable/minmax.rb,
lib/nuggets/enumerable/agrep.rb
Overview
–
#
nuggets – Extending Ruby #
#
Copyright © 2007-2015 Jens Wille #
#
Authors: #
Jens Wille <[email protected]> #
#
nuggets is free software; you can redistribute it and/or modify it under # the terms of the GNU Affero General Public License as published by the Free # Software Foundation; either version 3 of the License, or (at your option) # any later version. #
#
nuggets is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for # more details. #
#
You should have received a copy of the GNU Affero General Public License # along with nuggets. If not, see <www.gnu.org/licenses/>. #
#
++
Instance Method Summary collapse
- #_nuggets_original_max ⇒ Object
- #_nuggets_original_max_by ⇒ Object
- #_nuggets_original_min ⇒ Object
- #_nuggets_original_min_by ⇒ Object
- #_nuggets_original_minmax ⇒ Object
- #_nuggets_original_minmax_by ⇒ Object
-
#agrep(pattern, distance = 0) ⇒ Object
call-seq: enum.agrep(pattern[, distance]) -> anArray enum.agrep(pattern[, distance]) { |element| … } -> enum.
-
#max(what = nil, &block) ⇒ Object
call-seq: enum.max(what) => aValue.
-
#max_by(by = nil, &block) ⇒ Object
call-seq: enum.max_by(by) => aValue.
-
#min(what = nil, &block) ⇒ Object
call-seq: enum.min(what) => aValue.
-
#min_by(by = nil, &block) ⇒ Object
call-seq: enum.min_by(by) => aValue.
-
#minmax(meth = nil, what = nil, &block) ⇒ Object
call-seq: enum.minmax(meth, what) => anObject.
-
#minmax_by(meth = nil, by = nil, &block) ⇒ Object
call-seq: enum.minmax_by(meth, by) => aValue.
Instance Method Details
#_nuggets_original_max ⇒ Object
34 |
# File 'lib/nuggets/enumerable/minmax.rb', line 34 alias_method :_nuggets_original_max, :max |
#_nuggets_original_max_by ⇒ Object
31 |
# File 'lib/nuggets/enumerable/minmax.rb', line 31 alias_method :_nuggets_original_max_by, :max_by |
#_nuggets_original_min ⇒ Object
35 |
# File 'lib/nuggets/enumerable/minmax.rb', line 35 alias_method :_nuggets_original_min, :min |
#_nuggets_original_min_by ⇒ Object
30 |
# File 'lib/nuggets/enumerable/minmax.rb', line 30 alias_method :_nuggets_original_min_by, :min_by |
#_nuggets_original_minmax ⇒ Object
32 |
# File 'lib/nuggets/enumerable/minmax.rb', line 32 alias_method :_nuggets_original_minmax, :minmax |
#_nuggets_original_minmax_by ⇒ Object
29 |
# File 'lib/nuggets/enumerable/minmax.rb', line 29 alias_method :_nuggets_original_minmax_by, :minmax_by |
#agrep(pattern, distance = 0) ⇒ Object
call-seq:
enum.agrep(pattern[, distance]) -> anArray
enum.agrep(pattern[, distance]) { |element| ... } -> enum
Returns an array of all elements in enum for which pattern
approximately matches element
(see Amatch::Levenshtein#search). If the optional block
is supplied, each matching element is passed to it, and the enum itself is returned.
LIMITATIONS:
-
Only strings are allowed as
pattern
. Regular expressions are reverted to their respective source. (Equivalent toagrep -k
) -
Only works with string elements in enum. (Calls
to_s
on each element) -
The cost for individual error types (substitution, insertion, deletion) cannot be adjusted.
56 57 58 59 60 61 62 63 |
# File 'lib/nuggets/enumerable/agrep.rb', line 56 def agrep(pattern, distance = 0) pattern = pattern.source if pattern.is_a?(::Regexp) am = ::Amatch::Levenshtein.new(pattern) ma = lambda { |i| am.search(i.to_s) <= distance } block_given? ? each { |i| yield i if ma[i] } : select(&ma) end |
#max(what = nil, &block) ⇒ Object
call-seq:
enum.max(what) => aValue
Maximum #minmax. If what
is omitted, or nil
, the original Enumerable#max is called.
95 96 97 |
# File 'lib/nuggets/enumerable/minmax.rb', line 95 def max(what = nil, &block) what ? minmax(:max, what) : _nuggets_original_max(&block) end |
#max_by(by = nil, &block) ⇒ Object
call-seq:
enum.max_by(by) => aValue
Maximum #minmax_by.
54 55 56 57 |
# File 'lib/nuggets/enumerable/minmax.rb', line 54 def max_by(by = nil, &block) by.nil? || by.is_a?(::Numeric) ? _nuggets_original_max_by(by, &block) : minmax_by(:max, by) end |
#min(what = nil, &block) ⇒ Object
call-seq:
enum.min(what) => aValue
Minimum #minmax. If what
is omitted, or nil
, the original Enumerable#min is called.
104 105 106 |
# File 'lib/nuggets/enumerable/minmax.rb', line 104 def min(what = nil, &block) what ? minmax(:min, what) : _nuggets_original_min(&block) end |
#min_by(by = nil, &block) ⇒ Object
call-seq:
enum.min_by(by) => aValue
Minimum #minmax_by.
63 64 65 66 |
# File 'lib/nuggets/enumerable/minmax.rb', line 63 def min_by(by = nil, &block) by.nil? || by.is_a?(::Numeric) ? _nuggets_original_min_by(by, &block) : minmax_by(:min, by) end |
#minmax(meth = nil, what = nil, &block) ⇒ Object
call-seq:
enum.minmax(meth, what) => anObject
Finds the #minmax_by according to what
and returns that “what”.
Example:
%w[a bcd ef].max(:length) #=> 3
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/nuggets/enumerable/minmax.rb', line 75 def minmax(meth = nil, what = nil, &block) return _nuggets_original_minmax(&block) unless meth #m = minmax_by(meth, what) #what.is_a?(Proc) ? what[m] : m.send(what) _what = what.is_a?(::Proc) ? what : lambda { |i| i.send(what) } map { |i| _what[i] }.send(meth) # Benchmark (:max, :length; enum.size = 20, N = 100_000): # # max_by(:length).length 7.920000 0.890000 8.810000 ( 8.991915) # map(:length).max 4.800000 0.600000 5.400000 ( 5.418114) end |
#minmax_by(meth = nil, by = nil, &block) ⇒ Object
call-seq:
enum.minmax_by(meth, by) => aValue
Finds the maximum/minimum (or whatever meth
is) value in enum according to by
(which may be a symbol/string that is sent to each value, or a proc that receives each value as parameter).
43 44 45 46 47 48 |
# File 'lib/nuggets/enumerable/minmax.rb', line 43 def minmax_by(meth = nil, by = nil, &block) return _nuggets_original_minmax_by(&block) unless meth _by = by.is_a?(::Proc) ? by : lambda { |i| i.send(by) } send(meth) { |a, b| _by[a] <=> _by[b] } end |