Class: Array

Inherits:
Object show all
Defined in:
lib/y_support/core_ext/array/misc.rb,
lib/y_support/typing/array/typing.rb

Instance Method Summary collapse

Instance Method Details

#>>(collection) ⇒ Object

Zips this array with another collection into a hash.



42
43
44
# File 'lib/y_support/core_ext/array/misc.rb', line 42

def >> collection
  zip_to_hash collection
end

#aA_empty(what_is_receiver = "array") ⇒ Object

Fails with ArgumentError unless the receiver’s #empty? returns true.



56
57
58
# File 'lib/y_support/typing/array/typing.rb', line 56

def aA_empty what_is_receiver="array"
  tap { empty? or fail ArgumentError, "%s not empty".X!( what_is_receiver ) }
end

#aA_include(element, what_is_self = "array", what_is_element = nil) ⇒ Object

This method takes a block and fails with ArgumentError if the receiver array fails to include the specified element. An optional argument customizes the error message (element description).



39
40
41
42
43
44
45
# File 'lib/y_support/typing/array/typing.rb', line 39

def aA_include element, what_is_self="array", what_is_element=nil
  m = "%s is absent from #{what_is_self}!" %
    if what_is_element then what_is_element.to_s.capitalize else
      "Element (#{element.class} instance)"
    end
  tap { include? element or fail ArgumentError, m }
end

#aA_not_empty(what_is_receiver = "array") ⇒ Object

Fails with ArgumentError unless the receiver’s #empty? returns false.



62
63
64
# File 'lib/y_support/typing/array/typing.rb', line 62

def aA_not_empty what_is_receiver="array"
  tap { empty? and fail ArgumentError, "%s empty".X!( what_is_receiver ) }
end

#aA_uniq(what_is_self = "array") ⇒ Object

Fails with ArgumentError if the array contains duplicates (using #uniq).



49
50
51
52
# File 'lib/y_support/typing/array/typing.rb', line 49

def aA_uniq what_is_self="array"
  m = "#{what_is_self.to_s.capitalize} non-uniq!"
  tap { self == uniq or fail ArgumentError, m }
end

#arrays_to_hash(tail_from = 1) ⇒ Object

Converts an array, whose elements are also arrays, to a hash. Head (position 0) of each array is made to point at the rest of the array (tail), normally starting immediately after the head (position 1). The starting position of the tail can be controlled by an optional argument. Tails of 2 and more elements are represented as arrays.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/y_support/core_ext/array/misc.rb', line 12

def arrays_to_hash( tail_from = 1 )
  no_nil_heads = begin
                   self.reject { | e | e[0].nil? }
                 rescue NoMethodError => err
                   raise TypeError, "The receiver must be an array of " +
                     "arrays! (#{err})"
                 end
  no_nil_heads.each_with_object Hash.new do |element, memo|
    tail = element[ tail_from .. -1 ]
    memo.update element.first => ( tail.size > 1 ? tail : tail.first )
  end
end

#ascending_ceiling(arg, accept_equal = true) ⇒ Object

Assuming an array of comparable elements ordered in an ascending order, this method expects an argument comparable with the elements, and returns the neares greater or equal element. The second optional ordered argument, true by default, controls whether equality is OK. If set to false, then the nearest greater element is sought.



68
69
70
71
72
73
74
75
# File 'lib/y_support/core_ext/array/misc.rb', line 68

def ascending_ceiling arg, accept_equal=true
  idx = if accept_equal then
          find_index { |e| not e < arg }
        else find_index { |e| not e <= arg } end
  case idx
  when nil then nil
  else fetch idx end
end

#ascending_floor(arg, accept_equal = true) ⇒ Object

Assuming an array of comparable elements ordered in an ascending order, this method expects an argument comparable with the elements, and returns the nearest smaller or equal element. The second optional ordered argument, true by default, controls whether equality is OK. If set to false, then the nearest smaller element is sought.



52
53
54
55
56
57
58
59
60
# File 'lib/y_support/core_ext/array/misc.rb', line 52

def ascending_floor arg, accept_equal=true
  idx = if accept_equal then
          find_index { |e| e > arg }
        else find_index { |e| e >= arg } end
  case idx
  when 0 then nil
  when nil then last
  else fetch idx - 1 end
end

#aT_empty(what_is_receiver = "array") ⇒ Object

Fails with TypeError unless the receiver’s #empty? returns true.



25
26
27
# File 'lib/y_support/typing/array/typing.rb', line 25

def aT_empty what_is_receiver="array"
  tap { empty? or fail TypeError, "%s not empty".X!( what_is_receiver ) }
end

#aT_include(element, what_is_self = "array", what_is_element = nil) ⇒ Object

This method takes a block and fails with TypeError, if the receiver array fails to include the specified element. An optional argument customizes the error message (element description).



8
9
10
11
12
13
14
# File 'lib/y_support/typing/array/typing.rb', line 8

def aT_include element, what_is_self="array", what_is_element=nil
  m = "%s is absent from #{what_is_self}!" %
    if what_is_element then what_is_element.to_s.capitalize else
      "Element (#{element.class} instance)"
    end
  tap { include? element or fail TypeError, m }
end

#aT_not_empty(what_is_receiver = "array") ⇒ Object

Fails with TypeError unless the receiver’s #empty? returns false.



31
32
33
# File 'lib/y_support/typing/array/typing.rb', line 31

def aT_not_empty what_is_receiver="array"
  tap { empty? and fail TypeError, "%s empty".X!( what_is_receiver ) }
end

#aT_uniq(what_is_self = "array") ⇒ Object

Fails with TypeError if the array contains duplicates (using #uniq).



18
19
20
21
# File 'lib/y_support/typing/array/typing.rb', line 18

def aT_uniq what_is_self="array"
  m = "#{what_is_self.to_s.capitalize} non-uniq!"
  tap { self == uniq or fail TypeError, m }
end

#correspondence_matrix(other) ⇒ Object

Returns correspondence matrix to another array.



131
132
133
# File 'lib/y_support/core_ext/array/misc.rb', line 131

def correspondence_matrix other
  Matrix.correspondence_matrix self, other
end

#indices_of(other) ⇒ Object

Returns indices of elements of another array in this array (inverse of #values_at).



138
139
140
# File 'lib/y_support/core_ext/array/misc.rb', line 138

def indices_of other
  other.map { |e| index e }
end

#pop_named(key) ⇒ Object

With array construction syntax [:foo, bar: 42] now possible in Ruby, arrays become closer to argument collections, and supporting methods might come handy. This method pops an element from the “ordered arguments” array part.



118
119
120
121
# File 'lib/y_support/core_ext/array/misc.rb', line 118

def pop_named key
  l = last
  l.delete( key ).tap { pop if l.empty? } if l.is_a? Hash
end

#pop_orderedObject

With array construction syntax [:foo, bar: 42] now possible in Ruby, arrays become closer to argument collections, and supporting methods might come handy. This method pops an element from the “ordered arguments” array part.



108
109
110
111
112
# File 'lib/y_support/core_ext/array/misc.rb', line 108

def pop_ordered
  l = pop
  return l unless l.is_a? Hash
  pop.tap { push l }
end

Pretty-prints the array as line, with prescribed :precision and :gap (both are named arguments).



145
146
147
148
149
# File 'lib/y_support/core_ext/array/misc.rb', line 145

def print_as_line precision: 2, distance: precision + 4
  element_strings = map { |e| "%.#{precision}f" % e rescue "%s" % e }
    .map { |ς| "%- #{distance}s" % ς[ 0, distance ] }
  puts element_strings.join
end

#push_named(**oo) ⇒ Object

With array construction syntax [:foo, bar: 42] now possible in Ruby, arrays become closer to argument collections, and supporting methods might come handy. This method pushes an element on top of the “named arguments” part of the array.



98
99
100
101
102
# File 'lib/y_support/core_ext/array/misc.rb', line 98

def push_named **oo
  l = last
  return push oo unless l.is_a? Hash
  tap { l.update oo }
end

#push_ordered(element) ⇒ Object

With array construction syntax [:foo, bar: 42] now possible in Ruby, arrays become closer to argument collections, and supporting methods might come handy. This method pushes an element on top of the “ordered arguments” part of the array.



88
89
90
91
# File 'lib/y_support/core_ext/array/misc.rb', line 88

def push_ordered element
  return push element unless last.is_a? Hash
  push pop.tap { push element }
end

#to_column_vectorObject

Converts the array to a Matrix#column_vector.



125
126
127
# File 'lib/y_support/core_ext/array/misc.rb', line 125

def to_column_vector
  Matrix.column_vector self
end

#to_procObject

Allows style &[ function, *arguments ]



79
80
81
# File 'lib/y_support/core_ext/array/misc.rb', line 79

def to_proc
  proc { |receiver| receiver.send *self }
end

#zip_to_hash(collection = nil) ⇒ Object

Zips this array with another collection into a hash. If a block is given, it is applied to each element of the array to get the hash values.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/y_support/core_ext/array/misc.rb', line 28

def zip_to_hash collection=nil
  if block_given? then
    fail ArgumentError, "Argument not allowed if block given!" unless
      collection.nil?
    Hash[ zip( map { |e| yield e } ) ]
  else
    fail ArgumentError "A second collection expected as an argument!" unless
      collection.respond_to? :each
    Hash[ zip( collection ) ]
  end
end