Skip to content

Commit 3e01f0d

Browse files
authored
Merge pull request #18 from cuviper/iter-fold
add Iterator::fold to MapDeref and FilterMapDeref
2 parents 7045918 + 7677711 commit 3e01f0d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "streaming-iterator"
3-
version = "0.1.4"
3+
version = "0.1.5"
44
authors = ["Steven Fackler <sfackler@gmail.com>"]
55
license = "MIT/Apache-2.0"
66
description = "Streaming iterators"

src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,19 @@ where
11071107

11081108
None
11091109
}
1110+
1111+
#[inline]
1112+
fn fold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
1113+
where
1114+
Self: Sized,
1115+
Fold: FnMut(Acc, Self::Item) -> Acc,
1116+
{
1117+
let mut map = self.f;
1118+
self.it.fold(init, move |acc, item| match map(item) {
1119+
Some(mapped) => f(acc, mapped),
1120+
None => acc,
1121+
})
1122+
}
11101123
}
11111124

11121125
impl<I, B, F> DoubleEndedIterator for FilterMapDeref<I, F>
@@ -1375,6 +1388,16 @@ where
13751388
fn size_hint(&self) -> (usize, Option<usize>) {
13761389
self.it.size_hint()
13771390
}
1391+
1392+
#[inline]
1393+
fn fold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
1394+
where
1395+
Self: Sized,
1396+
Fold: FnMut(Acc, Self::Item) -> Acc,
1397+
{
1398+
let mut map = self.f;
1399+
self.it.fold(init, move |acc, item| f(acc, map(item)))
1400+
}
13781401
}
13791402

13801403
impl<I, B, F> DoubleEndedIterator for MapDeref<I, F>

0 commit comments

Comments
 (0)