more idiomatic iterator use#1145
Open
rogpeppe wants to merge 1 commit intoalecthomas:aat/stdlib-iteratorfrom
Open
more idiomatic iterator use#1145rogpeppe wants to merge 1 commit intoalecthomas:aat/stdlib-iteratorfrom
rogpeppe wants to merge 1 commit intoalecthomas:aat/stdlib-iteratorfrom
Conversation
nerdsnipe alert: I'm still in relatively early phases of exploring iterator idiom, so couldn't resist having a go at simplifying SplitTokensIntoLines which is now fully streaming (even though none of the call sites look like they can take advantage of that currently). Incidentally, it's now O(n) not O(n^2) too (that `for strings.Contains` looked expensive). I removed the `EOF` variable, replacing it with an `IsZero` method which can be used to check if a `Token` is valid or not. I don't see anywhere that seems to rely on yielding an EOF value: all the tests pass. Signed-off-by: Roger Peppe <[email protected]>
Author
|
One other thought: a possibly simpler way to write I don't think I've got the semantics quite right (the tests fail) but it seems a bit more conceptually simple than merging two token streams. Also with the original approach, ISTM there was no way for a root lexer to be able to distinguish between two independent sections because all the text is mashed together into Aside: idiomatic Go tends to use American spelling, so I'm used to typing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
NOTE: this PR is targeted at the stdlib-iterator branch as a potential improved v2 API, not current tip.
nerdsnipe alert: I'm still in relatively early phases of exploring iterator idiom, so couldn't resist having a go at simplifying SplitTokensIntoLines which is now fully streaming (even though none of the call sites look like they can take advantage of that currently).
I removed the
EOFvariable, replacing it with anIsZeromethod which can be used to check if aTokenis valid or not. (I don't see anywhere that seems to rely on yielding an EOF value: all the tests pass.)Aside: one convention from the stdlib I realise I've absorbed but never actually expressed: iterators are generally named in Go as "sequences", so perhaps use
chroma.Seqrather thanchroma.Iterator(also has the advantage that it's shorter), or just inlineiter.Seq[chroma.Token]everywhere - it's not that long and it's super-clear - and removeIteratorentirely.