Module: Enumerable

Included in:
Nuggets::Array::RegressionMixin::IncrementalLinearRegression, Nuggets::RDF::Turtle
Defined in:
lib/nuggets/enumerable/minmax.rb,
lib/nuggets/enumerable/agrep.rb,
lib/nuggets/enumerable/all_any_extended.rb

Overview

#

A component of ruby-nuggets, some extensions to the Ruby programming # language. #

#

Copyright (C) 2007-2011 Jens Wille #

#

Authors: #

Jens Wille <jens.wille@gmail.com>                                       #
                                                                        #

ruby-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. #

#

ruby-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 ruby-nuggets. If not, see <www.gnu.org/licenses/>. #

#

++

Instance Method Summary (collapse)

Instance Method Details

- (Object) _nuggets_original_all?



30
# File 'lib/nuggets/enumerable/all_any_extended.rb', line 30

alias_method :_nuggets_original_all?, :all?

- (Object) _nuggets_original_any?



31
# File 'lib/nuggets/enumerable/all_any_extended.rb', line 31

alias_method :_nuggets_original_any?, :any?

- (Object) _nuggets_original_max



35
# File 'lib/nuggets/enumerable/minmax.rb', line 35

alias_method :_nuggets_original_max, :max

- (Object) _nuggets_original_max_by



32
# File 'lib/nuggets/enumerable/minmax.rb', line 32

alias_method :_nuggets_original_max_by,    :max_by

- (Object) _nuggets_original_min



36
# File 'lib/nuggets/enumerable/minmax.rb', line 36

alias_method :_nuggets_original_min, :min

- (Object) _nuggets_original_min_by



31
# File 'lib/nuggets/enumerable/minmax.rb', line 31

alias_method :_nuggets_original_min_by,    :min_by

- (Object) _nuggets_original_minmax



33
# File 'lib/nuggets/enumerable/minmax.rb', line 33

alias_method :_nuggets_original_minmax,    :minmax

- (Object) _nuggets_original_minmax_by



30
# File 'lib/nuggets/enumerable/minmax.rb', line 30

alias_method :_nuggets_original_minmax_by, :minmax_by

- (Object) agrep(pattern, distance = 0)

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 to agrep -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.



57
58
59
60
61
62
63
64
# File 'lib/nuggets/enumerable/agrep.rb', line 57

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

- (Boolean) all?(object = default = true, operator = :===, &block)

call-seq:

enum.all?(obj[, operator]) => +true+ or +false+
enum.all? { ... } => +true+ or +false+

Adds the ability to pass an object instead of a block, which will then be tested against each item in enum according to operator, defaulting to :===.

Returns:

  • (Boolean)


40
41
42
# File 'lib/nuggets/enumerable/all_any_extended.rb', line 40

def all?(object = default = true, operator = :===, &block)
  _nuggets_original_all?(&_block_for_all_any_extended(object, default, operator, &block))
end

- (Boolean) any?(object = default = true, operator = :===, &block)

call-seq:

enum.any?(obj[, operator]) => +true+ or +false+
enum.any? { ... } => +true+ or +false+

Adds the ability to pass an object instead of a block, which will then be tested against each item in enum according to operator, defaulting to :===.

Returns:

  • (Boolean)


51
52
53
# File 'lib/nuggets/enumerable/all_any_extended.rb', line 51

def any?(object = default = true, operator = :===, &block)
  _nuggets_original_any?(&_block_for_all_any_extended(object, default, operator, &block))
end

- (Object) max(what = nil)

call-seq:

enum.max(what) => aValue

Maximum #minmax. If what is omitted, or nil, the original Enumerable#max is called.



90
91
92
93
# File 'lib/nuggets/enumerable/minmax.rb', line 90

def max(what = nil)
  what ? minmax(:max, what) : block_given? ?
    _nuggets_original_max { |*a| yield(*a) } : _nuggets_original_max
end

- (Object) max_by(by)

call-seq:

enum.max_by(by) => aValue

Maximum #minmax_by.



53
54
55
# File 'lib/nuggets/enumerable/minmax.rb', line 53

def max_by(by)
  minmax_by(:max, by)
end

- (Object) min(what = nil)

call-seq:

enum.min(what) => aValue

Minimum #minmax. If what is omitted, or nil, the original Enumerable#min is called.



100
101
102
103
# File 'lib/nuggets/enumerable/minmax.rb', line 100

def min(what = nil)
  what ? minmax(:min, what) : block_given? ?
    _nuggets_original_min { |*a| yield(*a) } : _nuggets_original_min
end

- (Object) min_by(by)

call-seq:

enum.min_by(by) => aValue

Minimum #minmax_by.



61
62
63
# File 'lib/nuggets/enumerable/minmax.rb', line 61

def min_by(by)
  minmax_by(:min, by)
end

- (Object) minmax(meth, what)

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


72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nuggets/enumerable/minmax.rb', line 72

def minmax(meth, what)
  #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

- (Object) minmax_by(meth, by)

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).



44
45
46
47
# File 'lib/nuggets/enumerable/minmax.rb', line 44

def minmax_by(meth, by)
  _by = by.is_a?(::Proc) ? by : lambda { |i| i.send(by) }
  send(meth) { |a, b| _by[a] <=> _by[b] }
end