module RubymentArraysModule

# begin_documentation

This module offers functions to manipulate
Arrays.

# end_documentation

Public Instance Methods

array__at(arrays, position) click to toggle source

returns an array with the elements at the given position in each array of arrays.

it's equivalent of calling arrays.transpose, however, it saves the transposed memory.

examples: array__at [ [ :a, :b, :c], [ nil, :b ], [:a] ], 0 # => [:a, nil, :a]

array__at [ [ :a, :b, :c], [ nil, :b ], [:a] ], 2 # => [:c, nil, nil]

# slices can be given in an array: array__at [ [ :a, :b, :c], [ nil, :b ], [:a] ], [1..2] # => [[:b, :c], [:b], []]

# File lib/rubyment.rb, line 619
def array__at arrays, position

  pattern_exec__mapping_an_object [
    arrays,
    "map",
    "slice",
    position,
  ]

end
array__index(arrays, element) click to toggle source

returns an array with the indexes of ocurrences of element in each array of arrays.

examples: array__index [ [ :a, :b, :c], [ nil, :b ], [:a] ], :a # => [0, nil, 0]

r.array__index [ [ :a, :b, :c], [ nil, :b ], [:a] ], :b # => [1, 1, nil]

# File lib/rubyment.rb, line 587
def array__index arrays, element

  pattern_exec__mapping_an_object [
    arrays,
    "map",
    "index",
    element,
  ]

end
array__merge_shallow(*arrays) click to toggle source

merge a list of arrays (like applying || on each column of the matrix that arrays forms).

examples: array__merge_shallow [1, false ], [false, 2, 3] # => [1, 2, 3]

# File lib/rubyment.rb, line 565
def array__merge_shallow *arrays

  arrays__zip(*arrays).map { |a|
    a.reduce "nne"
  }

end
arrays__product(*args) click to toggle source

Just a convenience function for achieving the same as first_array.product other_arrays

Closed for extension

examples:

arrays__product [ “model 1”, “model 2”], [“:”], [“variant with sound system”, “variant no system”], [“and stabilizer”, “and without stabilizer”] # => [[“model 1”, “:”, “variant with sound system”, “and stabilizer”], # [“model 1”, “:”, “variant with sound system”, “and without stabilizer”], # … # [“model 2”, “:”, “variant no system”, “and stabilizer”], # [“model 2”, “:”, “variant no system”, “and without stabilizer”]]

# File lib/rubyment.rb, line 650
def arrays__product *args
  first, *others = args
  first.product *others
end
arrays__zip(*arrays) click to toggle source

zip will finish with the dimension of the first array. It may sound like a feature, I guess it is that way because it was the easiest thing to implement. This one does the harder thing: zip arrays: the generated array will have the longest needed dimension.

The transpose method of Array suffers from the same problem (indeed a bigger one, because it will completely refuse to tranpose arrays with different dimensions, while it could fill them with nil for us), and, again, this function is the solution.

returns an arrays__ definition, which is an array of array having a transposition of arrays, which can be interpreted as a zip of those arrays.

examples:

# this case is the same as [1, 2, 3].zip [4, 5] arrays__zip [1, 2, 3], [4, 5] # => [[1, 4], [2, 5], [3, nil]]

# this case is not the same as [1, 2].zip [3, 4, 5] arrays__zip [1, 2], [3, 4, 5] # => [[1, 3], [2, 4], [nil, 5]]

# File lib/rubyment.rb, line 547
def arrays__zip *arrays
  dimension = arrays.map(&:size).max - 1
  (0..dimension).map { |i|
    arrays.map { |a| a[i] }
  }
end