use core::prelude::*;
+use core::iterator::IteratorUtil;
use core::cast;
use core::ptr;
use core::sys;
xs: &[A],
fn_factory: &fn() -> ~fn(uint, &A) -> bool) -> bool
{
- do vec::all(map_slices(xs, || {
+ let mapped = map_slices(xs, || {
let f = fn_factory();
let result: ~fn(uint, &[A]) -> bool = |base, slice| {
- vec::alli(slice, |i, x| {
- f(i + base, x)
- })
+ slice.iter().enumerate().all(|(i, x)| f(i + base, x))
};
result
- })) |x| { *x }
+ });
+ mapped.iter().all(|&x| x)
}
/// Returns true if the function holds for any elements in the vector.
pub fn any<A:Copy + Owned>(
xs: &[A],
fn_factory: &fn() -> ~fn(&A) -> bool) -> bool {
- do vec::any(map_slices(xs, || {
+ let mapped = map_slices(xs, || {
let f = fn_factory();
- let result: ~fn(uint, &[A]) -> bool =
- |_, slice| vec::any(slice, |x| f(x));
+ let result: ~fn(uint, &[A]) -> bool = |_, slice| slice.iter().any(f);
result
- })) |x| { *x }
+ });
+ mapped.iter().any(|&x| x)
}
use middle::ty;
use util::common::indenter;
+use core::iterator::IteratorUtil;
use core::hashmap::HashMap;
use core::vec;
use syntax::ast;
pub fn get_options(bcx: block, m: &[@Match], col: uint) -> ~[Opt] {
let ccx = bcx.ccx();
fn add_to_set(tcx: ty::ctxt, set: &mut ~[Opt], val: Opt) {
- if set.any(|l| opt_eq(tcx, l, &val)) {return;}
+ if set.iter().any(|l| opt_eq(tcx, l, &val)) {return;}
set.push(val);
}
fn extend(idents: &mut ~[ast::ident], field_pats: &[ast::field_pat]) {
for field_pats.each |field_pat| {
let field_ident = field_pat.ident;
- if !vec::any(*idents, |x| *x == field_ident) {
+ if !idents.iter().any(|x| *x == field_ident) {
idents.push(field_ident);
}
}
m: &[@Match],
col: uint)
-> bool {
- vec::any(m, |br| {
+ do m.iter().any |br| {
let pat_id = br.pats[col].id;
let key = root_map_key {id: pat_id, derefs: 0u };
bcx.ccx().maps.root_map.contains_key(&key)
- })
+ }
}
pub fn root_pats_as_necessary(mut bcx: block,
// matches may be wildcards like _ or identifiers).
macro_rules! any_pat (
($m:expr, $pattern:pat) => (
- vec::any($m, |br| {
+ do ($m).iter().any |br| {
match br.pats[col].node {
$pattern => true,
_ => false
}
- })
+ }
)
)
}
pub fn any_tuple_struct_pat(bcx: block, m: &[@Match], col: uint) -> bool {
- vec::any(m, |br| {
+ do m.iter().any |br| {
let pat = br.pats[col];
match pat.node {
ast::pat_enum(_, Some(_)) => {
}
_ => false
}
- })
+ }
}
pub type mk_fail = @fn() -> BasicBlockRef;
use middle::typeck;
use util::ppaux::{Repr,ty_to_str};
+use core::iterator::IteratorUtil;
use core::vec;
use syntax::ast;
use syntax::ast_map;
let param_uses = type_use::type_uses_for(ccx, fn_id, substs.len());
let hash_id = make_mono_id(ccx, fn_id, substs, vtables, impl_did_opt,
Some(param_uses));
- if vec::any(hash_id.params,
+ if hash_id.params.iter().any(
|p| match *p { mono_precise(_, _) => false, _ => true }) {
must_cast = true;
}
use util::common::{indenter};
use util::enum_set::{EnumSet, CLike};
+use core::iterator::IteratorUtil;
use core::cast;
use core::cmp;
use core::hashmap::{HashMap, HashSet};
ty_struct(did, ref substs) => {
seen.push(did);
- let r = vec::any(struct_fields(cx, did, substs),
- |f| type_requires(cx, seen, r_ty, f.mt.ty));
+ let fields = struct_fields(cx, did, substs);
+ let r = fields.iter().any(|f| type_requires(cx, seen, r_ty, f.mt.ty));
seen.pop();
r
}
ty_enum(did, ref substs) => {
seen.push(did);
let vs = enum_variants(cx, did);
- let r = vec::len(*vs) > 0u && vec::all(*vs, |variant| {
- vec::any(variant.args, |aty| {
+ let r = vec::len(*vs) > 0u && do vs.iter().all |variant| {
+ do variant.args.iter().any |aty| {
let sty = subst(cx, substs, *aty);
type_requires(cx, seen, r_ty, sty)
- })
- });
+ }
+ };
seen.pop();
r
}
ty_param(_) => result = false,
ty_opaque_closure_ptr(_) => result = true,
ty_struct(did, ref substs) => {
- result = vec::all(lookup_struct_fields(cx, did), |f| {
+ let fields = lookup_struct_fields(cx, did);
+ result = do fields.iter().all |f| {
let fty = ty::lookup_item_type(cx, f.id);
let sty = subst(cx, substs, fty.ty);
type_is_pod(cx, sty)
- });
+ };
}
ty_estr(vstore_slice(*)) | ty_evec(_, vstore_slice(*)) => {
if variants.len() == 0 {
false
} else {
- variants.all(|v| v.args.len() == 0)
+ variants.iter().all(|v| v.args.len() == 0)
}
}
_ => false
return;
}
let e = ty::lookup_field_type(tcx, did, fields[0].id, substs);
- if !vec::all(fields,
+ if !fields.iter().all(
|f| ty::lookup_field_type(tcx, did, f.id, substs) == e) {
tcx.sess.span_err(sp, "SIMD vector should be homogeneous");
return;
/// assert!(a.iter().all(|&x| *x > 0));
/// assert!(!a.iter().all(|&x| *x > 2));
/// ~~~
- fn all(&mut self, f: &fn(&A) -> bool) -> bool;
+ fn all(&mut self, f: &fn(A) -> bool) -> bool;
/// Tests whether any element of an iterator satisfies the specified
/// predicate.
/// assert!(it.any(|&x| *x == 3));
/// assert!(!it.any(|&x| *x == 3));
/// ~~~
- fn any(&mut self, f: &fn(&A) -> bool) -> bool;
+ fn any(&mut self, f: &fn(A) -> bool) -> bool;
}
/// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also
fn count(&mut self) -> uint { self.fold(0, |cnt, _x| cnt + 1) }
#[inline(always)]
- fn all(&mut self, f: &fn(&A) -> bool) -> bool {
- for self.advance |x| { if !f(&x) { return false; } }
+ fn all(&mut self, f: &fn(A) -> bool) -> bool {
+ for self.advance |x| { if !f(x) { return false; } }
return true;
}
#[inline(always)]
- fn any(&mut self, f: &fn(&A) -> bool) -> bool {
- for self.advance |x| { if f(&x) { return true; } }
+ fn any(&mut self, f: &fn(A) -> bool) -> bool {
+ for self.advance |x| { if f(x) { return true; } }
return false;
}
}
#[test]
fn test_all() {
let v = ~&[1, 2, 3, 4, 5];
- assert!(v.iter().all(|&x| *x < 10));
+ assert!(v.iter().all(|&x| x < 10));
assert!(!v.iter().all(|&x| x.is_even()));
- assert!(!v.iter().all(|&x| *x > 100));
+ assert!(!v.iter().all(|&x| x > 100));
assert!(v.slice(0, 0).iter().all(|_| fail!()));
}
#[test]
fn test_any() {
let v = ~&[1, 2, 3, 4, 5];
- assert!(v.iter().any(|&x| *x < 10));
+ assert!(v.iter().any(|&x| x < 10));
assert!(v.iter().any(|&x| x.is_even()));
- assert!(!v.iter().any(|&x| *x > 100));
+ assert!(!v.iter().any(|&x| x > 100));
assert!(!v.slice(0, 0).iter().any(|_| fail!()));
}
}
#[cfg(test)]
mod tests {
+ use iterator::IteratorUtil;
use container::Container;
use char;
use option::Some;
let mut v = ~[];
for each_split_char(s, c) |s| { v.push(s.to_owned()) }
debug!("split_byte to: %?", v);
- assert!(vec::all2(v, u, |a,b| a == b));
+ assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
}
t("abc.hello.there", '.', [~"abc", ~"hello", ~"there"]);
t(".hello.there", '.', [~"", ~"hello", ~"there"]);
let mut v = ~[];
for each_split_char(s, c) |s| { v.push(s.to_owned()) }
debug!("split_byte to: %?", v);
- assert!(vec::all2(v, u, |a,b| a == b));
+ assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
}
let data = "ประเทศไทย中华Việt Nam";
t(data, 'V', [~"ประเทศไทย中华", ~"iệt Nam"]);
for each_splitn_char(s, c, n) |s| { v.push(s.to_owned()) }
debug!("split_byte to: %?", v);
debug!("comparing vs. %?", u);
- assert!(vec::all2(v, u, |a,b| a == b));
+ assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
}
t("abc.hello.there", '.', 0u, [~"abc.hello.there"]);
t("abc.hello.there", '.', 1u, [~"abc", ~"hello.there"]);
for each_splitn_char(s, c, n) |s| { v.push(s.to_owned()) }
debug!("split_byte to: %?", v);
debug!("comparing vs. %?", u);
- assert!(vec::all2(v, u, |a,b| a == b));
+ assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
}
t("ประเทศไทย中华Việt Nam", '华', 1u, [~"ประเทศไทย中", ~"Việt Nam"]);
for each_splitn_char(s, c, n) |s| { v.push(s.to_owned()) }
debug!("split_byte to: %?", v);
debug!("comparing vs. %?", u);
- assert!(vec::all2(v, u, |a,b| a == b));
+ assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
}
let data = "ประเทศไทย中华Việt Nam";
t(data, 'V', 1u, [~"ประเทศไทย中华", ~"iệt Nam"]);
let mut v = ~[];
for each_split_char_no_trailing(s, c) |s| { v.push(s.to_owned()) }
debug!("split_byte to: %?", v);
- assert!(vec::all2(v, u, |a,b| a == b));
+ assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
}
t("abc.hello.there", '.', [~"abc", ~"hello", ~"there"]);
t(".hello.there", '.', [~"", ~"hello", ~"there"]);
let mut v = ~[];
for each_split_char_no_trailing(s, c) |s| { v.push(s.to_owned()) }
debug!("split_byte to: %?", v);
- assert!(vec::all2(v, u, |a,b| a == b));
+ assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
}
let data = "ประเทศไทย中华Việt Nam";
t(data, 'V', [~"ประเทศไทย中华", ~"iệt Nam"]);
fn t<'a>(s: &str, sep: &'a str, u: &[~str]) {
let mut v = ~[];
for each_split_str(s, sep) |s| { v.push(s.to_owned()) }
- assert!(vec::all2(v, u, |a,b| a == b));
+ assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
}
t("--1233345--", "12345", [~"--1233345--"]);
t("abc::hello::there", "::", [~"abc", ~"hello", ~"there"]);
fn t(s: &str, sepf: &fn(char) -> bool, u: &[~str]) {
let mut v = ~[];
for each_split(s, sepf) |s| { v.push(s.to_owned()) }
- assert!(vec::all2(v, u, |a,b| a == b));
+ assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
}
t("ประเทศไทย中华Việt Nam", |cc| cc == '华', [~"ประเทศไทย中", ~"Việt Nam"]);
fn t(s: &str, sepf: &fn(char) -> bool, u: &[~str]) {
let mut v = ~[];
for each_split_no_trailing(s, sepf) |s| { v.push(s.to_owned()) }
- assert!(vec::all2(v, u, |a,b| a == b));
+ assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
}
t("ประเทศไทย中华Việt Nam", |cc| cc == '华', [~"ประเทศไทย中", ~"Việt Nam"]);
fn t(s: &str, f: &fn(&str, &fn(&str) -> bool) -> bool, u: &[~str]) {
let mut v = ~[];
for f(s) |s| { v.push(s.to_owned()) }
- assert!(vec::all2(v, u, |a,b| a == b));
+ assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
}
t(lf, each_line, [~"", ~"Mary had a little lamb", ~"Little lamb"]);
fn t(s: &str, f: &fn(&str, &fn(&str) -> bool) -> bool, u: &[~str]) {
let mut v = ~[];
for f(s) |s| { v.push(s.to_owned()) }
- assert!(vec::all2(v, u, |a,b| a == b));
+ assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
}
let data = "\nMary had a little lamb\nLittle lamb\n";
fn t(s: &str, i: uint, u: &[~str]) {
let mut v = ~[];
for each_split_within(s, i) |s| { v.push(s.to_owned()) }
- assert!(vec::all2(v, u, |a,b| a == b));
+ assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
}
t("", 0, []);
t("", 15, []);
return z;
}
-/**
- * Return true if a predicate matches any elements
- *
- * If the vector contains no elements then false is returned.
- */
-pub fn any<T>(v: &[T], f: &fn(t: &T) -> bool) -> bool {
- for each(v) |elem| { if f(elem) { return true; } }
- false
-}
-
-/**
- * Return true if a predicate matches any elements in both vectors.
- *
- * If the vectors contains no elements then false is returned.
- */
-pub fn any2<T, U>(v0: &[T], v1: &[U],
- f: &fn(a: &T, b: &U) -> bool) -> bool {
- let v0_len = len(v0);
- let v1_len = len(v1);
- let mut i = 0u;
- while i < v0_len && i < v1_len {
- if f(&v0[i], &v1[i]) { return true; };
- i += 1u;
- }
- false
-}
-
-/**
- * Return true if a predicate matches all elements
- *
- * If the vector contains no elements then true is returned.
- */
-pub fn all<T>(v: &[T], f: &fn(t: &T) -> bool) -> bool {
- for each(v) |elem| { if !f(elem) { return false; } }
- true
-}
-
-/**
- * Return true if a predicate matches all elements
- *
- * If the vector contains no elements then true is returned.
- */
-pub fn alli<T>(v: &[T], f: &fn(uint, t: &T) -> bool) -> bool {
- for eachi(v) |i, elem| { if !f(i, elem) { return false; } }
- true
-}
-
-/**
- * Return true if a predicate matches all elements in both vectors.
- *
- * If the vectors are not the same size then false is returned.
- */
-pub fn all2<T, U>(v0: &[T], v1: &[U],
- f: &fn(t: &T, u: &U) -> bool) -> bool {
- let v0_len = len(v0);
- if v0_len != len(v1) { return false; }
- let mut i = 0u;
- while i < v0_len { if !f(&v0[i], &v1[i]) { return false; }; i += 1u; }
- true
-}
-
/// Return true if a vector contains an element with the given value
pub fn contains<T:Eq>(v: &[T], x: &T) -> bool {
for each(v) |elt| { if *x == *elt { return true; } }
false
}
-/// Returns the number of elements that are equal to a given value
-pub fn count<T:Eq>(v: &[T], x: &T) -> uint {
- let mut cnt = 0u;
- for each(v) |elt| { if *x == *elt { cnt += 1u; } }
- cnt
-}
-
/**
* Search for the first element that matches a given predicate
*
* If the vector is empty, true is returned.
*/
fn alli(&self, f: &fn(uint, t: &T) -> bool) -> bool {
- alli(*self, f)
+ self.iter().enumerate().all(|(i, t)| f(i,t))
}
/**
* Apply a function to each element of a vector and return a concatenation
~[~[5,2,0],~[5,0,2],~[2,5,0],~[2,0,5],~[0,5,2],~[0,2,5]]);
}
- #[test]
- fn test_any_and_all() {
- assert!(any([1u, 2u, 3u], is_three));
- assert!(!any([0u, 1u, 2u], is_three));
- assert!(any([1u, 2u, 3u, 4u, 5u], is_three));
- assert!(!any([1u, 2u, 4u, 5u, 6u], is_three));
-
- assert!(all([3u, 3u, 3u], is_three));
- assert!(!all([3u, 3u, 2u], is_three));
- assert!(all([3u, 3u, 3u, 3u, 3u], is_three));
- assert!(!all([3u, 3u, 0u, 1u, 2u], is_three));
- }
-
- #[test]
- fn test_any2_and_all2() {
-
- assert!(any2([2u, 4u, 6u], [2u, 4u, 6u], is_equal));
- assert!(any2([1u, 2u, 3u], [4u, 5u, 3u], is_equal));
- assert!(!any2([1u, 2u, 3u], [4u, 5u, 6u], is_equal));
- assert!(any2([2u, 4u, 6u], [2u, 4u], is_equal));
-
- assert!(all2([2u, 4u, 6u], [2u, 4u, 6u], is_equal));
- assert!(!all2([1u, 2u, 3u], [4u, 5u, 3u], is_equal));
- assert!(!all2([1u, 2u, 3u], [4u, 5u, 6u], is_equal));
- assert!(!all2([2u, 4u, 6u], [2u, 4u], is_equal));
- }
-
#[test]
fn test_zip_unzip() {
let v1 = ~[1, 2, 3];
};
}
- #[test]
- #[ignore(windows)]
- #[should_fail]
- fn test_any_fail() {
- let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
- let mut i = 0;
- do any(v) |_elt| {
- if i == 2 {
- fail!()
- }
- i += 0;
- false
- };
- }
-
- #[test]
- #[ignore(windows)]
- #[should_fail]
- fn test_any2_fail() {
- let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
- let mut i = 0;
- do any(v) |_elt| {
- if i == 2 {
- fail!()
- }
- i += 0;
- false
- };
- }
-
- #[test]
- #[ignore(windows)]
- #[should_fail]
- fn test_all_fail() {
- let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
- let mut i = 0;
- do all(v) |_elt| {
- if i == 2 {
- fail!()
- }
- i += 0;
- true
- };
- }
-
- #[test]
- #[ignore(windows)]
- #[should_fail]
- fn test_alli_fail() {
- let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
- let mut i = 0;
- do alli(v) |_i, _elt| {
- if i == 2 {
- fail!()
- }
- i += 0;
- true
- };
- }
-
- #[test]
- #[ignore(windows)]
- #[should_fail]
- fn test_all2_fail() {
- let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
- let mut i = 0;
- do all2(v, v) |_elt1, _elt2| {
- if i == 2 {
- fail!()
- }
- i += 0;
- true
- };
- }
-
#[test]
#[ignore(windows)]
#[should_fail]
#[test]
fn test_mut_rev_iterator() {
use iterator::*;
- let mut xs = [1, 2, 3, 4, 5];
+ let mut xs = [1u, 2, 3, 4, 5];
for xs.mut_rev_iter().enumerate().advance |(i,x)| {
*x += i;
}
use extra::time;
use extra::deque::Deque;
use extra::par;
+use std::iterator::IteratorUtil;
use std::hashmap::HashSet;
use std::int::abs;
use std::io;
while keys.len() < n {
let k = r.gen_uint_range(0u, graph.len());
- if graph[k].len() > 0u && vec::any(graph[k], |i| {
+ if graph[k].len() > 0u && graph[k].iter().any(|i| {
*i != k as node_id
}) {
keys.insert(k as node_id);
}
let mut i = 0;
- while vec::any(colors, is_gray) {
+ while colors.iter().any(is_gray) {
// Do the BFS.
info!("PBFS iteration %?", i);
i += 1;
// option. This file may not be copied, modified, or distributed
// except according to those terms.
+use std::iterator::IteratorUtil;
use std::vec;
// Check usage and precedence of block arguments in expressions:
}
// Usable at all:
- let mut any_negative = do vec::any(v) |e| { e.is_negative() };
+ let mut any_negative = do v.iter().any |e| { e.is_negative() };
assert!(any_negative);
// Higher precedence than assignments:
- any_negative = do vec::any(v) |e| { e.is_negative() };
+ any_negative = do v.iter().any |e| { e.is_negative() };
assert!(any_negative);
// Higher precedence than unary operations:
let abs_v = do vec::map(v) |e| { e.abs() };
- assert!(do vec::all(abs_v) |e| { e.is_positive() });
- assert!(!do vec::any(abs_v) |e| { e.is_negative() });
+ assert!(do abs_v.iter().all |e| { e.is_positive() });
+ assert!(!do abs_v.iter().any |e| { e.is_negative() });
// Usable in funny statement-like forms:
- if !do vec::any(v) |e| { e.is_positive() } {
+ if !do v.iter().any |e| { e.is_positive() } {
assert!(false);
}
- match do vec::all(v) |e| { e.is_negative() } {
+ match do v.iter().all |e| { e.is_negative() } {
true => { fail!("incorrect answer."); }
false => { }
}
match 3 {
- _ if do vec::any(v) |e| { e.is_negative() } => {
+ _ if do v.iter().any |e| { e.is_negative() } => {
}
_ => {
fail!("wrong answer.");
// In the tail of a block
let w =
- if true { do vec::any(abs_v) |e| { e.is_positive() } }
+ if true { do abs_v.iter().any |e| { e.is_positive() } }
else { false };
assert!(w);
}
// xfail-fast
+use std::iterator::IteratorUtil;
use std::cmp::Eq;
use std::vec;
pub fn main() {
let mut last = 0;
- for vec::all(~[1, 2, 3, 4, 5, 6, 7]) |e| {
+ let v = ~[1, 2, 3, 4, 5, 6, 7];
+ for v.iter().all |e| {
last = *e;
if *e == 5 { break; }
if *e % 2 == 1 { loop; }
extern mod extra;
-use std::vec;
+use std::iterator::IteratorUtil;
#[test]
#[ignore(cfg(ignorecfg))]
// Pull the tests out of the secreturn test module
let tests = __test::tests;
- assert!(vec::any(
- tests,
- |t| t.desc.name.to_str() == ~"shouldignore" && t.desc.ignore));
+ assert!(
+ tests.iter().any(|t| t.desc.name.to_str() == ~"shouldignore" && t.desc.ignore));
- assert!(vec::any(
- tests,
- |t| t.desc.name.to_str() == ~"shouldnotignore" && !t.desc.ignore));
+ assert!(
+ tests.iter().any(|t| t.desc.name.to_str() == ~"shouldnotignore" && !t.desc.ignore));
}