Skip to content

Commit fb4139a

Browse files
huonwthestinger
authored andcommitted
---
yaml --- r: 72119 b: refs/heads/dist-snap c: 90313b7 h: refs/heads/master i: 72117: d0dea8d 72115: 6b47fa7 72111: 20d9f07 v: v3
1 parent 91841b1 commit fb4139a

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
@@ -7,7 +7,7 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
99
refs/heads/incoming: b50030718cf28f2a5a81857a26b57442734fe854
10-
refs/heads/dist-snap: d7a2ae6c42f1d9755178485fd93f234c2df8a8fe
10+
refs/heads/dist-snap: 90313b789c1d057dcc4aeed0374359f4927214c5
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1313
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/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)