Class: Emu::Decoder
- Inherits:
-
Object
- Object
- Emu::Decoder
- Defined in:
- lib/emu/decoder.rb
Instance Method Summary collapse
- #>(value) ⇒ Object
- #fmap ⇒ Object
-
#initialize(&block) ⇒ Decoder
constructor
A new instance of Decoder.
- #run(value) ⇒ Object
- #run!(value) ⇒ Object
- #then ⇒ Object
- #|(decoder) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Decoder
Returns a new instance of Decoder.
6 7 8 |
# File 'lib/emu/decoder.rb', line 6 def initialize(&block) @f = block end |
Instance Method Details
#>(value) ⇒ Object
53 54 55 |
# File 'lib/emu/decoder.rb', line 53 def >(value) fmap { |_| value } end |
#fmap ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/emu/decoder.rb', line 23 def fmap Decoder.new do |input| result = run(input) if result.error? result else Ok.new(yield result.unwrap) end end end |
#run(value) ⇒ Object
10 11 12 |
# File 'lib/emu/decoder.rb', line 10 def run(value) @f.call(value) end |
#run!(value) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/emu/decoder.rb', line 14 def run!(value) result = run(value) if result.error? raise DecodeError, result.unwrap_err else result.unwrap end end |