I'm doing a test with generators, to split a string into tokens . But the result obtained is not as expected:
function *stringIterator( str ) {
for( const ch of str ) {
yield ch
}
}
function parseString( iterator ) {
var value = '';
for( const ch of iterator ) {
if( ch == "'" ) { break; }
value += ch;
}
return { value };
}
function *tokenGenerator( input ) {
iterator = stringIterator( input );
for( const ch of iterator ) {
switch( ch ) {
case "'":
yield parseString( iterator );
break;
}
}
}
for( const token of tokenGenerator( "'hola, mundo''hadios'" ) ) {
console.log( token );
}
The value obtained by console is
{ value: 'hello, world' }
However, what I hope to get is
{ value: 'hello, world' }
{ value: 'goodbye' }
There are 3 functions involved, 2 of them being generators and the third being a normal function :
function *stringIterator( )
Generator that loops through a string, returning one character at a time.
function parseString( )
Normal function that, using the first, has to accumulate characters in a string, and return the accumulated string when the character found is a
'
(single quote).function *tokenGenerator( )
Generator function that creates a generator (
1
) from a string and should repeatedly call2
.
From the results obtained, it seems that as much 1
as they 2
work correctly, they fail 3
(or my way of calling it).
What am I doing wrong ?
How do I solve it ?
The problem is that inside
parseString()
you are iterating again withfor(...)
and "breaking" the generator loop.Within a while loop , you must continue iterating with
.next()
to preserve the functionality of the generator, knowing that the function returns a result with two properties:value
, for each value in iteration anddone
, true when it ends or false when there are still pending values.Reference: Yield - Description