Class: Kleisli::ComposedFn

Inherits:
Blank show all
Defined in:
lib/kleisli/composition.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Blank

find_hidden_method, hide, reveal

Constructor Details

#initialize(fns = []) ⇒ ComposedFn

Returns a new instance of ComposedFn.



9
10
11
# File 'lib/kleisli/composition.rb', line 9

def initialize(fns=[])
  @fns = fns
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



20
21
22
23
24
25
# File 'lib/kleisli/composition.rb', line 20

def method_missing(meth, *args, &block)
  f = -> arguments, receiver {
    receiver.send(meth, *arguments, &block)
  }.curry[args]
  ComposedFn.new(@fns + [f])
end

Class Method Details

.comp(f, g) ⇒ Object



5
6
7
# File 'lib/kleisli/composition.rb', line 5

def self.comp(f, g)
  lambda { |*args| f[g[*args]] }
end

Instance Method Details

#call(*args) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/kleisli/composition.rb', line 27

def call(*args)
  if @fns.any?
    @fns.reduce { |f, g| ComposedFn.comp(f, g) }.call(*args)
  else
    args.first
  end
end

#fn(*args, &block) ⇒ Object



13
14
15
16
17
18
# File 'lib/kleisli/composition.rb', line 13

def fn(*args, &block)
  f = -> arguments, receiver {
    block.call(receiver, *arguments)
  }.curry[args]
  ComposedFn.new(@fns + [f])
end

#to_aryObject



35
36
37
# File 'lib/kleisli/composition.rb', line 35

def to_ary
  @fns.to_ary
end