From 938058b0040e3c482e10b78eeef7afb941b2b64e Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Tue, 18 Dec 2012 20:48:23 -0800 Subject: [PATCH] Fix vec::flat_map_to_vec method --- src/libcore/vec.rs | 126 ++++++++++++++++++++++ src/test/run-pass/iter-flat-map-to-vec.rs | 27 +++-- 2 files changed, 139 insertions(+), 14 deletions(-) diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index c091f48728f..5071fb903d9 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -2024,6 +2024,32 @@ impl &[A]: iter::BaseIter { pure fn size_hint(&self) -> Option { Some(len(*self)) } } +// FIXME(#4148): This should be redundant +impl ~[A]: iter::BaseIter { + pub pure fn each(&self, blk: fn(v: &A) -> bool) { + // FIXME(#2263)---should be able to call each(self, blk) + for each(*self) |e| { + if (!blk(e)) { + return; + } + } + } + pure fn size_hint(&self) -> Option { Some(len(*self)) } +} + +// FIXME(#4148): This should be redundant +impl @[A]: iter::BaseIter { + pub pure fn each(&self, blk: fn(v: &A) -> bool) { + // FIXME(#2263)---should be able to call each(self, blk) + for each(*self) |e| { + if (!blk(e)) { + return; + } + } + } + pure fn size_hint(&self) -> Option { Some(len(*self)) } +} + impl &[A]: iter::ExtendedIter { pub pure fn eachi(&self, blk: fn(uint, v: &A) -> bool) { iter::eachi(self, blk) @@ -2049,11 +2075,75 @@ impl &[A]: iter::ExtendedIter { } } +// FIXME(#4148): This should be redundant +impl ~[A]: iter::ExtendedIter { + pub pure fn eachi(&self, blk: fn(uint, v: &A) -> bool) { + iter::eachi(self, blk) + } + pub pure fn all(&self, blk: fn(&A) -> bool) -> bool { + iter::all(self, blk) + } + pub pure fn any(&self, blk: fn(&A) -> bool) -> bool { + iter::any(self, blk) + } + pub pure fn foldl(&self, b0: B, blk: fn(&B, &A) -> B) -> B { + iter::foldl(self, b0, blk) + } + pub pure fn position(&self, f: fn(&A) -> bool) -> Option { + iter::position(self, f) + } + pure fn map_to_vec(&self, op: fn(&A) -> B) -> ~[B] { + iter::map_to_vec(self, op) + } + pure fn flat_map_to_vec>(&self, op: fn(&A) -> IB) + -> ~[B] { + iter::flat_map_to_vec(self, op) + } +} + +// FIXME(#4148): This should be redundant +impl @[A]: iter::ExtendedIter { + pub pure fn eachi(&self, blk: fn(uint, v: &A) -> bool) { + iter::eachi(self, blk) + } + pub pure fn all(&self, blk: fn(&A) -> bool) -> bool { + iter::all(self, blk) + } + pub pure fn any(&self, blk: fn(&A) -> bool) -> bool { + iter::any(self, blk) + } + pub pure fn foldl(&self, b0: B, blk: fn(&B, &A) -> B) -> B { + iter::foldl(self, b0, blk) + } + pub pure fn position(&self, f: fn(&A) -> bool) -> Option { + iter::position(self, f) + } + pure fn map_to_vec(&self, op: fn(&A) -> B) -> ~[B] { + iter::map_to_vec(self, op) + } + pure fn flat_map_to_vec>(&self, op: fn(&A) -> IB) + -> ~[B] { + iter::flat_map_to_vec(self, op) + } +} + impl &[A]: iter::EqIter { pub pure fn contains(&self, x: &A) -> bool { iter::contains(self, x) } pub pure fn count(&self, x: &A) -> uint { iter::count(self, x) } } +// FIXME(#4148): This should be redundant +impl ~[A]: iter::EqIter { + pub pure fn contains(&self, x: &A) -> bool { iter::contains(self, x) } + pub pure fn count(&self, x: &A) -> uint { iter::count(self, x) } +} + +// FIXME(#4148): This should be redundant +impl @[A]: iter::EqIter { + pub pure fn contains(&self, x: &A) -> bool { iter::contains(self, x) } + pub pure fn count(&self, x: &A) -> uint { iter::count(self, x) } +} + impl &[A]: iter::CopyableIter { pure fn filter_to_vec(&self, pred: fn(&A) -> bool) -> ~[A] { iter::filter_to_vec(self, pred) @@ -2064,11 +2154,45 @@ impl &[A]: iter::CopyableIter { } } +// FIXME(#4148): This should be redundant +impl ~[A]: iter::CopyableIter { + pure fn filter_to_vec(&self, pred: fn(&A) -> bool) -> ~[A] { + iter::filter_to_vec(self, pred) + } + pure fn to_vec(&self) -> ~[A] { iter::to_vec(self) } + pub pure fn find(&self, f: fn(&A) -> bool) -> Option { + iter::find(self, f) + } +} + +// FIXME(#4148): This should be redundant +impl @[A]: iter::CopyableIter { + pure fn filter_to_vec(&self, pred: fn(&A) -> bool) -> ~[A] { + iter::filter_to_vec(self, pred) + } + pure fn to_vec(&self) -> ~[A] { iter::to_vec(self) } + pub pure fn find(&self, f: fn(&A) -> bool) -> Option { + iter::find(self, f) + } +} + impl &[A]: iter::CopyableOrderedIter { pure fn min(&self) -> A { iter::min(self) } pure fn max(&self) -> A { iter::max(self) } } +// FIXME(#4148): This should be redundant +impl ~[A]: iter::CopyableOrderedIter { + pure fn min(&self) -> A { iter::min(self) } + pure fn max(&self) -> A { iter::max(self) } +} + +// FIXME(#4148): This should be redundant +impl @[A]: iter::CopyableOrderedIter { + pure fn min(&self) -> A { iter::min(self) } + pure fn max(&self) -> A { iter::max(self) } +} + impl &[A] : iter::CopyableNonstrictIter { pure fn each_val(&const self, f: fn(A) -> bool) { let mut i = 0; @@ -2079,6 +2203,7 @@ impl &[A] : iter::CopyableNonstrictIter { } } +// FIXME(#4148): This should be redundant impl ~[A] : iter::CopyableNonstrictIter { pure fn each_val(&const self, f: fn(A) -> bool) { let mut i = 0; @@ -2089,6 +2214,7 @@ impl ~[A] : iter::CopyableNonstrictIter { } } +// FIXME(#4148): This should be redundant impl @[A] : iter::CopyableNonstrictIter { pure fn each_val(&const self, f: fn(A) -> bool) { let mut i = 0; diff --git a/src/test/run-pass/iter-flat-map-to-vec.rs b/src/test/run-pass/iter-flat-map-to-vec.rs index 2177066a033..a90206188e3 100644 --- a/src/test/run-pass/iter-flat-map-to-vec.rs +++ b/src/test/run-pass/iter-flat-map-to-vec.rs @@ -8,23 +8,22 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// xfail-test -- flat_map_to_vec currently disable -fn repeat(x: &uint) -> ~[uint] { ~[x, x] } +fn repeat(x: &uint) -> ~[uint] { ~[*x, *x] } -fn incd_if_even(x: &uint) -> option { - if (x % 2u) == 0u {some(x + 1u)} else {none} +fn incd_if_even(x: &uint) -> Option { + if (*x % 2u) == 0u {Some(*x + 1u)} else {None} } fn main() { - assert ~[1u, 3u].flat_map_to_vec(repeat) == ~[1u, 1u, 3u, 3u]; - assert ~[].flat_map_to_vec(repeat) == ~[]; - assert none.flat_map_to_vec(repeat) == ~[]; - assert some(1u).flat_map_to_vec(repeat) == ~[1u, 1u]; - assert some(2u).flat_map_to_vec(repeat) == ~[2u, 2u]; + assert (~[1u, 3u]).flat_map_to_vec(repeat) == ~[1u, 1u, 3u, 3u]; + assert (~[]).flat_map_to_vec(repeat) == ~[]; + assert None.flat_map_to_vec(repeat) == ~[]; + assert Some(1u).flat_map_to_vec(repeat) == ~[1u, 1u]; + assert Some(2u).flat_map_to_vec(repeat) == ~[2u, 2u]; - assert ~[1u, 2u, 5u].flat_map_to_vec(incd_if_even) == ~[3u]; - assert ~[].flat_map_to_vec(incd_if_even) == ~[]; - assert none.flat_map_to_vec(incd_if_even) == ~[]; - assert some(1u).flat_map_to_vec(incd_if_even) == ~[]; - assert some(2u).flat_map_to_vec(incd_if_even) == ~[3u]; + assert (~[1u, 2u, 5u]).flat_map_to_vec(incd_if_even) == ~[3u]; + assert (~[]).flat_map_to_vec(incd_if_even) == ~[]; + assert None.flat_map_to_vec(incd_if_even) == ~[]; + assert Some(1u).flat_map_to_vec(incd_if_even) == ~[]; + assert Some(2u).flat_map_to_vec(incd_if_even) == ~[3u]; }