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)

Instance Method Summary collapse

Instance 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)


47
48
49
50
51
52
53
54
55
56
# File 'lib/abstract_mapper/functions.rb', line 47

def 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)


27
28
29
# File 'lib/abstract_mapper/functions.rb', line 27

def filter(array, fn)
  map_array(array, fn).compact.flatten
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)


70
71
72
# File 'lib/abstract_mapper/functions.rb', line 70

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