Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
303: Introduce peek_nth r=jswrenn a=davidcornu I've been implementing Bob Nystrom's [Crafting Interpreters](https://www.craftinginterpreters.com) in Rust as a way to learn the language and stumbled upon [`multipeek`](https://docs.rs/itertools/0.7.8/itertools/fn.multipeek.html) while looking for a way to `peek` more than one value ahead (as is required in order to [tokenize number literals](http://www.craftinginterpreters.com/scanning.html#number-literals)). `multipeek` seemed like a great solution to this but I find it's tricky to use correctly. Because it makes `peek` stateful, you have to be careful to call [`reset_peek`](https://docs.rs/itertools/0.7.8/itertools/structs/struct.MultiPeek.html#method.reset_peek) whenever handing control of the iterator to another part of the code or risk running into bugs when `peek` doesn't return what you expect. To work around this I implemented `peek_nth` which allows you to `peek` 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** ```rust let xs = vec![1,2,3]; let mut iter = peek_nth(xs.iter()); assert_eq!(iter.peek(), Some(&&1)); assert_eq!(iter.next(), Some(&1)); // The iterator does not advance even if we call `peek_nth` multiple times assert_eq!(iter.peek_nth(0), Some(&&2)); assert_eq!(iter.peek_nth(1), Some(&&3)); assert_eq!(iter.next(), Some(&2)); // Calling `peek_nth` past the end of the iterator will return `None` assert_eq!(iter.peek_nth(1), None); ``` Note - `peek` equivalent to `peek_nth(0)` and is included for convenience. ----⚠️ I'm _very_ new to Rust and took a lot of inspiration from`multipeek` to implement this. I'm more than willing to rewrite/fix any mistakes I may have made 😅. Co-authored-by: David Cornu <me@davidcornu.com>
- Loading branch information
Showing
5 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use crate::size_hint; | ||
use crate::PeekingNext; | ||
use std::collections::VecDeque; | ||
use std::iter::Fuse; | ||
|
||
/// See [`peek_nth()`](../fn.peek_nth.html) for more information. | ||
#[derive(Clone, Debug)] | ||
pub struct PeekNth<I> | ||
where | ||
I: Iterator, | ||
{ | ||
iter: Fuse<I>, | ||
buf: VecDeque<I::Item>, | ||
} | ||
|
||
/// A drop-in replacement for `std::iter::Peekable` which adds a `peek_nth` | ||
/// method allowing the user to `peek` at a value several iterations forward | ||
/// without advancing the base iterator. | ||
/// | ||
/// This differs from `multipeek` in that subsequent calls to `peek` or | ||
/// `peek_nth` will always return the same value until `next` is called | ||
/// (making `reset_peek` unnecessary). | ||
pub fn peek_nth<I>(iterable: I) -> PeekNth<I::IntoIter> | ||
where | ||
I: IntoIterator, | ||
{ | ||
PeekNth { | ||
iter: iterable.into_iter().fuse(), | ||
buf: VecDeque::new(), | ||
} | ||
} | ||
|
||
impl<I> PeekNth<I> | ||
where | ||
I: Iterator, | ||
{ | ||
/// Works exactly like the `peek` method in `std::iter::Peekable` | ||
pub fn peek(&mut self) -> Option<&I::Item> { | ||
self.peek_nth(0) | ||
} | ||
|
||
/// Returns a reference to the `nth` value without advancing the iterator. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Basic usage: | ||
/// | ||
/// ```rust | ||
/// use itertools::peek_nth; | ||
/// | ||
/// let xs = vec![1,2,3]; | ||
/// let mut iter = peek_nth(xs.iter()); | ||
/// | ||
/// assert_eq!(iter.peek_nth(0), Some(&&1)); | ||
/// assert_eq!(iter.next(), Some(&1)); | ||
/// | ||
/// // The iterator does not advance even if we call `peek_nth` multiple times | ||
/// assert_eq!(iter.peek_nth(0), Some(&&2)); | ||
/// assert_eq!(iter.peek_nth(1), Some(&&3)); | ||
/// assert_eq!(iter.next(), Some(&2)); | ||
/// | ||
/// // Calling `peek_nth` past the end of the iterator will return `None` | ||
/// assert_eq!(iter.peek_nth(1), None); | ||
/// ``` | ||
pub fn peek_nth(&mut self, n: usize) -> Option<&I::Item> { | ||
let unbuffered_items = (n + 1).saturating_sub(self.buf.len()); | ||
|
||
self.buf.extend(self.iter.by_ref().take(unbuffered_items)); | ||
|
||
self.buf.get(n) | ||
} | ||
} | ||
|
||
impl<I> Iterator for PeekNth<I> | ||
where | ||
I: Iterator, | ||
{ | ||
type Item = I::Item; | ||
|
||
fn next(&mut self) -> Option<I::Item> { | ||
self.buf.pop_front().or_else(|| self.iter.next()) | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
size_hint::add_scalar(self.iter.size_hint(), self.buf.len()) | ||
} | ||
} | ||
|
||
impl<I> ExactSizeIterator for PeekNth<I> where I: ExactSizeIterator {} | ||
|
||
impl<I> PeekingNext for PeekNth<I> | ||
where | ||
I: Iterator, | ||
{ | ||
fn peeking_next<F>(&mut self, accept: F) -> Option<Self::Item> | ||
where | ||
F: FnOnce(&Self::Item) -> bool, | ||
{ | ||
self.peek().filter(|item| accept(item))?; | ||
self.next() | ||
} | ||
} |
This file contains 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
This file contains 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