Module: Monad::ClassMethods

Defined in:
lib/do_notation/monad.rb

Instance Method Summary collapse

Instance Method Details

#fmap(f) ⇒ Object



65
66
67
# File 'lib/do_notation/monad.rb', line 65

def fmap f
  lambda { |m| m.bind { |y| unit(f.call(y)) } }
end

#foldM(i, l, &f) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/do_notation/monad.rb', line 54

def foldM(i,l, &f)
  if l.empty?
   unit(i)
  else
     run do
       x <- f.call(i,l[0])
       foldM(x,l[1..-1]) { |a,b| f.call(a,b) }
     end
  end
end

#reflect(m) ⇒ Object



73
74
75
# File 'lib/do_notation/monad.rb', line 73

def reflect m
  shift { |k| m.bind { |v| k.call(v) } }
end

#reify(&t) ⇒ Object



69
70
71
# File 'lib/do_notation/monad.rb', line 69

def reify &t
  reset { unit( t.call() ) }
end

#ruby_for(block) ⇒ Object



37
38
39
40
# File 'lib/do_notation/monad.rb', line 37

def ruby_for block
  @cached_ruby ||= {}
  @cached_ruby[block.to_s] ||= "#{self.name}.instance_eval { #{Ruby2Ruby.new.process(Rewriter.new.process(block.to_method.to_sexp)[2])} }"
end

#run(&block) ⇒ Object



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

def run &block
  eval(ruby_for(block), block).call
end

#sequence(l) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/do_notation/monad.rb', line 42

def sequence l
  if l.empty?
   unit([])
  else
     run do
       x <- l[0]
       q <- sequence(l[1..-1])
       unit([x] + q)
     end
  end
end