Module: Enumerable

Included in:
Xqsr3::Containers::FrequencyMap, Xqsr3::Containers::MultiMap
Defined in:
lib/xqsr3/doc_.rb,
lib/xqsr3/extensions/enumerable/unique.rb,
lib/xqsr3/extensions/enumerable/detect_map.rb,
lib/xqsr3/extensions/enumerable/collect_with_index.rb

Overview

Standard module, extended with methods:

  • Enumerable#collect_with_index
  • Enumerable#detect_map
  • Enumerable#unique

Instance Method Summary collapse

Instance Method Details

#collect_with_index(base = 0) ⇒ Object

Two-parameter variant of +Enumerable+#+collect+, where the second parameter is a +base+-based index, which increments by 1 for each enumerated element.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/xqsr3/extensions/enumerable/collect_with_index.rb', line 56

def collect_with_index(base = 0)

  a = []

  self.each_with_index do |element, index|

    a.push yield(element, base + index)
  end

  a
end

#detect_map(&block) ⇒ Object

The +Enumerable+#+detect+ method provides a way to detect the presence of a particular value in a collection. The only constraint is that you get back the object unchanged.

The +Enumerable+#+map+ method provides a way to transform the contents of a collection. The only constraint is that you get back another collection.

This extension method, +Enumerable+#+detect_map+ combines the features of both, in that it detects the presence of a particular value in a collection and transform the detected value. The block should return nil to continue searching; any other value (including false or 0) is treated as a hit and returned immediately.

[ 1, 2, 3 ].detect_map { |v| -2 * v if v > 2 } # => -6

{ :ab => 'cd', :ef => 'gh' }.detect_map { |k, v| v.upcase if k == :ef } # => 'GH'

Note: The block is required (for technical reasons), and must have arity 1 for sequences or 2 for associations



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/xqsr3/extensions/enumerable/detect_map.rb', line 73

def detect_map &block

  case block.arity
  when 1

    self.each do |v|

      r = yield(v)

      return r unless r.nil?
    end
  when 2

    self.each do |k, v|

      r = yield(k, v)

      return r unless r.nil?
    end
  else

    raise ArgumentError, "detect_map requires block with arity of 1 (for sequences) or 2 (for associations); block with arity #{block.arity} given to instance of #{self.class}"
  end

  nil
end

#unique(&block) ⇒ Object

Removes all duplicate elements in a sequence subject to an optional two-parameter block in order to return an array containing unique elements. The first occurrence of each unique element is retained, in encounter order.

Without a block, uniqueness is determined by Hash membership (i.e. #eql? / #hash).

With a block of arity 2, the block is treated as an equality predicate: when it returns a truey value for (kept, candidate), candidate is treated as a duplicate of kept and is discarded. Comparator mode is O(n^2) in the number of elements.

[ 1, 2, 3 ].unique # => [ 1, 2, 3 ] [ 1, 2, 1, 3 ].unique # => [ 1, 2, 3 ] [ 1, 2, 1.0 ].unique { |a, b| a.to_s == b.to_s } # => [ 1, 2 ]

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/xqsr3/extensions/enumerable/unique.rb', line 69

def unique(&block)

  ar = self.to_a

  return ar if ar.length < 2

  unless block

    r = []
    h = {}

    ar.each do |v|

      unless h.has_key?(v)

        r << v
        h[v] = nil
      end
    end

    return r
  end

  raise ArgumentError, "block requires two parameters" unless block.arity == 2

  r = []

  ar.each do |v|

    unless r.any? { |kept| yield(kept, v) }

      r << v
    end
  end

  return r
end