Skip to content

Commit 0b49d8b

Browse files
author
Erik Rhodes
committed
changed name to take_while_inclusive and reversed semantics back to match take_while
1 parent 681ed7a commit 0b49d8b

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

src/lib.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ pub mod structs {
146146
pub use crate::repeatn::RepeatN;
147147
#[allow(deprecated)]
148148
pub use crate::sources::{RepeatCall, Unfold, Iterate};
149-
pub use crate::take_until::TakeUntil;
149+
pub use crate::take_while_inclusive::TakeWhileInclusive;
150150
#[cfg(feature = "use_alloc")]
151151
pub use crate::tee::Tee;
152152
pub use crate::tuple_impl::{TupleBuffer, TupleWindows, CircularTupleWindows, Tuples};
@@ -234,7 +234,7 @@ mod rciter_impl;
234234
mod repeatn;
235235
mod size_hint;
236236
mod sources;
237-
mod take_until;
237+
mod take_while_inclusive;
238238
#[cfg(feature = "use_alloc")]
239239
mod tee;
240240
mod tuple_impl;
@@ -1392,20 +1392,15 @@ pub trait Itertools : Iterator {
13921392
}
13931393

13941394
/// Returns an iterator adaptor that consumes elements while the given
1395-
/// predicate is `false`, *including* the element for which the predicate
1396-
/// first returned `true`.
1395+
/// predicate is `true`, *including* the element for which the predicate
1396+
/// first returned `false`.
13971397
///
13981398
/// The [`.take_while()`][std::iter::Iterator::take_while] adaptor is useful
13991399
/// when you want items satisfying a predicate, but to know when to stop
14001400
/// taking elements, we have to consume that last element that doesn't
14011401
/// satisfy the predicate. This adaptor includes that element where
14021402
/// [`.take_while()`][std::iter::Iterator::take_while] would drop it.
14031403
///
1404-
/// Note that the semantics of this predicate are reversed from
1405-
/// [`.take_while()`][std::iter::Iterator::take_while], i.e. this function's
1406-
/// predicate yields elements when it evaluates to `false` instead of when
1407-
/// it evaluates to `true`.
1408-
///
14091404
/// The [`.take_while_ref()`][crate::Itertools::take_while_ref] adaptor
14101405
/// serves a similar purpose, but this adaptor doesn't require [`Clone`]ing
14111406
/// the underlying elements.
@@ -1414,7 +1409,10 @@ pub trait Itertools : Iterator {
14141409
/// # use itertools::Itertools;
14151410
///
14161411
/// let items = vec![1, 2, 3, 4, 5];
1417-
/// let filtered: Vec<_> = items.into_iter().take_until(|&n| n % 3 == 0).collect();
1412+
/// let filtered: Vec<_> = items
1413+
/// .into_iter()
1414+
/// .take_while_inclusive(|&n| n % 3 != 0)
1415+
/// .collect();
14181416
///
14191417
/// assert_eq!(filtered, vec![1, 2, 3]);
14201418
/// ```
@@ -1426,7 +1424,7 @@ pub trait Itertools : Iterator {
14261424
/// let take_until_result: Vec<_> = items
14271425
/// .clone()
14281426
/// .into_iter()
1429-
/// .take_until(|&n| n % 3 == 0)
1427+
/// .take_while_inclusive(|&n| n % 3 != 0)
14301428
/// .collect();
14311429
/// let take_while_result: Vec<_> = items
14321430
/// .into_iter()
@@ -1448,16 +1446,16 @@ pub trait Itertools : Iterator {
14481446
/// .collect();
14491447
/// let filtered: Vec<_> = non_clonable_items
14501448
/// .into_iter()
1451-
/// .take_until(|n| n.0 % 3 == 0)
1449+
/// .take_while_inclusive(|n| n.0 % 3 != 0)
14521450
/// .collect();
14531451
/// let expected: Vec<_> = vec![1, 2, 3].into_iter().map(NoCloneImpl).collect();
14541452
/// assert_eq!(filtered, expected);
1455-
fn take_until<F>(&mut self, accept: F) -> TakeUntil<Self, F>
1453+
fn take_while_inclusive<F>(&mut self, accept: F) -> TakeWhileInclusive<Self, F>
14561454
where
14571455
Self: Sized,
14581456
F: FnMut(&Self::Item) -> bool,
14591457
{
1460-
take_until::TakeUntil::new(self, accept)
1458+
take_while_inclusive::TakeWhileInclusive::new(self, accept)
14611459
}
14621460

14631461
/// Return an iterator adaptor that filters `Option<A>` iterator elements
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
use core::iter::FusedIterator;
22
use std::fmt;
33

4-
/// An iterator adaptor that consumes elements while the given predicate is false, including the
5-
/// element for which the predicate first returned true.
4+
/// An iterator adaptor that consumes elements while the given predicate is `true`, including the
5+
/// element for which the predicate first returned `false`.
66
///
7-
/// See [`.take_until()`](crate::Itertools::take_until) for more information.
7+
/// See [`.take_while_inclusive()`](crate::Itertools::take_while_inclusive) for more information.
88
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
9-
pub struct TakeUntil<'a, I: 'a, F> {
9+
pub struct TakeWhileInclusive<'a, I: 'a, F> {
1010
iter: &'a mut I,
1111
f: F,
1212
done: bool,
1313
}
1414

15-
impl<'a, I, F> TakeUntil<'a, I, F>
15+
impl<'a, I, F> TakeWhileInclusive<'a, I, F>
1616
where
1717
I: Iterator,
1818
F: FnMut(&I::Item) -> bool,
1919
{
20-
/// Create a new [`TakeUntil`] from an iterator and a predicate.
20+
/// Create a new [`TakeWhileInclusive`] from an iterator and a predicate.
2121
pub fn new(iter: &'a mut I, f: F) -> Self {
2222
Self { iter, f, done: false}
2323
}
2424
}
2525

26-
impl<'a, I, F> fmt::Debug for TakeUntil<'a, I, F>
26+
impl<'a, I, F> fmt::Debug for TakeWhileInclusive<'a, I, F>
2727
where I: Iterator + fmt::Debug,
2828
{
29-
debug_fmt_fields!(TakeUntil, iter);
29+
debug_fmt_fields!(TakeWhileInclusive, iter);
3030
}
3131

32-
impl<'a, I, F> Iterator for TakeUntil<'a, I, F>
32+
impl<'a, I, F> Iterator for TakeWhileInclusive<'a, I, F>
3333
where
3434
I: Iterator,
3535
F: FnMut(&I::Item) -> bool
@@ -41,7 +41,7 @@ where
4141
None
4242
} else {
4343
self.iter.next().map(|item| {
44-
if (self.f)(&item) {
44+
if !(self.f)(&item) {
4545
self.done = true;
4646
}
4747
item
@@ -58,7 +58,7 @@ where
5858
}
5959
}
6060

61-
impl<I, F> FusedIterator for TakeUntil<'_, I, F>
61+
impl<I, F> FusedIterator for TakeWhileInclusive<'_, I, F>
6262
where
6363
I: Iterator,
6464
F: FnMut(&I::Item) -> bool

0 commit comments

Comments
 (0)