New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce peek_nth #303
Introduce peek_nth #303
Conversation
4750e7c
to
133d698
Compare
Ran my code against clippy and fixed all lints. |
Thanks, looks very thoughtful, I'm sorry I haven't had time to look |
No worries! Let me know if I can help in any way. |
@jswrenn is this feature something that would be of interest? Happy to fix the conflicts and get tests passing again if that's the case. Otherwise feel free to close. |
@davidcornu This is excellent. I'd be happy to merge it if you have time to fix up the conflicts and get the tests running. |
@jswrenn will do |
Done ✅ |
437: Simpl is empty r=jswrenn a=phimuemue Taking inspiration from #303 (comment), I think we could exploit that `pop`/`pop_front` return `Option`. Note: https://godbolt.org/z/WVTD7m seems to indicate that the compiler is doing a good job at optimizing either version. Co-authored-by: philipp <descpl@yahoo.de>
@jswrenn thanks for the comments! Definitely cleaned up the implementation. |
Thanks for the contribution! bors r+ |
Build succeeded: |
I've been implementing Bob Nystrom's Crafting Interpreters in Rust as a way to learn the language and stumbled upon
multipeek
while looking for a way topeek
more than one value ahead (as is required in order to tokenize number literals).multipeek
seemed like a great solution to this but I find it's tricky to use correctly. Because it makespeek
stateful, you have to be careful to callreset_peek
whenever handing control of the iterator to another part of the code or risk running into bugs whenpeek
doesn't return what you expect.To work around this I implemented
peek_nth
which allows you topeek
more than one iteration into the future without relying on a cursor. I have a working version in my project but I figured I'd submit it here for discussion.Example usage
Note -
peek
equivalent topeek_nth(0)
and is included for convenience.multipeek
to implement this. I'm more than willing to rewrite/fix any mistakes I may have made 😅.