Class: Monadt::Monad
- Inherits:
-
Object
- Object
- Monadt::Monad
- Defined in:
- lib/monadt.rb,
lib/monadt/monad.rb
Class Method Summary collapse
- .do_m(klass, &blk) ⇒ Object
- .do_m_recur(klass, e) ⇒ Object
- .either(&blk) ⇒ Object
- .maybe(&blk) ⇒ Object
- .present(&blk) ⇒ Object
- .reader(&blk) ⇒ Object
- .reader_state_choice(&blk) ⇒ Object
- .run_reader(env, &blk) ⇒ Object
- .run_reader_state_choice(env, initial_state, &blk) ⇒ Object
- .run_state(initial_state, &blk) ⇒ Object
- .state(&blk) ⇒ Object
Class Method Details
.do_m(klass, &blk) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/monadt/monad.rb', line 21 def do_m(klass, &blk) e = Enumerator.new do |y| m_obj = Internal::MonadObj.new klass, y blk.call(m_obj) end do_m_recur(klass, e) end |
.do_m_recur(klass, e) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/monadt/monad.rb', line 29 def do_m_recur(klass, e) begin ma = e.next rescue StopIteration => ex return ex.result end klass.bind(ma) do |a| e.feed a do_m_recur(klass, e) end end |
.either(&blk) ⇒ Object
18 19 20 |
# File 'lib/monadt.rb', line 18 def either(&blk) do_m(EitherM, &blk) end |
.maybe(&blk) ⇒ Object
10 11 12 |
# File 'lib/monadt.rb', line 10 def maybe(&blk) do_m(MaybeM, &blk) end |
.present(&blk) ⇒ Object
14 15 16 |
# File 'lib/monadt.rb', line 14 def present(&blk) do_m(PresentM, &blk) end |
.reader(&blk) ⇒ Object
31 32 33 |
# File 'lib/monadt.rb', line 31 def reader(&blk) do_m(ReaderM, &blk) end |
.reader_state_choice(&blk) ⇒ Object
39 40 41 |
# File 'lib/monadt.rb', line 39 def reader_state_choice(&blk) do_m(ReaderStateEitherM, &blk) end |
.run_reader(env, &blk) ⇒ Object
35 36 37 |
# File 'lib/monadt.rb', line 35 def run_reader(env, &blk) reader(&blk).(env) end |
.run_reader_state_choice(env, initial_state, &blk) ⇒ Object
43 44 45 |
# File 'lib/monadt.rb', line 43 def run_reader_state_choice(env, initial_state, &blk) reader_state_choice(&blk).(env, initial_state).first end |
.run_state(initial_state, &blk) ⇒ Object
26 27 28 29 |
# File 'lib/monadt.rb', line 26 def run_state(initial_state, &blk) prc = state(&blk) prc.(initial_state).first end |
.state(&blk) ⇒ Object
22 23 24 |
# File 'lib/monadt.rb', line 22 def state(&blk) do_m(StateM, &blk) end |