Module: TAlgebra::Monad::Static

Defined in:
lib/t_algebra/monad.rb

Instance Method Summary collapse

Instance Method Details

#augmented_receiver(block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/t_algebra/monad.rb', line 22

def augmented_receiver(block)
  block_self = block.binding.receiver

  self_class = self
  block_self.define_singleton_method(:method_missing) do |m, *args, &block|
    self_class.send(m, *args, &block)
  end

  block_self
end

#bound(&block) ⇒ Object Also known as: _pick



17
18
19
# File 'lib/t_algebra/monad.rb', line 17

def bound(&block)
  Fiber.yield(block)
end

#chain(&block) ⇒ Object Also known as: run



10
11
12
13
14
# File 'lib/t_algebra/monad.rb', line 10

def chain(&block)
  receiver = augmented_receiver(block)
  fiber_initializer = -> { Fiber.new { receiver.instance_exec(&block) } }
  chain_recursive(fiber_initializer, [])
end

#chain_bind(ma, &block) ⇒ Object



33
34
35
36
# File 'lib/t_algebra/monad.rb', line 33

def chain_bind(ma, &block)
  raise "Yield blocks must return instances of #{self}" unless ma.instance_of?(self)
  ma.bind(&block)
end

#lift_a2(ma, mb) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/t_algebra/monad.rb', line 38

def lift_a2(ma, mb)
  ma.bind do |a|
    mb.bind do |b|
      pure(yield(a, b))
    end
  end
end