Module: RParsec::Monad
- Included in:
- Parser
- Defined in:
- lib/rparsec/monad.rb
Overview
module for Monad
Instance Attribute Summary collapse
-
#this ⇒ Object
readonly
Returns the value of attribute this.
Instance Method Summary collapse
-
#bind(&binder) ⇒ Object
Run the bind operation on the encapsulated object following the monad law.
-
#initMonad(m, v) ⇒ Object
To initialize with a monad implementation and an object that obeys the monad law.
-
#map(&mapper) ⇒ Object
Run the map operation on the encapsulated object following the monad law.
-
#plus(other) ⇒ Object
Run the plus operation on the encapsulated object following the MonadPlus law.
-
#seq(other) ⇒ Object
Run the seq operation on the encapsulated object following the monad law.
-
#value(v) ⇒ Object
To create a value based on the monad impl.
Instance Attribute Details
#this ⇒ Object (readonly)
Returns the value of attribute this.
9 10 11 |
# File 'lib/rparsec/monad.rb', line 9 def this @this end |
Instance Method Details
#bind(&binder) ⇒ Object
Run the bind operation on the encapsulated object following the monad law.
30 31 32 |
# File 'lib/rparsec/monad.rb', line 30 def bind(&binder) @monad.bind(@this, &binder) end |
#initMonad(m, v) ⇒ Object
To initialize with a monad implementation and an object that obeys the monad law.
14 15 16 17 18 |
# File 'lib/rparsec/monad.rb', line 14 def initMonad(m, v) raise ArgumentError, 'monad cannot be nil' if m.nil? @monad = m; @this = v; end |
#map(&mapper) ⇒ Object
Run the map operation on the encapsulated object following the monad law. #bind is used to implement.
51 52 53 54 55 56 |
# File 'lib/rparsec/monad.rb', line 51 def map(&mapper) bind do |v| result = mapper.call v; value(result); end end |
#plus(other) ⇒ Object
Run the plus operation on the encapsulated object following the MonadPlus law.
61 62 63 |
# File 'lib/rparsec/monad.rb', line 61 def plus other @monad.mplus(@this, other.this) end |
#seq(other) ⇒ Object
Run the seq operation on the encapsulated object following the monad law. If seq is not defined by the monad impl, use #bind to implement.
39 40 41 42 43 44 45 |
# File 'lib/rparsec/monad.rb', line 39 def seq(other) if @monad.respond_to? :seq @monad.seq(other) else bind { |_x| other } end end |
#value(v) ⇒ Object
To create a value based on the monad impl.
23 24 25 |
# File 'lib/rparsec/monad.rb', line 23 def value v @monad.value v end |