Module: Invokable::Helpers

Included in:
Invokable
Defined in:
lib/invokable/helpers.rb

Overview

A collection of helper methods for working with invokables where they accept and invokable as an argument they will “coerce” the value (see #coerce). This enables any method that implements ‘call` or `to_proc` to be treated as an invokable.

Version:

  • 0.7.0

Instance Method Summary collapse

Instance Method Details

#always(x) ⇒ Proc

Return a proc that will always return the value given it.

Returns:

Version:

  • 0.7.0



24
25
26
27
28
# File 'lib/invokable/helpers.rb', line 24

def always(x)
  lambda do
    x
  end
end

#coerce(invokable) ⇒ Object

If the invokable passed responds to :call it will be returned. If it responds to to_proc to_proc is called and the resulting proc is returned. Otherwise a TypeError will be raised.

Raises:

  • (TypeError)

Version:

  • 0.7.0



34
35
36
37
38
39
# File 'lib/invokable/helpers.rb', line 34

def coerce(invokable)
  return invokable         if invokable.respond_to?(:call)
  return invokable.to_proc if invokable.respond_to?(:to_proc)
  
  raise TypeError, "#{invokable.inspect} is not a valid invokable"
end

#compose(*invokables) ⇒ Proc

Return a proc that is a composition of the given invokables from right-to-left. The invokables passed will be coerced before they are called (see #coerce).

Examples:

compose(:to_s, :upcase).call(:this_is_a_test) # => "THIS_IS_A_TEST"

Returns:

Version:

  • 0.7.0



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/invokable/helpers.rb', line 90

def compose(*invokables)
  return identity              if invokables.empty?
  return coerce(invokables[0]) if invokables.count == 1
  
  if invokables.count == 2
    f, g = invokables
    return lambda do |*args|
      coerce(f).call(coerce(g).call(*args))
    end
  end
  
  invokables.reduce do |a, b|
    compose(a, b)
  end
end

#guarded(invokable, *alternatives) ⇒ Proc Also known as: fnil, nil_safe

Return a proc that guards it’s arguments from nil by replacing nil values with the alternative value that corresponds to it’s place in the argument list. The invokable passed will be coerced before they are called (see #coerce).

Examples:

count = guarded(:count, [])
count.call(nil) # => 0
count.call([1]) # => 1

Returns:

Version:

  • 0.7.0



118
119
120
121
122
123
124
125
# File 'lib/invokable/helpers.rb', line 118

def guarded(invokable, *alternatives)
  lambda do |*args|
    new_args = args.each_with_index.map do |x, i|
      x.nil? ? alternatives[i] : x
    end
    coerce(invokable).call(*new_args)
  end
end

#identityProc

Return a proc that returns the value that is passed to it.

Returns:

Version:

  • 0.7.0



13
14
15
16
17
# File 'lib/invokable/helpers.rb', line 13

def identity
  lambda do |x|
    x
  end
end

#juxtapose(*invokables) ⇒ Proc Also known as: juxt

Return a proc that passes it’s arguments to the given invokables and returns an array of results. The invokables passed will be coerced before they are called (see #coerce).

Examples:

juxtapose(:first, :count).call('A'..'Z') # => ["A", 26]

Returns:

Version:

  • 0.7.0



50
51
52
53
54
55
56
# File 'lib/invokable/helpers.rb', line 50

def juxtapose(*invokables)
  lambda do |*args|
    invokables.map do |invokable|
      coerce(invokable).call(*args)
    end
  end
end

#knit(*invokables) ⇒ Proc

A relative of #juxtapose–return a proc that takes a collection and calls the invokables on their corresponding values (in sequence) in the collection. The invokables passed will be coerced before they are called (see #coerce).

Examples:

knit(:upcase, :downcase).call(['FoO', 'BaR']) # => ["FOO", "bar"]

Returns:

Version:

  • 0.7.0



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/invokable/helpers.rb', line 69

def knit(*invokables)
  lambda do |enumerable|
    results = []
    enumerable.each_with_index do |x, i|
      return results if invokables[i].nil?
  
      results << coerce(invokables[i]).call(x)
    end
    results
  end
end

#partial(invokable, *args) ⇒ Proc

Given an invokable and and a fewer number of arguments that the invokable takes return a proc that will accept the rest of the arguments (i.e. a partialy applied function).

Returns:

Version:

  • 0.7.0



135
136
137
138
139
# File 'lib/invokable/helpers.rb', line 135

def partial(invokable, *args)
  lambda do |*other_args|
    coerce(invokable).call(*(args + other_args))
  end
end