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.
Instance Method Summary collapse
-
#always(x) ⇒ Proc
Return a proc that will always return the value given it.
-
#coerce(invokable) ⇒ Object
If the invokable passed responds to :call it will be returned.
-
#compose(*invokables) ⇒ Proc
Return a proc that is a composition of the given invokables from right-to-left.
-
#guarded(invokable, *alternatives) ⇒ Proc
(also: #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.
-
#identity ⇒ Proc
Return a proc that returns the value that is passed to it.
-
#juxtapose(*invokables) ⇒ Proc
(also: #juxt)
Return a proc that passes it’s arguments to the given invokables and returns an array of results.
-
#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.
-
#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).
Instance Method Details
#always(x) ⇒ Proc
Return a proc that will always return the value given it.
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.
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).
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).
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 |
#identity ⇒ Proc
Return a proc that returns the value that is passed to it.
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).
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).
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).
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 |