Method: RPEG#Cf

Defined in:
lib/rpeg/rpeg.rb

#Cf(pattern, lambda) ⇒ Object

From LPEG docs:

Creates a fold capture. If patt produces a list of captures C1 C2 ... Cn, this capture will produce the value
func(...func(func(C1, C2), C3)..., Cn), that is, it will fold (or accumulate, or reduce) the captures from patt using
function func.

The second argument should be a lambda taking two arguments and returning one in the standard way.

The first nested capture is examined. If there isn’t one or it captures no values there is an error. If this capture contains more than one value all but the first are discarded. This is the initial value for the fold. Then we extract the remaining nested captures and use their values C2, …, Cn in the fold as described.

If Ci (i >= 2) produces k values then the lambda will receive k+1 arguments: the accumulator and the k captured values. an array.



240
241
242
243
244
# File 'lib/rpeg/rpeg.rb', line 240

def Cf(pattern, lambda)
  raise "Fold capture must have an accumulation lambda" unless lambda

  Pattern.new(Pattern::CAPTURE, P(pattern), data: lambda, capture: Capture::FOLD)
end