Skip to content

Commit 65c7c58

Browse files
committed
std: remove {all*,any*,count} in favour of iterators
1 parent ce4f63d commit 65c7c58

File tree

12 files changed

+78
-241
lines changed

12 files changed

+78
-241
lines changed

src/libextra/par.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use core::prelude::*;
1212

13+
use core::iterator::IteratorUtil;
1314
use core::cast;
1415
use core::ptr;
1516
use core::sys;
@@ -122,25 +123,24 @@ pub fn alli<A:Copy + Owned>(
122123
xs: &[A],
123124
fn_factory: &fn() -> ~fn(uint, &A) -> bool) -> bool
124125
{
125-
do vec::all(map_slices(xs, || {
126+
let mapped = map_slices(xs, || {
126127
let f = fn_factory();
127128
let result: ~fn(uint, &[A]) -> bool = |base, slice| {
128-
vec::alli(slice, |i, x| {
129-
f(i + base, x)
130-
})
129+
slice.iter().enumerate().all(|(i, x)| f(i + base, x))
131130
};
132131
result
133-
})) |x| { *x }
132+
});
133+
mapped.iter().all(|&x| x)
134134
}
135135

136136
/// Returns true if the function holds for any elements in the vector.
137137
pub fn any<A:Copy + Owned>(
138138
xs: &[A],
139139
fn_factory: &fn() -> ~fn(&A) -> bool) -> bool {
140-
do vec::any(map_slices(xs, || {
140+
let mapped = map_slices(xs, || {
141141
let f = fn_factory();
142-
let result: ~fn(uint, &[A]) -> bool =
143-
|_, slice| vec::any(slice, |x| f(x));
142+
let result: ~fn(uint, &[A]) -> bool = |_, slice| slice.iter().any(f);
144143
result
145-
})) |x| { *x }
144+
});
145+
mapped.iter().any(|&x| x)
146146
}

src/librustc/middle/trans/_match.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ use middle::trans::type_of;
172172
use middle::ty;
173173
use util::common::indenter;
174174

175+
use core::iterator::IteratorUtil;
175176
use core::hashmap::HashMap;
176177
use core::vec;
177178
use syntax::ast;
@@ -798,7 +799,7 @@ pub fn enter_region<'r>(bcx: block,
798799
pub fn get_options(bcx: block, m: &[@Match], col: uint) -> ~[Opt] {
799800
let ccx = bcx.ccx();
800801
fn add_to_set(tcx: ty::ctxt, set: &mut ~[Opt], val: Opt) {
801-
if set.any(|l| opt_eq(tcx, l, &val)) {return;}
802+
if set.iter().any(|l| opt_eq(tcx, l, &val)) {return;}
802803
set.push(val);
803804
}
804805

@@ -965,7 +966,7 @@ pub fn collect_record_or_struct_fields(bcx: block,
965966
fn extend(idents: &mut ~[ast::ident], field_pats: &[ast::field_pat]) {
966967
for field_pats.each |field_pat| {
967968
let field_ident = field_pat.ident;
968-
if !vec::any(*idents, |x| *x == field_ident) {
969+
if !idents.iter().any(|x| *x == field_ident) {
969970
idents.push(field_ident);
970971
}
971972
}
@@ -976,11 +977,11 @@ pub fn pats_require_rooting(bcx: block,
976977
m: &[@Match],
977978
col: uint)
978979
-> bool {
979-
vec::any(m, |br| {
980+
do m.iter().any |br| {
980981
let pat_id = br.pats[col].id;
981982
let key = root_map_key {id: pat_id, derefs: 0u };
982983
bcx.ccx().maps.root_map.contains_key(&key)
983-
})
984+
}
984985
}
985986

986987
pub fn root_pats_as_necessary(mut bcx: block,
@@ -1005,12 +1006,12 @@ pub fn root_pats_as_necessary(mut bcx: block,
10051006
// matches may be wildcards like _ or identifiers).
10061007
macro_rules! any_pat (
10071008
($m:expr, $pattern:pat) => (
1008-
vec::any($m, |br| {
1009+
do ($m).iter().any |br| {
10091010
match br.pats[col].node {
10101011
$pattern => true,
10111012
_ => false
10121013
}
1013-
})
1014+
}
10141015
)
10151016
)
10161017

@@ -1031,7 +1032,7 @@ pub fn any_tup_pat(m: &[@Match], col: uint) -> bool {
10311032
}
10321033

10331034
pub fn any_tuple_struct_pat(bcx: block, m: &[@Match], col: uint) -> bool {
1034-
vec::any(m, |br| {
1035+
do m.iter().any |br| {
10351036
let pat = br.pats[col];
10361037
match pat.node {
10371038
ast::pat_enum(_, Some(_)) => {
@@ -1043,7 +1044,7 @@ pub fn any_tuple_struct_pat(bcx: block, m: &[@Match], col: uint) -> bool {
10431044
}
10441045
_ => false
10451046
}
1046-
})
1047+
}
10471048
}
10481049

10491050
pub type mk_fail = @fn() -> BasicBlockRef;

src/librustc/middle/trans/monomorphize.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use middle::ty::{FnSig};
3232
use middle::typeck;
3333
use util::ppaux::{Repr,ty_to_str};
3434

35+
use core::iterator::IteratorUtil;
3536
use core::vec;
3637
use syntax::ast;
3738
use syntax::ast_map;
@@ -75,7 +76,7 @@ pub fn monomorphic_fn(ccx: @CrateContext,
7576
let param_uses = type_use::type_uses_for(ccx, fn_id, substs.len());
7677
let hash_id = make_mono_id(ccx, fn_id, substs, vtables, impl_did_opt,
7778
Some(param_uses));
78-
if vec::any(hash_id.params,
79+
if hash_id.params.iter().any(
7980
|p| match *p { mono_precise(_, _) => false, _ => true }) {
8081
must_cast = true;
8182
}

src/librustc/middle/ty.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use util::ppaux::{Repr, UserString};
2727
use util::common::{indenter};
2828
use util::enum_set::{EnumSet, CLike};
2929

30+
use core::iterator::IteratorUtil;
3031
use core::cast;
3132
use core::cmp;
3233
use core::hashmap::{HashMap, HashSet};
@@ -2355,8 +2356,8 @@ pub fn is_instantiable(cx: ctxt, r_ty: t) -> bool {
23552356

23562357
ty_struct(did, ref substs) => {
23572358
seen.push(did);
2358-
let r = vec::any(struct_fields(cx, did, substs),
2359-
|f| type_requires(cx, seen, r_ty, f.mt.ty));
2359+
let fields = struct_fields(cx, did, substs);
2360+
let r = fields.iter().any(|f| type_requires(cx, seen, r_ty, f.mt.ty));
23602361
seen.pop();
23612362
r
23622363
}
@@ -2372,12 +2373,12 @@ pub fn is_instantiable(cx: ctxt, r_ty: t) -> bool {
23722373
ty_enum(did, ref substs) => {
23732374
seen.push(did);
23742375
let vs = enum_variants(cx, did);
2375-
let r = vec::len(*vs) > 0u && vec::all(*vs, |variant| {
2376-
vec::any(variant.args, |aty| {
2376+
let r = vec::len(*vs) > 0u && do vs.iter().all |variant| {
2377+
do variant.args.iter().any |aty| {
23772378
let sty = subst(cx, substs, *aty);
23782379
type_requires(cx, seen, r_ty, sty)
2379-
})
2380-
});
2380+
}
2381+
};
23812382
seen.pop();
23822383
r
23832384
}
@@ -2519,11 +2520,12 @@ pub fn type_is_pod(cx: ctxt, ty: t) -> bool {
25192520
ty_param(_) => result = false,
25202521
ty_opaque_closure_ptr(_) => result = true,
25212522
ty_struct(did, ref substs) => {
2522-
result = vec::all(lookup_struct_fields(cx, did), |f| {
2523+
let fields = lookup_struct_fields(cx, did);
2524+
result = do fields.iter().all |f| {
25232525
let fty = ty::lookup_item_type(cx, f.id);
25242526
let sty = subst(cx, substs, fty.ty);
25252527
type_is_pod(cx, sty)
2526-
});
2528+
};
25272529
}
25282530

25292531
ty_estr(vstore_slice(*)) | ty_evec(_, vstore_slice(*)) => {
@@ -2569,7 +2571,7 @@ pub fn type_is_c_like_enum(cx: ctxt, ty: t) -> bool {
25692571
if variants.len() == 0 {
25702572
false
25712573
} else {
2572-
variants.all(|v| v.args.len() == 0)
2574+
variants.iter().all(|v| v.args.len() == 0)
25732575
}
25742576
}
25752577
_ => false

src/librustc/middle/typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3065,7 +3065,7 @@ pub fn check_simd(tcx: ty::ctxt, sp: span, id: ast::node_id) {
30653065
return;
30663066
}
30673067
let e = ty::lookup_field_type(tcx, did, fields[0].id, substs);
3068-
if !vec::all(fields,
3068+
if !fields.iter().all(
30693069
|f| ty::lookup_field_type(tcx, did, f.id, substs) == e) {
30703070
tcx.sess.span_err(sp, "SIMD vector should be homogeneous");
30713071
return;

src/libstd/iterator.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ pub trait IteratorUtil<A> {
326326
/// assert!(a.iter().all(|&x| *x > 0));
327327
/// assert!(!a.iter().all(|&x| *x > 2));
328328
/// ~~~
329-
fn all(&mut self, f: &fn(&A) -> bool) -> bool;
329+
fn all(&mut self, f: &fn(A) -> bool) -> bool;
330330

331331
/// Tests whether any element of an iterator satisfies the specified
332332
/// predicate.
@@ -341,7 +341,7 @@ pub trait IteratorUtil<A> {
341341
/// assert!(it.any(|&x| *x == 3));
342342
/// assert!(!it.any(|&x| *x == 3));
343343
/// ~~~
344-
fn any(&mut self, f: &fn(&A) -> bool) -> bool;
344+
fn any(&mut self, f: &fn(A) -> bool) -> bool;
345345
}
346346

347347
/// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also
@@ -462,14 +462,14 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
462462
fn count(&mut self) -> uint { self.fold(0, |cnt, _x| cnt + 1) }
463463

464464
#[inline(always)]
465-
fn all(&mut self, f: &fn(&A) -> bool) -> bool {
466-
for self.advance |x| { if !f(&x) { return false; } }
465+
fn all(&mut self, f: &fn(A) -> bool) -> bool {
466+
for self.advance |x| { if !f(x) { return false; } }
467467
return true;
468468
}
469469

470470
#[inline(always)]
471-
fn any(&mut self, f: &fn(&A) -> bool) -> bool {
472-
for self.advance |x| { if f(&x) { return true; } }
471+
fn any(&mut self, f: &fn(A) -> bool) -> bool {
472+
for self.advance |x| { if f(x) { return true; } }
473473
return false;
474474
}
475475
}
@@ -1080,18 +1080,18 @@ mod tests {
10801080
#[test]
10811081
fn test_all() {
10821082
let v = ~&[1, 2, 3, 4, 5];
1083-
assert!(v.iter().all(|&x| *x < 10));
1083+
assert!(v.iter().all(|&x| x < 10));
10841084
assert!(!v.iter().all(|&x| x.is_even()));
1085-
assert!(!v.iter().all(|&x| *x > 100));
1085+
assert!(!v.iter().all(|&x| x > 100));
10861086
assert!(v.slice(0, 0).iter().all(|_| fail!()));
10871087
}
10881088

10891089
#[test]
10901090
fn test_any() {
10911091
let v = ~&[1, 2, 3, 4, 5];
1092-
assert!(v.iter().any(|&x| *x < 10));
1092+
assert!(v.iter().any(|&x| x < 10));
10931093
assert!(v.iter().any(|&x| x.is_even()));
1094-
assert!(!v.iter().any(|&x| *x > 100));
1094+
assert!(!v.iter().any(|&x| x > 100));
10951095
assert!(!v.slice(0, 0).iter().any(|_| fail!()));
10961096
}
10971097
}

src/libstd/str.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,6 +2883,7 @@ impl<'self> Iterator<char> for StrCharIterator<'self> {
28832883
28842884
#[cfg(test)]
28852885
mod tests {
2886+
use iterator::IteratorUtil;
28862887
use container::Container;
28872888
use char;
28882889
use option::Some;
@@ -2977,7 +2978,7 @@ mod tests {
29772978
let mut v = ~[];
29782979
for each_split_char(s, c) |s| { v.push(s.to_owned()) }
29792980
debug!("split_byte to: %?", v);
2980-
assert!(vec::all2(v, u, |a,b| a == b));
2981+
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
29812982
}
29822983
t("abc.hello.there", '.', [~"abc", ~"hello", ~"there"]);
29832984
t(".hello.there", '.', [~"", ~"hello", ~"there"]);
@@ -2995,7 +2996,7 @@ mod tests {
29952996
let mut v = ~[];
29962997
for each_split_char(s, c) |s| { v.push(s.to_owned()) }
29972998
debug!("split_byte to: %?", v);
2998-
assert!(vec::all2(v, u, |a,b| a == b));
2999+
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
29993000
}
30003001
let data = "ประเทศไทย中华Việt Nam";
30013002
t(data, 'V', [~"ประเทศไทย中华", ~"iệt Nam"]);
@@ -3010,7 +3011,7 @@ mod tests {
30103011
for each_splitn_char(s, c, n) |s| { v.push(s.to_owned()) }
30113012
debug!("split_byte to: %?", v);
30123013
debug!("comparing vs. %?", u);
3013-
assert!(vec::all2(v, u, |a,b| a == b));
3014+
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
30143015
}
30153016
t("abc.hello.there", '.', 0u, [~"abc.hello.there"]);
30163017
t("abc.hello.there", '.', 1u, [~"abc", ~"hello.there"]);
@@ -3037,7 +3038,7 @@ mod tests {
30373038
for each_splitn_char(s, c, n) |s| { v.push(s.to_owned()) }
30383039
debug!("split_byte to: %?", v);
30393040
debug!("comparing vs. %?", u);
3040-
assert!(vec::all2(v, u, |a,b| a == b));
3041+
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
30413042
}
30423043
30433044
t("ประเทศไทย中华Việt Nam", '华', 1u, [~"ประเทศไทย中", ~"Việt Nam"]);
@@ -3055,7 +3056,7 @@ mod tests {
30553056
for each_splitn_char(s, c, n) |s| { v.push(s.to_owned()) }
30563057
debug!("split_byte to: %?", v);
30573058
debug!("comparing vs. %?", u);
3058-
assert!(vec::all2(v, u, |a,b| a == b));
3059+
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
30593060
}
30603061
let data = "ประเทศไทย中华Việt Nam";
30613062
t(data, 'V', 1u, [~"ประเทศไทย中华", ~"iệt Nam"]);
@@ -3069,7 +3070,7 @@ mod tests {
30693070
let mut v = ~[];
30703071
for each_split_char_no_trailing(s, c) |s| { v.push(s.to_owned()) }
30713072
debug!("split_byte to: %?", v);
3072-
assert!(vec::all2(v, u, |a,b| a == b));
3073+
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
30733074
}
30743075
t("abc.hello.there", '.', [~"abc", ~"hello", ~"there"]);
30753076
t(".hello.there", '.', [~"", ~"hello", ~"there"]);
@@ -3088,7 +3089,7 @@ mod tests {
30883089
let mut v = ~[];
30893090
for each_split_char_no_trailing(s, c) |s| { v.push(s.to_owned()) }
30903091
debug!("split_byte to: %?", v);
3091-
assert!(vec::all2(v, u, |a,b| a == b));
3092+
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
30923093
}
30933094
let data = "ประเทศไทย中华Việt Nam";
30943095
t(data, 'V', [~"ประเทศไทย中华", ~"iệt Nam"]);
@@ -3100,7 +3101,7 @@ mod tests {
31003101
fn t<'a>(s: &str, sep: &'a str, u: &[~str]) {
31013102
let mut v = ~[];
31023103
for each_split_str(s, sep) |s| { v.push(s.to_owned()) }
3103-
assert!(vec::all2(v, u, |a,b| a == b));
3104+
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
31043105
}
31053106
t("--1233345--", "12345", [~"--1233345--"]);
31063107
t("abc::hello::there", "::", [~"abc", ~"hello", ~"there"]);
@@ -3124,7 +3125,7 @@ mod tests {
31243125
fn t(s: &str, sepf: &fn(char) -> bool, u: &[~str]) {
31253126
let mut v = ~[];
31263127
for each_split(s, sepf) |s| { v.push(s.to_owned()) }
3127-
assert!(vec::all2(v, u, |a,b| a == b));
3128+
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
31283129
}
31293130
31303131
t("ประเทศไทย中华Việt Nam", |cc| cc == '华', [~"ประเทศไทย中", ~"Việt Nam"]);
@@ -3140,7 +3141,7 @@ mod tests {
31403141
fn t(s: &str, sepf: &fn(char) -> bool, u: &[~str]) {
31413142
let mut v = ~[];
31423143
for each_split_no_trailing(s, sepf) |s| { v.push(s.to_owned()) }
3143-
assert!(vec::all2(v, u, |a,b| a == b));
3144+
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
31443145
}
31453146
31463147
t("ประเทศไทย中华Việt Nam", |cc| cc == '华', [~"ประเทศไทย中", ~"Việt Nam"]);
@@ -3159,7 +3160,7 @@ mod tests {
31593160
fn t(s: &str, f: &fn(&str, &fn(&str) -> bool) -> bool, u: &[~str]) {
31603161
let mut v = ~[];
31613162
for f(s) |s| { v.push(s.to_owned()) }
3162-
assert!(vec::all2(v, u, |a,b| a == b));
3163+
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
31633164
}
31643165

31653166
t(lf, each_line, [~"", ~"Mary had a little lamb", ~"Little lamb"]);
@@ -3179,7 +3180,7 @@ mod tests {
31793180
fn t(s: &str, f: &fn(&str, &fn(&str) -> bool) -> bool, u: &[~str]) {
31803181
let mut v = ~[];
31813182
for f(s) |s| { v.push(s.to_owned()) }
3182-
assert!(vec::all2(v, u, |a,b| a == b));
3183+
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
31833184
}
31843185
let data = "\nMary had a little lamb\nLittle lamb\n";
31853186

@@ -3193,7 +3194,7 @@ mod tests {
31933194
fn t(s: &str, i: uint, u: &[~str]) {
31943195
let mut v = ~[];
31953196
for each_split_within(s, i) |s| { v.push(s.to_owned()) }
3196-
assert!(vec::all2(v, u, |a,b| a == b));
3197+
assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
31973198
}
31983199
t("", 0, []);
31993200
t("", 15, []);

0 commit comments

Comments
 (0)