-
Notifications
You must be signed in to change notification settings - Fork 5
Complete API for Apep
Run a generator to completion, combining results into a string.
-
g
- Generator to run. -
ud
- Optional, user data. -
r
- Optional, custom random number generator.
const p = pep.seq('a', pep.choice('b', 'c'));
p.run() === 'ac';
p.run() === 'ac';
p.run() === 'ab';
Left fold over a generator.
-
f
- Accumulation function, passed accumulated value and current value. -
z
- Initial value. -
g
- Generator to run. -
ud
- Optional, user data. -
r
- Optional, custom random number generator.
const p = pep.seq('a', 'b', 'c');
pep.fold((p, c) => p.concat(c), [], p) === ['a', 'b', 'c'];
Begin the execution of a generator. Returns a Javascript iterator.
-
g
- Generator to run. -
ud
- Optional, user data. -
r
- Optional, custom random number generator.
Values are lazily produced. Javascript iterators are stateful, so you can only iterate over the result of begin
once.
const p = pep.seq('a', 'b', 'c');
for (const x of p.begin())
console.log(x);
Yields x
.
pep.lit('abc').run() === 'abc';
pep.lit(5).exec().next().value === 5;
Use lit
to force a generator to yield a non string value.
// Normally, `seq` will attempt to convert `myObj` to a string.
Array.from(pep.seq('a', myObj, 'c')) === ['a', '[Object object]', 'c'];
// But `lit`forces the object to be treated as a literal value.
Array.from(pep.seq('a', pep.lit(myObj), 'c')) === ['a', myObj, 'c'];
Yields x
as a string. x
is converted to a string when str
is first called.
pep.str('abc').run() === 'abc';
pep.str(5).exec().next().value === '5';
pep.str({}).exec().next().value === '[Object object]';
Note that pep.str('')
is different from pep.empty
. The former yield the value ''
while the latter does not yield any values.
Generator that does not yield any values. This can be used with choice
to add the possibility that nothing will be output.
const p = pep.seq('a', pep.empty, 'b');
Array.from(p.begin()) === ['a', 'b'];
For most cases, empty
is the preferred way to indicate that no value is yielded, rather than str('')
.