Skip to content

Commit

Permalink
Demoding in iter: any, all, map_to_vec, flat_map_to_vec, filter_to_vec
Browse files Browse the repository at this point in the history
  • Loading branch information
catamorphism committed Sep 29, 2012
1 parent f7e90fc commit fec96b2
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 57 deletions.
20 changes: 10 additions & 10 deletions src/libcore/iter-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) }
}
Expand Down
26 changes: 13 additions & 13 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}
Expand All @@ -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>;
}
Expand Down Expand Up @@ -74,40 +74,40 @@ 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); }
}
}
}

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| {
Expand Down
12 changes: 6 additions & 6 deletions src/libcore/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) }

Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/pipes/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions src/rustc/metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}));

Expand Down
6 changes: 3 additions & 3 deletions src/rustc/middle/check_alt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 }
}
Expand Down
2 changes: 1 addition & 1 deletion src/rustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/rustc/middle/trans/alt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/rustc/middle/typeck/check/vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
})
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/bench/graph500-bfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
6 changes: 3 additions & 3 deletions src/test/run-pass/issue-2611.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/iter-all.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/test/run-pass/iter-any.rs
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 5 additions & 5 deletions src/test/run-pass/iter-filter-to-vec.rs
Original file line number Diff line number Diff line change
@@ -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];
}
10 changes: 5 additions & 5 deletions src/test/run-pass/iter-map-to-vec.rs
Original file line number Diff line number Diff line change
@@ -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];
}

0 comments on commit fec96b2

Please sign in to comment.