Module: TAlgebra::Applicative::Static

Defined in:
lib/t_algebra/applicative.rb

Instance Method Summary collapse

Instance Method Details

#lift_a2(_fa, _fb, &_block) ⇒ Object



9
10
11
# File 'lib/t_algebra/applicative.rb', line 9

def lift_a2(_fa, _fb, &_block)
  raise "Implement .lift_a2 in extending class"
end

#pure(_) ⇒ Object



5
6
7
# File 'lib/t_algebra/applicative.rb', line 5

def pure(_)
  raise "Implement .pure in extending class"
end

#seq_a(*array_of_parsers, **hash_of_parsers) ⇒ Object



13
14
15
16
17
# File 'lib/t_algebra/applicative.rb', line 13

def seq_a(*array_of_parsers, **hash_of_parsers)
  return sequence_array(array_of_parsers) unless array_of_parsers.empty?

  sequence_hash(hash_of_parsers) unless hash_of_parsers.empty?
end

#sequence_array(fbs) ⇒ Applicative<Array>

Parameters:

Returns:



21
22
23
24
25
26
27
28
29
# File 'lib/t_algebra/applicative.rb', line 21

def sequence_array(fbs)
  return pure([]) if fbs.empty?

  fbs.reduce(pure([])) do |prev, fb|
    lift_a2(prev, fb) do |p, b|
      p + [b]
    end
  end
end

#sequence_hash(fbs) ⇒ Applicative<Hash>

Parameters:

Returns:



33
34
35
36
37
38
39
40
41
# File 'lib/t_algebra/applicative.rb', line 33

def sequence_hash(fbs)
  return pure({}) if fbs.empty?

  fbs.reduce(pure({})) do |prev, (k, fb)|
    lift_a2(prev, fb) do |p, b|
      p.merge(k => b)
    end
  end
end