Fix vec::flat_map_to_vec method

This commit is contained in:
Erick Tryzelaar 2012-12-18 20:48:23 -08:00
parent 97ddf3c7bd
commit 938058b004
2 changed files with 139 additions and 14 deletions

View File

@ -2024,6 +2024,32 @@ impl<A> &[A]: iter::BaseIter<A> {
pure fn size_hint(&self) -> Option<uint> { Some(len(*self)) } pure fn size_hint(&self) -> Option<uint> { Some(len(*self)) }
} }
// FIXME(#4148): This should be redundant
impl<A> ~[A]: iter::BaseIter<A> {
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<uint> { Some(len(*self)) }
}
// FIXME(#4148): This should be redundant
impl<A> @[A]: iter::BaseIter<A> {
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<uint> { Some(len(*self)) }
}
impl<A> &[A]: iter::ExtendedIter<A> { impl<A> &[A]: iter::ExtendedIter<A> {
pub pure fn eachi(&self, blk: fn(uint, v: &A) -> bool) { pub pure fn eachi(&self, blk: fn(uint, v: &A) -> bool) {
iter::eachi(self, blk) iter::eachi(self, blk)
@ -2049,11 +2075,75 @@ impl<A> &[A]: iter::ExtendedIter<A> {
} }
} }
// FIXME(#4148): This should be redundant
impl<A> ~[A]: iter::ExtendedIter<A> {
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<B>(&self, b0: B, blk: fn(&B, &A) -> B) -> B {
iter::foldl(self, b0, blk)
}
pub pure fn position(&self, f: fn(&A) -> bool) -> Option<uint> {
iter::position(self, f)
}
pure fn map_to_vec<B>(&self, op: fn(&A) -> B) -> ~[B] {
iter::map_to_vec(self, op)
}
pure fn flat_map_to_vec<B,IB:BaseIter<B>>(&self, op: fn(&A) -> IB)
-> ~[B] {
iter::flat_map_to_vec(self, op)
}
}
// FIXME(#4148): This should be redundant
impl<A> @[A]: iter::ExtendedIter<A> {
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<B>(&self, b0: B, blk: fn(&B, &A) -> B) -> B {
iter::foldl(self, b0, blk)
}
pub pure fn position(&self, f: fn(&A) -> bool) -> Option<uint> {
iter::position(self, f)
}
pure fn map_to_vec<B>(&self, op: fn(&A) -> B) -> ~[B] {
iter::map_to_vec(self, op)
}
pure fn flat_map_to_vec<B,IB:BaseIter<B>>(&self, op: fn(&A) -> IB)
-> ~[B] {
iter::flat_map_to_vec(self, op)
}
}
impl<A: Eq> &[A]: iter::EqIter<A> { impl<A: Eq> &[A]: iter::EqIter<A> {
pub pure fn contains(&self, x: &A) -> bool { iter::contains(self, x) } pub pure fn contains(&self, x: &A) -> bool { iter::contains(self, x) }
pub pure fn count(&self, x: &A) -> uint { iter::count(self, x) } pub pure fn count(&self, x: &A) -> uint { iter::count(self, x) }
} }
// FIXME(#4148): This should be redundant
impl<A: Eq> ~[A]: iter::EqIter<A> {
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: Eq> @[A]: iter::EqIter<A> {
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: Copy> &[A]: iter::CopyableIter<A> { impl<A: Copy> &[A]: iter::CopyableIter<A> {
pure fn filter_to_vec(&self, pred: fn(&A) -> bool) -> ~[A] { pure fn filter_to_vec(&self, pred: fn(&A) -> bool) -> ~[A] {
iter::filter_to_vec(self, pred) iter::filter_to_vec(self, pred)
@ -2064,11 +2154,45 @@ impl<A: Copy> &[A]: iter::CopyableIter<A> {
} }
} }
// FIXME(#4148): This should be redundant
impl<A: Copy> ~[A]: iter::CopyableIter<A> {
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<A> {
iter::find(self, f)
}
}
// FIXME(#4148): This should be redundant
impl<A: Copy> @[A]: iter::CopyableIter<A> {
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<A> {
iter::find(self, f)
}
}
impl<A: Copy Ord> &[A]: iter::CopyableOrderedIter<A> { impl<A: Copy Ord> &[A]: iter::CopyableOrderedIter<A> {
pure fn min(&self) -> A { iter::min(self) } pure fn min(&self) -> A { iter::min(self) }
pure fn max(&self) -> A { iter::max(self) } pure fn max(&self) -> A { iter::max(self) }
} }
// FIXME(#4148): This should be redundant
impl<A: Copy Ord> ~[A]: iter::CopyableOrderedIter<A> {
pure fn min(&self) -> A { iter::min(self) }
pure fn max(&self) -> A { iter::max(self) }
}
// FIXME(#4148): This should be redundant
impl<A: Copy Ord> @[A]: iter::CopyableOrderedIter<A> {
pure fn min(&self) -> A { iter::min(self) }
pure fn max(&self) -> A { iter::max(self) }
}
impl<A:Copy> &[A] : iter::CopyableNonstrictIter<A> { impl<A:Copy> &[A] : iter::CopyableNonstrictIter<A> {
pure fn each_val(&const self, f: fn(A) -> bool) { pure fn each_val(&const self, f: fn(A) -> bool) {
let mut i = 0; let mut i = 0;
@ -2079,6 +2203,7 @@ impl<A:Copy> &[A] : iter::CopyableNonstrictIter<A> {
} }
} }
// FIXME(#4148): This should be redundant
impl<A:Copy> ~[A] : iter::CopyableNonstrictIter<A> { impl<A:Copy> ~[A] : iter::CopyableNonstrictIter<A> {
pure fn each_val(&const self, f: fn(A) -> bool) { pure fn each_val(&const self, f: fn(A) -> bool) {
let mut i = 0; let mut i = 0;
@ -2089,6 +2214,7 @@ impl<A:Copy> ~[A] : iter::CopyableNonstrictIter<A> {
} }
} }
// FIXME(#4148): This should be redundant
impl<A:Copy> @[A] : iter::CopyableNonstrictIter<A> { impl<A:Copy> @[A] : iter::CopyableNonstrictIter<A> {
pure fn each_val(&const self, f: fn(A) -> bool) { pure fn each_val(&const self, f: fn(A) -> bool) {
let mut i = 0; let mut i = 0;

View File

@ -8,23 +8,22 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // 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<uint> { fn incd_if_even(x: &uint) -> Option<uint> {
if (x % 2u) == 0u {some(x + 1u)} else {none} if (*x % 2u) == 0u {Some(*x + 1u)} else {None}
} }
fn main() { fn main() {
assert ~[1u, 3u].flat_map_to_vec(repeat) == ~[1u, 1u, 3u, 3u]; assert (~[1u, 3u]).flat_map_to_vec(repeat) == ~[1u, 1u, 3u, 3u];
assert ~[].flat_map_to_vec(repeat) == ~[]; assert (~[]).flat_map_to_vec(repeat) == ~[];
assert none.flat_map_to_vec(repeat) == ~[]; assert None.flat_map_to_vec(repeat) == ~[];
assert some(1u).flat_map_to_vec(repeat) == ~[1u, 1u]; assert Some(1u).flat_map_to_vec(repeat) == ~[1u, 1u];
assert some(2u).flat_map_to_vec(repeat) == ~[2u, 2u]; assert Some(2u).flat_map_to_vec(repeat) == ~[2u, 2u];
assert ~[1u, 2u, 5u].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 (~[]).flat_map_to_vec(incd_if_even) == ~[];
assert none.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(1u).flat_map_to_vec(incd_if_even) == ~[];
assert some(2u).flat_map_to_vec(incd_if_even) == ~[3u]; assert Some(2u).flat_map_to_vec(incd_if_even) == ~[3u];
} }