about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2012-02-12 02:44:40 -0800
committerBrian Anderson <banderson@mozilla.com>2012-02-12 19:24:24 -0800
commit85175d639f0b40062d2741f4918df48aee3ef758 (patch)
tree4da18db0e5df31541dcab6776744a82278127fe0
parentc21db3bbc28c47e846783dedfe1eb1228f955f2b (diff)
downloadrust-85175d639f0b40062d2741f4918df48aee3ef758.tar.gz
rust-85175d639f0b40062d2741f4918df48aee3ef758.zip
core: Add iter::reverse
-rw-r--r--src/libcore/iter.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs
index 23779e04487..8500ce7a783 100644
--- a/src/libcore/iter.rs
+++ b/src/libcore/iter.rs
@@ -77,6 +77,11 @@ fn to_list<A:copy,IA:iterable<A>>(self: IA) -> [A] {
     foldl::<A,[A],IA>(self, [], {|r, a| r + [a]})
 }
 
+// FIXME: This could be made more efficient with an riterable interface
+fn reverse<A:copy,IA:iterable<A>>(self: IA, blk: fn(A)) {
+    vec::riter(to_list(self), blk)
+}
+
 fn repeat(times: uint, blk: fn()) {
     let i = 0u;
     while i < times {
@@ -214,3 +219,8 @@ fn test_max() {
 fn test_max_empty() {
     max::<int, [int]>([]);
 }
+
+#[test]
+fn test_reverse() {
+    assert to_list(bind reverse([1, 2, 3], _)) == [3, 2, 1];
+}
\ No newline at end of file