Class: Emu::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/emu/decoder.rb

Instance Method Summary collapse

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

#fmapObject



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

#thenObject



34
35
36
37
38
39
40
# File 'lib/emu/decoder.rb', line 34

def then
  Decoder.new do |input|
    run(input).then do |result|
      (yield result).run(input)
    end
  end
end

#|(decoder) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/emu/decoder.rb', line 42

def |(decoder)
  Decoder.new do |input|
    result = run(input)
    if result.error?
      decoder.run(input)
    else
      result
    end
  end
end