Demoding in iter: any, all, map_to_vec, flat_map_to_vec, filter_to_vec

This commit is contained in:
Tim Chevalier 2012-09-28 17:04:39 -07:00
parent f7e90fca6e
commit fec96b2ae0
15 changed files with 57 additions and 57 deletions

View file

@ -14,8 +14,8 @@ impl<A> IMPL_T<A>: iter::BaseIter<A> {
impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
pure fn eachi(blk: fn(uint, v: &A) -> bool) { iter::eachi(&self, blk) }
pure fn all(blk: fn(A) -> bool) -> bool { iter::all(self, blk) }
pure fn any(blk: fn(A) -> bool) -> bool { iter::any(self, blk) }
pure fn all(blk: fn(&A) -> bool) -> bool { iter::all(&self, blk) }
pure fn any(blk: fn(&A) -> bool) -> bool { iter::any(&self, blk) }
pure fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B {
iter::foldl(self, move b0, blk)
}
@ -30,18 +30,18 @@ impl<A: Eq> IMPL_T<A>: iter::EqIter<A> {
}
impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {
pure fn filter_to_vec(pred: fn(A) -> bool) -> ~[A] {
iter::filter_to_vec(self, pred)
pure fn filter_to_vec(pred: fn(+a: A) -> bool) -> ~[A] {
iter::filter_to_vec(&self, pred)
}
pure fn map_to_vec<B>(op: fn(v: &A) -> B) -> ~[B] {
iter::map_to_vec(self, op)
pure fn map_to_vec<B>(op: fn(+v: A) -> B) -> ~[B] {
iter::map_to_vec(&self, op)
}
pure fn to_vec() -> ~[A] { iter::to_vec(self) }
// FIXME--bug in resolve prevents this from working (#2611)
// fn flat_map_to_vec<B:copy,IB:base_iter<B>>(op: fn(A) -> IB) -> ~[B] {
// iter::flat_map_to_vec(self, op)
// }
pure fn flat_map_to_vec<B:Copy,IB:BaseIter<B>>(op: fn(+a: A) -> IB)
-> ~[B] {
iter::flat_map_to_vec(&self, op)
}
pure fn find(p: fn(A) -> bool) -> Option<A> { iter::find(self, p) }
}

View file

@ -16,8 +16,8 @@ trait BaseIter<A> {
trait ExtendedIter<A> {
pure fn eachi(blk: fn(uint, v: &A) -> bool);
pure fn all(blk: fn(A) -> bool) -> bool;
pure fn any(blk: fn(A) -> bool) -> bool;
pure fn all(blk: fn(&A) -> bool) -> bool;
pure fn any(blk: fn(&A) -> bool) -> bool;
pure fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B;
pure fn position(f: fn(A) -> bool) -> Option<uint>;
}
@ -35,8 +35,8 @@ trait TimesIx{
}
trait CopyableIter<A:Copy> {
pure fn filter_to_vec(pred: fn(A) -> bool) -> ~[A];
pure fn map_to_vec<B>(op: fn(v: &A) -> B) -> ~[B];
pure fn filter_to_vec(pred: fn(+a: A) -> bool) -> ~[A];
pure fn map_to_vec<B>(op: fn(+v: A) -> B) -> ~[B];
pure fn to_vec() -> ~[A];
pure fn find(p: fn(A) -> bool) -> Option<A>;
}
@ -74,22 +74,22 @@ pure fn eachi<A,IA:BaseIter<A>>(self: &IA, blk: fn(uint, v: &A) -> bool) {
}
}
pure fn all<A,IA:BaseIter<A>>(self: IA, blk: fn(A) -> bool) -> bool {
pure fn all<A,IA:BaseIter<A>>(self: &IA, blk: fn(&A) -> bool) -> bool {
for self.each |a| {
if !blk(*a) { return false; }
if !blk(a) { return false; }
}
return true;
}
pure fn any<A,IA:BaseIter<A>>(self: IA, blk: fn(A) -> bool) -> bool {
pure fn any<A,IA:BaseIter<A>>(self: &IA, blk: fn(&A) -> bool) -> bool {
for self.each |a| {
if blk(*a) { return true; }
if blk(a) { return true; }
}
return false;
}
pure fn filter_to_vec<A:Copy,IA:BaseIter<A>>(self: IA,
prd: fn(A) -> bool) -> ~[A] {
pure fn filter_to_vec<A:Copy,IA:BaseIter<A>>(self: &IA,
prd: fn(+a: A) -> bool) -> ~[A] {
do vec::build_sized_opt(self.size_hint()) |push| {
for self.each |a| {
if prd(*a) { push(*a); }
@ -97,17 +97,17 @@ pure fn filter_to_vec<A:Copy,IA:BaseIter<A>>(self: IA,
}
}
pure fn map_to_vec<A:Copy,B,IA:BaseIter<A>>(self: IA, op: fn(v: &A) -> B)
pure fn map_to_vec<A:Copy,B,IA:BaseIter<A>>(self: &IA, op: fn(+v: A) -> B)
-> ~[B] {
do vec::build_sized_opt(self.size_hint()) |push| {
for self.each |a| {
push(op(a));
push(op(*a));
}
}
}
pure fn flat_map_to_vec<A:Copy,B:Copy,IA:BaseIter<A>,IB:BaseIter<B>>(
self: IA, op: fn(A) -> IB) -> ~[B] {
self: &IA, op: fn(+a: A) -> IB) -> ~[B] {
do vec::build |push| {
for self.each |a| {

View file

@ -1991,8 +1991,8 @@ impl<A> &[A]: iter::BaseIter<A> {
impl<A> &[A]: iter::ExtendedIter<A> {
pure fn eachi(blk: fn(uint, v: &A) -> bool) { iter::eachi(&self, blk) }
pure fn all(blk: fn(A) -> bool) -> bool { iter::all(self, blk) }
pure fn any(blk: fn(A) -> bool) -> bool { iter::any(self, blk) }
pure fn all(blk: fn(&A) -> bool) -> bool { iter::all(&self, blk) }
pure fn any(blk: fn(&A) -> bool) -> bool { iter::any(&self, blk) }
pure fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B {
iter::foldl(self, move b0, blk)
}
@ -2007,11 +2007,11 @@ impl<A: Eq> &[A]: iter::EqIter<A> {
}
impl<A: Copy> &[A]: iter::CopyableIter<A> {
pure fn filter_to_vec(pred: fn(A) -> bool) -> ~[A] {
iter::filter_to_vec(self, pred)
pure fn filter_to_vec(pred: fn(+a: A) -> bool) -> ~[A] {
iter::filter_to_vec(&self, pred)
}
pure fn map_to_vec<B>(op: fn(v: &A) -> B) -> ~[B] {
iter::map_to_vec(self, op)
pure fn map_to_vec<B>(op: fn(+v: A) -> B) -> ~[B] {
iter::map_to_vec(&self, op)
}
pure fn to_vec() -> ~[A] { iter::to_vec(self) }

View file

@ -210,10 +210,10 @@ fn visit<Tproto, Tstate, Tmessage, V: visitor<Tproto, Tstate, Tmessage>>(
// the copy keywords prevent recursive use of dvec
let states = do (copy proto.states).map_to_vec |s| {
let messages = do (copy s.messages).map_to_vec |m| {
let message(name, span, tys, this, next) = *m;
let message(name, span, tys, this, next) = m;
visitor.visit_message(name, span, tys, this, next)
};
visitor.visit_state(*s, messages)
visitor.visit_state(s, messages)
};
visitor.visit_proto(proto, states)
}

View file

@ -63,9 +63,9 @@ fn warn_if_multiple_versions(e: env, diag: span_handler,
partition(crate_cache.map_to_vec(|entry| {
let othername = loader::crate_name_from_metas(*entry.metas);
if name == othername {
Left(*entry)
Left(entry)
} else {
Right(*entry)
Right(entry)
}
}));

View file

@ -432,7 +432,7 @@ fn check_local(tcx: ty::ctxt, loc: @local, &&s: (), v: visit::vt<()>) {
}
}
fn is_refutable(tcx: ty::ctxt, pat: @pat) -> bool {
fn is_refutable(tcx: ty::ctxt, pat: &pat) -> bool {
match tcx.def_map.find(pat.id) {
Some(def_variant(enum_id, _)) => {
if vec::len(*ty::enum_variants(tcx, enum_id)) != 1u {
@ -457,10 +457,10 @@ fn is_refutable(tcx: ty::ctxt, pat: @pat) -> bool {
fields.any(|f| is_refutable(tcx, f.pat))
}
pat_tup(elts) => {
elts.any(|elt| is_refutable(tcx, elt))
elts.any(|elt| is_refutable(tcx, *elt))
}
pat_enum(_, Some(args)) => {
args.any(|a| is_refutable(tcx, a))
args.any(|a| is_refutable(tcx, *a))
}
pat_enum(_,_) => { false }
}

View file

@ -149,7 +149,7 @@ fn get_lint_dict() -> lint_dict {
(~"deprecated_mode",
@{lint: deprecated_mode,
desc: ~"warn about deprecated uses of modes",
default: allow}),
default: warn}),
(~"deprecated_pattern",
@{lint: deprecated_pattern,

View file

@ -514,7 +514,7 @@ fn enter_region(bcx: block, dm: DefMap, m: &[@Match/&r],
fn get_options(ccx: @crate_ctxt, m: &[@Match], col: uint) -> ~[Opt] {
fn add_to_set(tcx: ty::ctxt, set: &DVec<Opt>, val: Opt) {
if set.any(|l| opt_eq(tcx, &l, &val)) {return;}
if set.any(|l| opt_eq(tcx, l, &val)) {return;}
set.push(val);
}

View file

@ -24,7 +24,7 @@ use util::common::indenter;
fn has_trait_bounds(tps: ~[ty::param_bounds]) -> bool {
vec::any(tps, |bs| {
bs.any(|b| {
match b { ty::bound_trait(_) => true, _ => false }
match b { &ty::bound_trait(_) => true, _ => false }
})
})
}

View file

@ -352,7 +352,7 @@ fn validate(edges: ~[(node_id, node_id)],
log(info, ~"Verifying graph edges...");
let status = do edges.all() |e| {
let (u, v) = e;
let (u, v) = *e;
abs(level[u] - level[v]) <= 1
};

View file

@ -4,12 +4,12 @@
use iter::BaseIter;
trait FlatMapToVec<A> {
fn flat_map_to_vec<B:Copy, IB:BaseIter<B>>(op: fn(A) -> IB) -> ~[B];
fn flat_map_to_vec<B:Copy, IB:BaseIter<B>>(op: fn(+a: A) -> IB) -> ~[B];
}
impl<A:Copy> BaseIter<A>: FlatMapToVec<A> {
fn flat_map_to_vec<B:Copy, IB:BaseIter<B>>(op: fn(A) -> IB) -> ~[B] {
iter::flat_map_to_vec(self, op)
fn flat_map_to_vec<B:Copy, IB:BaseIter<B>>(op: fn(+a: A) -> IB) -> ~[B] {
iter::flat_map_to_vec(&self, op)
}
}

View file

@ -1,4 +1,4 @@
fn is_even(&&x: uint) -> bool { (x % 2u) == 0u }
fn is_even(x: &uint) -> bool { (*x % 2) == 0 }
fn main() {
assert ![1u, 2u]/_.all(is_even);

View file

@ -1,11 +1,11 @@
fn is_even(&&x: uint) -> bool { (x % 2u) == 0u }
fn is_even(x: &uint) -> bool { (*x % 2) == 0 }
fn main() {
assert ![1u, 3u]/_.any(is_even);
assert [1u, 2u]/_.any(is_even);
assert ![]/_.any(is_even);
assert !Some(1u).any(is_even);
assert Some(2u).any(is_even);
assert !Some(1).any(is_even);
assert Some(2).any(is_even);
assert !None.any(is_even);
}

View file

@ -1,9 +1,9 @@
fn is_even(&&x: uint) -> bool { (x % 2u) == 0u }
fn is_even(+x: uint) -> bool { (x % 2) == 0 }
fn main() {
assert [1u, 3u]/_.filter_to_vec(is_even) == ~[];
assert [1u, 2u, 3u]/_.filter_to_vec(is_even) == ~[2u];
assert [1, 3]/_.filter_to_vec(is_even) == ~[];
assert [1, 2, 3]/_.filter_to_vec(is_even) == ~[2];
assert None.filter_to_vec(is_even) == ~[];
assert Some(1u).filter_to_vec(is_even) == ~[];
assert Some(2u).filter_to_vec(is_even) == ~[2u];
assert Some(1).filter_to_vec(is_even) == ~[];
assert Some(2).filter_to_vec(is_even) == ~[2];
}

View file

@ -1,9 +1,9 @@
fn inc(x: &uint) -> uint { *x + 1u }
fn inc(+x: uint) -> uint { x + 1 }
fn main() {
assert [1u, 3u]/_.map_to_vec(inc) == ~[2u, 4u];
assert [1u, 2u, 3u]/_.map_to_vec(inc) == ~[2u, 3u, 4u];
assert [1, 3]/_.map_to_vec(inc) == ~[2, 4];
assert [1, 2, 3]/_.map_to_vec(inc) == ~[2, 3, 4];
assert None.map_to_vec(inc) == ~[];
assert Some(1u).map_to_vec(inc) == ~[2u];
assert Some(2u).map_to_vec(inc) == ~[3u];
assert Some(1).map_to_vec(inc) == ~[2];
assert Some(2).map_to_vec(inc) == ~[3];
}