module AbstractMapper::Functions

The collection of gem-specific pure functions (transproc)

@api private

Public Class Methods

compact(array, fn) click to toggle source

Applies the function to every consecutive pair of array elements, and removes empty values

@example

function = -> a, b { (a == b) ? [a + b] : [a, b] }
fn = Functions[:compact, function]
fn[[1, 1, 2]] # => [4]
fn[[1, 2, 2]] # => [1, 4]

@param [Array] array @param [Proc] fn

Anonymous function (proc, lambda), that takes two arguments
and returns an array

@return [Array]

# File lib/abstract_mapper/functions.rb, line 61
def self.compact(array, fn)
  array.each_with_object([]) do |i, a|
    if a.empty?
      a << i
    else
      a[-1] = fn.call(a.last, i)
      a.flatten!
    end
  end
end
filter(array, fn) click to toggle source

Applies the function to every element of array and removes empty values

@example

fn = Functions[:filter, -> v { v - 1 if v > 3 }]
fn[[1, 4, 5, 3, 2, 5, 9]]
# => [3, 4, 4, 8]

@param [Array] array @param [Proc] fn

@return [Array]

# File lib/abstract_mapper/functions.rb, line 41
def self.filter(array, fn)
  t(:map_array, fn)[array].compact.flatten
end
identity(value, *) click to toggle source

Returns the unchanged value whatever parameters are given

@example

fn = Functions[:identity, :foo]
fn[1] # => 1

@param [Object] value

@return [Object]

# File lib/abstract_mapper/functions.rb, line 25
def self.identity(value, *)
  value
end
restrict(hash, default_hash) click to toggle source

Restricts the hash by keys and values of the default one

@param [Hash] hash @param [Hash] default_hash

@return [Hash] <description>

# File lib/abstract_mapper/functions.rb, line 95
def self.restrict(hash, default_hash)
  keys = default_hash.keys
  values = default_hash.merge(hash).values
  Hash[keys.zip(values)]
end
subclass?(subling, ancestor) click to toggle source

Checks whether the class or module has given ancestor

@example

fn = Functions[:subclass?, Module]
fn[Class]  # => true
fn[Object] # => false

@param [Module] subling @param [Module] ancestor

@return [Boolean]

# File lib/abstract_mapper/functions.rb, line 84
def self.subclass?(subling, ancestor)
  subling.ancestors.include?(ancestor)
end