Skip to content

Commit 4595210

Browse files
huonwthestinger
authored andcommitted
---
yaml --- r: 57018 b: refs/heads/try c: 90313b7 h: refs/heads/master v: v3
1 parent 7caaeab commit 4595210

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: c081ffbd1e845687202a975ea2e698b623e5722f
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 79a2b2eafc3c766cecec8a5f76317693bae9ed17
5-
refs/heads/try: d7a2ae6c42f1d9755178485fd93f234c2df8a8fe
5+
refs/heads/try: 90313b789c1d057dcc4aeed0374359f4927214c5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libcore/iterator.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,28 @@ impl<A, T: Iterator<A>> Iterator<A> for TakeIterator<T> {
261261
}
262262
}
263263

264+
pub struct UnfoldrIterator<'self, A, St> {
265+
priv f: &'self fn(&mut St) -> Option<A>,
266+
priv state: St
267+
}
268+
269+
pub impl<'self, A, St> UnfoldrIterator<'self, A, St> {
270+
#[inline]
271+
fn new(f: &'self fn(&mut St) -> Option<A>, initial_state: St) -> UnfoldrIterator<'self, A, St> {
272+
UnfoldrIterator {
273+
f: f,
274+
state: initial_state
275+
}
276+
}
277+
}
278+
279+
impl<'self, A, St> Iterator<A> for UnfoldrIterator<'self, A, St> {
280+
#[inline]
281+
fn next(&mut self) -> Option<A> {
282+
(self.f)(&mut self.state)
283+
}
284+
}
285+
264286
#[cfg(test)]
265287
mod tests {
266288
use super::*;
@@ -326,4 +348,25 @@ mod tests {
326348
}
327349
assert_eq!(i, ys.len());
328350
}
351+
352+
#[test]
353+
fn test_unfoldr() {
354+
fn count(st: &mut uint) -> Option<uint> {
355+
if *st < 10 {
356+
let ret = Some(*st);
357+
*st += 1;
358+
ret
359+
} else {
360+
None
361+
}
362+
}
363+
364+
let mut it = UnfoldrIterator::new(count, 0);
365+
let mut i = 0;
366+
for it.advance |counted| {
367+
assert_eq!(counted, i);
368+
i += 1;
369+
}
370+
assert_eq!(i, 10);
371+
}
329372
}

0 commit comments

Comments
 (0)