rust-itertools / itertools

Extra iterator adaptors, iterator methods, free functions, and macros.
https://docs.rs/itertools/
Apache License 2.0
2.72k stars 309 forks source link

Infinite lookahead adaptor #260

Open Michael-F-Bryan opened 6 years ago

Michael-F-Bryan commented 6 years ago

For one of my projects I recently needed something which lets you peek more than one item ahead (std::iter::Peekable only lets you lookahead by one) so I implemented an InfiniteLookahead adaptor which buffers intermediate objects in a VecDeque internally. This is particularly useful when you're parsing text and your grammar requires 2 or more tokens of lookahead.

Would you be interested in a PR that adds it to the Itertools trait?

(example implementation)

bluss commented 6 years ago

Sounds like it overlaps with multipeek, so reconcile their differences

Michael-F-Bryan commented 6 years ago

Oops. I assumed all iterator adaptors would be implemented on the Itertools trait, so I never knew about multipeek().

It looks like they're doing effectively the same thing, although multipeek() uses a "cursor" which gets moved forward every time you call peek(), while InfiniteLookahead effectively gives you random lookup... Would it be worth adding a lookahead() method to MultiPeek?

bluss commented 6 years ago

See #93. It's best to let your use case guide us here, I don't have one.