Module: AbstractMapper::Functions Private

Extended by:
Transproc::Registry
Defined in:
lib/abstract_mapper/functions.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

The collection of gem-specific pure functions (transproc)

Class Method Summary collapse

Class Method Details

.compact(array, fn) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Examples:

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

Parameters:

  • array (Array)
  • fn (Proc)

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

Returns:

  • (Array)


61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Examples:

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

Parameters:

  • array (Array)
  • fn (Proc)

Returns:

  • (Array)


41
42
43
# File 'lib/abstract_mapper/functions.rb', line 41

def self.filter(array, fn)
  t(:map_array, fn)[array].compact.flatten
end

.identity(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the unchanged value whatever parameters are given

Examples:

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

Parameters:

  • value (Object)

Returns:

  • (Object)


25
26
27
# File 'lib/abstract_mapper/functions.rb', line 25

def self.identity(value, *)
  value
end

.restrict(hash, default_hash) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Restricts the hash by keys and values of the default one

Parameters:

  • hash (Hash)
  • default_hash (Hash)

Returns:

  • (Hash)

    <description>



95
96
97
98
99
# 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) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks whether the class or module has given ancestor

Examples:

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

Parameters:

  • subling (Module)
  • ancestor (Module)

Returns:

  • (Boolean)


84
85
86
# File 'lib/abstract_mapper/functions.rb', line 84

def self.subclass?(subling, ancestor)
  subling.ancestors.include?(ancestor)
end