diff --git a/src/libextra/test.rs b/src/libextra/test.rs index 313577ac67d..97793ce440c 100644 --- a/src/libextra/test.rs +++ b/src/libextra/test.rs @@ -523,7 +523,7 @@ pub fn filter_tests( } else { return option::None; } } - vec::filter_map(filtered, |x| filter_fn(x, filter_str)) + filtered.consume_iter().filter_map(|x| filter_fn(x, filter_str)).collect() }; // Maybe pull out the ignored test and unignore them @@ -541,7 +541,7 @@ pub fn filter_tests( None } }; - vec::filter_map(filtered, |x| filter(x)) + filtered.consume_iter().filter_map(|x| filter(x)).collect() }; // Sort the tests alphabetically @@ -720,9 +720,9 @@ impl BenchHarness { // Eliminate outliers let med = samples.median(); let mad = samples.median_abs_dev(); - let samples = do vec::filter(samples) |f| { + let samples = do samples.consume_iter().filter |f| { num::abs(*f - med) <= 3.0 * mad - }; + }.collect::<~[f64]>(); debug!("%u samples, median %f, MAD=%f, %u survived filter", n_samples, med as float, mad as float, diff --git a/src/librustc/front/config.rs b/src/librustc/front/config.rs index b1d4820f062..7d478afee41 100644 --- a/src/librustc/front/config.rs +++ b/src/librustc/front/config.rs @@ -10,7 +10,6 @@ use std::option; -use std::vec; use syntax::{ast, fold, attr}; type in_cfg_pred = @fn(attrs: ~[ast::attribute]) -> bool; @@ -61,13 +60,15 @@ fn filter_view_item(cx: @Context, view_item: @ast::view_item } fn fold_mod(cx: @Context, m: &ast::_mod, fld: @fold::ast_fold) -> ast::_mod { - let filtered_items = - m.items.filter_mapped(|a| filter_item(cx, *a)); - let filtered_view_items = - m.view_items.filter_mapped(|a| filter_view_item(cx, *a)); + let filtered_items = do m.items.iter().filter_map |a| { + filter_item(cx, *a).chain(|x| fld.fold_item(x)) + }.collect(); + let filtered_view_items = do m.view_items.iter().filter_map |a| { + filter_view_item(cx, *a).map(|x| fld.fold_view_item(*x)) + }.collect(); ast::_mod { - view_items: filtered_view_items.map(|x| fld.fold_view_item(*x)), - items: vec::filter_map(filtered_items, |x| fld.fold_item(x)) + view_items: filtered_view_items, + items: filtered_items } } @@ -83,14 +84,14 @@ fn fold_foreign_mod( nm: &ast::foreign_mod, fld: @fold::ast_fold ) -> ast::foreign_mod { - let filtered_items = - nm.items.filter_mapped(|a| filter_foreign_item(cx, *a)); - let filtered_view_items = - nm.view_items.filter_mapped(|a| filter_view_item(cx, *a)); + let filtered_items = nm.items.iter().filter_map(|a| filter_foreign_item(cx, *a)).collect(); + let filtered_view_items = do nm.view_items.iter().filter_map |a| { + filter_view_item(cx, *a).map(|x| fld.fold_view_item(*x)) + }.collect(); ast::foreign_mod { sort: nm.sort, abis: nm.abis, - view_items: filtered_view_items.iter().transform(|x| fld.fold_view_item(*x)).collect(), + view_items: filtered_view_items, items: filtered_items } } @@ -99,11 +100,13 @@ fn fold_item_underscore(cx: @Context, item: &ast::item_, fld: @fold::ast_fold) -> ast::item_ { let item = match *item { ast::item_impl(ref a, b, c, ref methods) => { - let methods = methods.filtered(|m| method_in_cfg(cx, *m) ); + let methods = methods.iter().filter(|m| method_in_cfg(cx, **m)) + .transform(|x| *x).collect(); ast::item_impl(/*bad*/ copy *a, b, c, methods) } ast::item_trait(ref a, ref b, ref methods) => { - let methods = methods.filtered(|m| trait_method_in_cfg(cx, m) ); + let methods = methods.iter().filter(|m| trait_method_in_cfg(cx, *m) ) + .transform(|x| /* bad */copy *x).collect(); ast::item_trait(/*bad*/copy *a, /*bad*/copy *b, methods) } ref item => /*bad*/ copy *item @@ -134,19 +137,12 @@ fn fold_block( b: &ast::blk_, fld: @fold::ast_fold ) -> ast::blk_ { - let filtered_stmts = - b.stmts.filter_mapped(|a| filter_stmt(cx, *a)); - let filtered_view_items = - b.view_items.filter_mapped(|a| filter_view_item(cx, *a)); - let filtered_view_items = - filtered_view_items.map(|x| fld.fold_view_item(*x)); - let mut resulting_stmts = ~[]; - for filtered_stmts.iter().advance |stmt| { - match fld.fold_stmt(*stmt) { - None => {} - Some(stmt) => resulting_stmts.push(stmt), - } - } + let resulting_stmts = do b.stmts.iter().filter_map |a| { + filter_stmt(cx, *a).chain(|stmt| fld.fold_stmt(stmt)) + }.collect(); + let filtered_view_items = do b.view_items.iter().filter_map |a| { + filter_view_item(cx, *a).map(|x| fld.fold_view_item(*x)) + }.collect(); ast::blk_ { view_items: filtered_view_items, stmts: resulting_stmts, @@ -193,7 +189,9 @@ pub fn metas_in_cfg(cfg: &[@ast::meta_item], // Pull the inner meta_items from the #[cfg(meta_item, ...)] attributes, // so we can match against them. This is the list of configurations for // which the item is valid - let cfg_metas = vec::filter_map(cfg_metas, |i| attr::get_meta_item_list(i)); + let cfg_metas = cfg_metas.consume_iter() + .filter_map(|i| attr::get_meta_item_list(i)) + .collect::<~[~[@ast::meta_item]]>(); if cfg_metas.iter().all(|c| c.is_empty()) { return true; } diff --git a/src/librustc/front/test.rs b/src/librustc/front/test.rs index 41c70c4c5b4..91000d68aad 100644 --- a/src/librustc/front/test.rs +++ b/src/librustc/front/test.rs @@ -109,9 +109,11 @@ fn fold_mod(cx: @mut TestCtxt, fn nomain(cx: @mut TestCtxt, item: @ast::item) -> @ast::item { if !*cx.sess.building_library { - @ast::item{attrs: item.attrs.filtered(|attr| { - "main" != attr::get_attr_name(attr) - }),.. copy *item} + @ast::item{ + attrs: do item.attrs.iter().filter_map |attr| { + if "main" != attr::get_attr_name(attr) {Some(*attr)} else {None} + }.collect(), + .. copy *item} } else { item } } @@ -229,10 +231,10 @@ fn is_ignored(cx: @mut TestCtxt, i: @ast::item) -> bool { let ignoreattrs = attr::find_attrs_by_name(i.attrs, "ignore"); let ignoreitems = attr::attr_metas(ignoreattrs); return if !ignoreitems.is_empty() { - let cfg_metas = - vec::concat( - vec::filter_map(ignoreitems, - |i| attr::get_meta_item_list(i))); + let cfg_metas = ignoreitems.consume_iter() + .filter_map(|i| attr::get_meta_item_list(i)) + .collect::<~[~[@ast::meta_item]]>() + .concat_vec(); config::metas_in_cfg(/*bad*/copy cx.crate.node.config, cfg_metas) } else { false diff --git a/src/librustc/middle/astencode.rs b/src/librustc/middle/astencode.rs index c6d7314f1cd..72b6f8e1c80 100644 --- a/src/librustc/middle/astencode.rs +++ b/src/librustc/middle/astencode.rs @@ -291,16 +291,16 @@ fn encode_ast(ebml_w: &mut writer::Encoder, item: ast::inlined_item) { // inlined items. fn simplify_ast(ii: &ast::inlined_item) -> ast::inlined_item { fn drop_nested_items(blk: &ast::blk_, fld: @fold::ast_fold) -> ast::blk_ { - let stmts_sans_items = do blk.stmts.filtered |stmt| { + let stmts_sans_items = do blk.stmts.iter().filter_map |stmt| { match stmt.node { ast::stmt_expr(_, _) | ast::stmt_semi(_, _) | - ast::stmt_decl(@codemap::spanned { node: ast::decl_local(_), - span: _}, _) => true, - ast::stmt_decl(@codemap::spanned { node: ast::decl_item(_), - span: _}, _) => false, + ast::stmt_decl(@codemap::spanned { node: ast::decl_local(_), span: _}, _) + => Some(*stmt), + ast::stmt_decl(@codemap::spanned { node: ast::decl_item(_), span: _}, _) + => None, ast::stmt_mac(*) => fail!("unexpanded macro in astencode") } - }; + }.collect(); let blk_sans_items = ast::blk_ { view_items: ~[], // I don't know if we need the view_items here, // but it doesn't break tests! diff --git a/src/librustc/middle/check_match.rs b/src/librustc/middle/check_match.rs index 72896258b2d..740499bbf25 100644 --- a/src/librustc/middle/check_match.rs +++ b/src/librustc/middle/check_match.rs @@ -95,7 +95,7 @@ pub fn check_expr(cx: @MatchCheckCtxt, ex: @expr, (s, v): ((), visit::vt<()>)) { } _ => { /* We assume only enum types can be uninhabited */ } } - let arms = vec::concat(arms.filter_mapped(unguarded_pat)); + let arms = arms.iter().filter_map(unguarded_pat).collect::<~[~[@pat]]>().concat_vec(); if arms.is_empty() { cx.tcx.sess.span_err(ex.span, "non-exhaustive patterns"); } else { @@ -265,7 +265,7 @@ pub fn is_useful(cx: &MatchCheckCtxt, m: &matrix, v: &[@pat]) -> useful { } Some(ref ctor) => { match is_useful(cx, - &m.filter_mapped(|r| default(cx, *r)), + &m.iter().filter_map(|r| default(cx, *r)).collect::(), v.tail()) { useful_ => useful(left_ty, /*bad*/copy *ctor), ref u => (/*bad*/copy *u) @@ -287,7 +287,7 @@ pub fn is_useful_specialized(cx: &MatchCheckCtxt, arity: uint, lty: ty::t) -> useful { - let ms = m.filter_mapped(|r| specialize(cx, *r, &ctor, arity, lty)); + let ms = m.iter().filter_map(|r| specialize(cx, *r, &ctor, arity, lty)).collect::(); let could_be_useful = is_useful( cx, &ms, specialize(cx, v, &ctor, arity, lty).get()); match could_be_useful { @@ -397,14 +397,14 @@ pub fn missing_ctor(cx: &MatchCheckCtxt, ty::ty_unboxed_vec(*) | ty::ty_evec(*) => { // Find the lengths and slices of all vector patterns. - let vec_pat_lens = do m.filter_mapped |r| { + let vec_pat_lens = do m.iter().filter_map |r| { match r[0].node { pat_vec(ref before, ref slice, ref after) => { Some((before.len() + after.len(), slice.is_some())) } _ => None } - }; + }.collect::<~[(uint, bool)]>(); // Sort them by length such that for patterns of the same length, // those with a destructured slice come first. diff --git a/src/librustc/middle/trans/controlflow.rs b/src/librustc/middle/trans/controlflow.rs index dc88ecbe936..75b1830778b 100644 --- a/src/librustc/middle/trans/controlflow.rs +++ b/src/librustc/middle/trans/controlflow.rs @@ -26,7 +26,6 @@ use util::ppaux; use middle::trans::type_::Type; use std::str; -use std::vec; use syntax::ast; use syntax::ast::ident; use syntax::ast_map::path_mod; @@ -190,9 +189,13 @@ pub fn trans_log(log_ex: &ast::expr, let (modpath, modname) = { let path = &mut bcx.fcx.path; - let modpath = vec::append( - ~[path_mod(ccx.sess.ident_of(ccx.link_meta.name))], - path.filtered(|e| match *e { path_mod(_) => true, _ => false })); + let mut modpath = ~[path_mod(ccx.sess.ident_of(ccx.link_meta.name))]; + for path.iter().advance |e| { + match *e { + path_mod(_) => { modpath.push(*e) } + _ => {} + } + } let modname = path_str(ccx.sess, modpath); (modpath, modname) }; diff --git a/src/librustdoc/attr_parser.rs b/src/librustdoc/attr_parser.rs index 7655e173e4e..0681cf867f1 100644 --- a/src/librustdoc/attr_parser.rs +++ b/src/librustdoc/attr_parser.rs @@ -45,9 +45,9 @@ pub fn parse_crate(attrs: ~[ast::attribute]) -> CrateAttrs { } pub fn parse_desc(attrs: ~[ast::attribute]) -> Option<~str> { - let doc_strs = do doc_metas(attrs).filter_mapped |meta| { - attr::get_meta_item_value_str(*meta).map(|s| s.to_owned()) - }; + let doc_strs = do doc_metas(attrs).consume_iter().filter_map |meta| { + attr::get_meta_item_value_str(meta).map(|s| s.to_owned()) + }.collect::<~[~str]>(); if doc_strs.is_empty() { None } else { diff --git a/src/librustdoc/doc.rs b/src/librustdoc/doc.rs index b45550c06e4..ffb4642be81 100644 --- a/src/librustdoc/doc.rs +++ b/src/librustdoc/doc.rs @@ -13,8 +13,6 @@ use doc; -use std::vec; - pub type AstId = int; #[deriving(Eq)] @@ -186,87 +184,64 @@ impl Doc { } } +macro_rules! filt_mapper { + ($vec:expr, $pat:pat) => { + do ($vec).iter().filter_map |thing| { + match thing { + &$pat => Some(copy *x), + _ => None + } + }.collect() + } +} + +macro_rules! md { + ($id:ident) => { + filt_mapper!(self.items, $id(ref x)) + } +} /// Some helper methods on ModDoc, mostly for testing impl ModDoc { pub fn mods(&self) -> ~[ModDoc] { - do vec::filter_mapped(self.items) |itemtag| { - match copy *itemtag { - ModTag(ModDoc) => Some(ModDoc), - _ => None - } - } + md!(ModTag) } pub fn nmods(&self) -> ~[NmodDoc] { - do vec::filter_mapped(self.items) |itemtag| { - match copy *itemtag { - NmodTag(nModDoc) => Some(nModDoc), - _ => None - } - } + md!(NmodTag) } pub fn fns(&self) -> ~[FnDoc] { - do vec::filter_mapped(self.items) |itemtag| { - match copy *itemtag { - FnTag(FnDoc) => Some(FnDoc), - _ => None - } - } + md!(FnTag) } pub fn consts(&self) -> ~[ConstDoc] { - do vec::filter_mapped(self.items) |itemtag| { - match copy *itemtag { - ConstTag(ConstDoc) => Some(ConstDoc), - _ => None - } - } + md!(ConstTag) } pub fn enums(&self) -> ~[EnumDoc] { - do vec::filter_mapped(self.items) |itemtag| { - match copy *itemtag { - EnumTag(EnumDoc) => Some(EnumDoc), - _ => None - } - } + md!(EnumTag) } pub fn traits(&self) -> ~[TraitDoc] { - do vec::filter_mapped(self.items) |itemtag| { - match copy *itemtag { - TraitTag(TraitDoc) => Some(TraitDoc), - _ => None - } - } + md!(TraitTag) } pub fn impls(&self) -> ~[ImplDoc] { - do vec::filter_mapped(self.items) |itemtag| { - match copy *itemtag { - ImplTag(ImplDoc) => Some(ImplDoc), - _ => None - } - } + md!(ImplTag) } pub fn types(&self) -> ~[TyDoc] { - do vec::filter_mapped(self.items) |itemtag| { - match copy *itemtag { - TyTag(TyDoc) => Some(TyDoc), - _ => None - } - } + md!(TyTag) } pub fn structs(&self) -> ~[StructDoc] { - do vec::filter_mapped(self.items) |itemtag| { - match copy *itemtag { - StructTag(StructDoc) => Some(StructDoc), - _ => None - } - } + md!(StructTag) + } +} + +macro_rules! pu { + ($id:ident) => { + filt_mapper!(*self, ItemPage($id(ref x))) } } @@ -284,75 +259,35 @@ pub trait PageUtils { impl PageUtils for ~[Page] { fn mods(&self) -> ~[ModDoc] { - do vec::filter_mapped(*self) |page| { - match copy *page { - ItemPage(ModTag(ModDoc)) => Some(ModDoc), - _ => None - } - } + pu!(ModTag) } fn nmods(&self) -> ~[NmodDoc] { - do vec::filter_mapped(*self) |page| { - match copy *page { - ItemPage(NmodTag(nModDoc)) => Some(nModDoc), - _ => None - } - } + pu!(NmodTag) } fn fns(&self) -> ~[FnDoc] { - do vec::filter_mapped(*self) |page| { - match copy *page { - ItemPage(FnTag(FnDoc)) => Some(FnDoc), - _ => None - } - } + pu!(FnTag) } fn consts(&self) -> ~[ConstDoc] { - do vec::filter_mapped(*self) |page| { - match copy *page { - ItemPage(ConstTag(ConstDoc)) => Some(ConstDoc), - _ => None - } - } + pu!(ConstTag) } fn enums(&self) -> ~[EnumDoc] { - do vec::filter_mapped(*self) |page| { - match copy *page { - ItemPage(EnumTag(EnumDoc)) => Some(EnumDoc), - _ => None - } - } + pu!(EnumTag) } fn traits(&self) -> ~[TraitDoc] { - do vec::filter_mapped(*self) |page| { - match copy *page { - ItemPage(TraitTag(TraitDoc)) => Some(TraitDoc), - _ => None - } - } + pu!(TraitTag) } fn impls(&self) -> ~[ImplDoc] { - do vec::filter_mapped(*self) |page| { - match copy *page { - ItemPage(ImplTag(ImplDoc)) => Some(ImplDoc), - _ => None - } - } + pu!(ImplTag) } fn types(&self) -> ~[TyDoc] { - do vec::filter_mapped(*self) |page| { - match copy *page { - ItemPage(TyTag(TyDoc)) => Some(TyDoc), - _ => None - } - } + pu!(TyTag) } } diff --git a/src/librustdoc/extract.rs b/src/librustdoc/extract.rs index d5d2b4ce628..01b77a985fe 100644 --- a/src/librustdoc/extract.rs +++ b/src/librustdoc/extract.rs @@ -15,7 +15,6 @@ use astsrv; use doc::ItemUtils; use doc; -use std::vec; use syntax::ast; use syntax::parse::token::{ident_interner, ident_to_str}; use syntax::parse::token; @@ -83,7 +82,7 @@ fn moddoc_from_mod( ) -> doc::ModDoc { doc::ModDoc { item: itemdoc, - items: do vec::filter_mapped(module_.items) |item| { + items: do module_.items.iter().filter_map |item| { let ItemDoc = mk_itemdoc(item.id, to_str(item.ident)); match copy item.node { ast::item_mod(m) => { @@ -133,7 +132,7 @@ fn moddoc_from_mod( } _ => None } - }, + }.collect(), index: None } } diff --git a/src/librustdoc/page_pass.rs b/src/librustdoc/page_pass.rs index 508cf302ede..83a0d44978e 100644 --- a/src/librustdoc/page_pass.rs +++ b/src/librustdoc/page_pass.rs @@ -128,13 +128,12 @@ fn fold_mod( fn strip_mod(doc: doc::ModDoc) -> doc::ModDoc { doc::ModDoc { - items: do doc.items.filtered |item| { - match *item { - doc::ModTag(_) => false, - doc::NmodTag(_) => false, + items: do doc.items.iter().filter |item| { + match **item { + doc::ModTag(_) | doc::NmodTag(_) => false, _ => true } - }, + }.transform(|x| copy *x).collect::<~[doc::ItemTag]>(), .. copy doc } } diff --git a/src/librustdoc/prune_hidden_pass.rs b/src/librustdoc/prune_hidden_pass.rs index 484eb8c7980..96c5df10680 100644 --- a/src/librustdoc/prune_hidden_pass.rs +++ b/src/librustdoc/prune_hidden_pass.rs @@ -41,9 +41,9 @@ fn fold_mod( let doc = fold::default_any_fold_mod(fold, doc); doc::ModDoc { - items: do doc.items.filtered |ItemTag| { - !is_hidden(fold.ctxt.clone(), ItemTag.item()) - }, + items: do doc.items.iter().filter |item_tag| { + !is_hidden(fold.ctxt.clone(), item_tag.item()) + }.transform(|x| copy *x).collect(), .. doc } } diff --git a/src/librustdoc/prune_private_pass.rs b/src/librustdoc/prune_private_pass.rs index cb7da801e96..aeb6e02f244 100644 --- a/src/librustdoc/prune_private_pass.rs +++ b/src/librustdoc/prune_private_pass.rs @@ -80,7 +80,7 @@ fn strip_priv_methods( methods: &[@ast::method], item_vis: ast::visibility ) -> doc::ImplDoc { - let methods = do (&doc.methods).filtered |method| { + let methods = do doc.methods.iter().filter |method| { let ast_method = do methods.iter().find_ |m| { extract::to_str(m.ident) == method.name }; @@ -91,7 +91,7 @@ fn strip_priv_methods( ast::private => false, ast::inherited => item_vis == ast::public } - }; + }.transform(|x| copy *x).collect(); doc::ImplDoc { methods: methods, @@ -106,9 +106,9 @@ fn fold_mod( let doc = fold::default_any_fold_mod(fold, doc); doc::ModDoc { - items: doc.items.filtered(|ItemTag| { - match ItemTag { - &doc::ImplTag(ref doc) => { + items: doc.items.iter().filter(|item_tag| { + match item_tag { + & &doc::ImplTag(ref doc) => { if doc.trait_types.is_empty() { // This is an associated impl. We have already pruned the // non-visible methods. If there are any left then @@ -123,10 +123,10 @@ fn fold_mod( } } _ => { - is_visible(fold.ctxt.clone(), ItemTag.item()) + is_visible(fold.ctxt.clone(), item_tag.item()) } } - }), + }).transform(|x| copy *x).collect(), .. doc } } diff --git a/src/librustpkg/util.rs b/src/librustpkg/util.rs index 669e5042d3b..1e99a3fa4bc 100644 --- a/src/librustpkg/util.rs +++ b/src/librustpkg/util.rs @@ -77,9 +77,9 @@ fn fold_mod(_ctx: @mut ReadyCtx, fold: @fold::ast_fold) -> ast::_mod { fn strip_main(item: @ast::item) -> @ast::item { @ast::item { - attrs: do item.attrs.filtered |attr| { - "main" != attr::get_attr_name(attr) - }, + attrs: do item.attrs.iter().filter_map |attr| { + if "main" != attr::get_attr_name(attr) {Some(*attr)} else {None} + }.collect(), .. copy *item } } diff --git a/src/librustpkg/workspace.rs b/src/librustpkg/workspace.rs index dd2cf445302..5876dbdc9de 100644 --- a/src/librustpkg/workspace.rs +++ b/src/librustpkg/workspace.rs @@ -34,6 +34,7 @@ pub fn each_pkg_parent_workspace(pkgid: &PkgId, action: &fn(&Path) -> bool) -> b } pub fn pkg_parent_workspaces(pkgid: &PkgId) -> ~[Path] { - rust_path().filtered(|ws| - workspace_contains_package_id(pkgid, ws)) + rust_path().consume_iter() + .filter(|ws| workspace_contains_package_id(pkgid, ws)) + .collect() } diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 39041b48369..c994bbf6fa4 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -777,9 +777,9 @@ pub fn list_dir(p: &Path) -> ~[~str] { strings } } - do get_list(p).filtered |filename| { - *filename != ~"." && *filename != ~".." - } + do get_list(p).consume_iter().filter |filename| { + "." != *filename && ".." != *filename + }.collect() } } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 3fa9df2a9e0..5d4943c49d9 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -309,77 +309,6 @@ pub fn flat_map(v: &[T], f: &fn(t: &T) -> ~[U]) -> ~[U] { result } -pub fn filter_map( - v: ~[T], - f: &fn(t: T) -> Option) -> ~[U] -{ - /*! - * - * Apply a function to each element of a vector and return the results. - * Consumes the input vector. If function `f` returns `None` then that - * element is excluded from the resulting vector. - */ - - let mut result = ~[]; - for v.consume_iter().advance |elem| { - match f(elem) { - None => {} - Some(result_elem) => { result.push(result_elem); } - } - } - result -} - -pub fn filter_mapped( - v: &[T], - f: &fn(t: &T) -> Option) -> ~[U] -{ - /*! - * - * Like `filter_map()`, but operates on a borrowed slice - * and does not consume the input. - */ - - let mut result = ~[]; - for v.iter().advance |elem| { - match f(elem) { - None => {/* no-op */ } - Some(result_elem) => { result.push(result_elem); } - } - } - result -} - -/** - * Construct a new vector from the elements of a vector for which some - * predicate holds. - * - * Apply function `f` to each element of `v` and return a vector containing - * only those elements for which `f` returned true. - */ -pub fn filter(v: ~[T], f: &fn(t: &T) -> bool) -> ~[T] { - let mut result = ~[]; - for v.consume_iter().advance |elem| { - if f(&elem) { result.push(elem); } - } - result -} - -/** - * Construct a new vector from the elements of a vector for which some - * predicate holds. - * - * Apply function `f` to each element of `v` and return a vector containing - * only those elements for which `f` returned true. - */ -pub fn filtered(v: &[T], f: &fn(t: &T) -> bool) -> ~[T] { - let mut result = ~[]; - for v.iter().advance |elem| { - if f(elem) { result.push(copy *elem); } - } - result -} - /// Flattens a vector of vectors of T into a single vector of T. pub fn concat(v: &[~[T]]) -> ~[T] { v.concat_vec() } @@ -866,7 +795,6 @@ pub trait ImmutableVector<'self, T> { fn last_opt(&self) -> Option<&'self T>; fn rposition(&self, f: &fn(t: &T) -> bool) -> Option; fn flat_map(&self, f: &fn(t: &T) -> ~[U]) -> ~[U]; - fn filter_mapped(&self, f: &fn(t: &T) -> Option) -> ~[U]; unsafe fn unsafe_ref(&self, index: uint) -> *T; fn bsearch(&self, f: &fn(&T) -> Ordering) -> Option; @@ -976,17 +904,6 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] { fn flat_map(&self, f: &fn(t: &T) -> ~[U]) -> ~[U] { flat_map(*self, f) } - /** - * Apply a function to each element of a vector and return the results - * - * If function `f` returns `none` then that element is excluded from - * the resulting vector. - */ - #[inline] - fn filter_mapped(&self, f: &fn(t: &T) -> Option) -> ~[U] { - filter_mapped(*self, f) - } - /// Returns a pointer to the element at the given index, without doing /// bounds checking. #[inline] @@ -1077,25 +994,12 @@ impl<'self, T: TotalOrd> ImmutableTotalOrdVector for &'self [T] { #[allow(missing_doc)] pub trait ImmutableCopyableVector { - fn filtered(&self, f: &fn(&T) -> bool) -> ~[T]; fn partitioned(&self, f: &fn(&T) -> bool) -> (~[T], ~[T]); unsafe fn unsafe_get(&self, elem: uint) -> T; } /// Extension methods for vectors impl<'self,T:Copy> ImmutableCopyableVector for &'self [T] { - /** - * Construct a new vector from the elements of a vector for which some - * predicate holds. - * - * Apply function `f` to each element of `v` and return a vector - * containing only those elements for which `f` returned true. - */ - #[inline] - fn filtered(&self, f: &fn(t: &T) -> bool) -> ~[T] { - filtered(*self, f) - } - /** * Partitions the vector into those that satisfies the predicate, and * those that do not. @@ -1144,7 +1048,6 @@ pub trait OwnedVector { fn swap_remove(&mut self, index: uint) -> T; fn truncate(&mut self, newlen: uint); fn retain(&mut self, f: &fn(t: &T) -> bool); - fn filter(self, f: &fn(t: &T) -> bool) -> ~[T]; fn partition(self, f: &fn(&T) -> bool) -> (~[T], ~[T]); fn grow_fn(&mut self, n: uint, op: &fn(uint) -> T); } @@ -1482,11 +1385,6 @@ impl OwnedVector for ~[T] { } } - #[inline] - fn filter(self, f: &fn(&T) -> bool) -> ~[T] { - filter(self, f) - } - /** * Partitions the vector into those that satisfies the predicate, and * those that do not. @@ -2616,87 +2514,6 @@ mod tests { assert_eq!(w[4], 25u); } - #[test] - fn test_filter_mapped() { - // Test on-stack filter-map. - let mut v = ~[1u, 2u, 3u]; - let mut w = filter_mapped(v, square_if_odd_r); - assert_eq!(w.len(), 2u); - assert_eq!(w[0], 1u); - assert_eq!(w[1], 9u); - - // Test on-heap filter-map. - v = ~[1u, 2u, 3u, 4u, 5u]; - w = filter_mapped(v, square_if_odd_r); - assert_eq!(w.len(), 3u); - assert_eq!(w[0], 1u); - assert_eq!(w[1], 9u); - assert_eq!(w[2], 25u); - - fn halve(i: &int) -> Option { - if *i % 2 == 0 { - Some::(*i / 2) - } else { - None:: - } - } - fn halve_for_sure(i: &int) -> int { *i / 2 } - let all_even: ~[int] = ~[0, 2, 8, 6]; - let all_odd1: ~[int] = ~[1, 7, 3]; - let all_odd2: ~[int] = ~[]; - let mix: ~[int] = ~[9, 2, 6, 7, 1, 0, 0, 3]; - let mix_dest: ~[int] = ~[1, 3, 0, 0]; - assert!(filter_mapped(all_even, halve) == - all_even.map(halve_for_sure)); - assert_eq!(filter_mapped(all_odd1, halve), ~[]); - assert_eq!(filter_mapped(all_odd2, halve), ~[]); - assert_eq!(filter_mapped(mix, halve), mix_dest); - } - - #[test] - fn test_filter_map() { - // Test on-stack filter-map. - let mut v = ~[1u, 2u, 3u]; - let mut w = filter_map(v, square_if_odd_v); - assert_eq!(w.len(), 2u); - assert_eq!(w[0], 1u); - assert_eq!(w[1], 9u); - - // Test on-heap filter-map. - v = ~[1u, 2u, 3u, 4u, 5u]; - w = filter_map(v, square_if_odd_v); - assert_eq!(w.len(), 3u); - assert_eq!(w[0], 1u); - assert_eq!(w[1], 9u); - assert_eq!(w[2], 25u); - - fn halve(i: int) -> Option { - if i % 2 == 0 { - Some::(i / 2) - } else { - None:: - } - } - fn halve_for_sure(i: &int) -> int { *i / 2 } - let all_even: ~[int] = ~[0, 2, 8, 6]; - let all_even0: ~[int] = copy all_even; - let all_odd1: ~[int] = ~[1, 7, 3]; - let all_odd2: ~[int] = ~[]; - let mix: ~[int] = ~[9, 2, 6, 7, 1, 0, 0, 3]; - let mix_dest: ~[int] = ~[1, 3, 0, 0]; - assert!(filter_map(all_even, halve) == - all_even0.map(halve_for_sure)); - assert_eq!(filter_map(all_odd1, halve), ~[]); - assert_eq!(filter_map(all_odd2, halve), ~[]); - assert_eq!(filter_map(mix, halve), mix_dest); - } - - #[test] - fn test_filter() { - assert_eq!(filter(~[1u, 2u, 3u], is_odd), ~[1u, 3u]); - assert_eq!(filter(~[1u, 2u, 4u, 8u, 16u], is_three), ~[]); - } - #[test] fn test_retain() { let mut v = ~[1, 2, 3, 4, 5]; @@ -3227,38 +3044,6 @@ mod tests { }; } - #[test] - #[ignore(windows)] - #[should_fail] - #[allow(non_implicitly_copyable_typarams)] - fn test_filter_mapped_fail() { - let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)]; - let mut i = 0; - do filter_mapped(v) |_elt| { - if i == 2 { - fail!() - } - i += 0; - Some((~0, @0)) - }; - } - - #[test] - #[ignore(windows)] - #[should_fail] - #[allow(non_implicitly_copyable_typarams)] - fn test_filter_fail() { - let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)]; - let mut i = 0; - do v.filtered |_elt| { - if i == 2 { - fail!() - } - i += 0; - true - }; - } - #[test] #[ignore(windows)] #[should_fail] diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 529d5bfe70b..ce8e24fd444 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -238,12 +238,12 @@ pub fn unguarded_pat(a: &arm) -> Option<~[@pat]> { } pub fn public_methods(ms: ~[@method]) -> ~[@method] { - do ms.filtered |m| { + do ms.consume_iter().filter |m| { match m.vis { public => true, _ => false } - } + }.collect() } // extract a ty_method from a trait_method. if the trait_method is diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index a1a0c700628..d04d96b2481 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -143,13 +143,13 @@ pub fn get_name_value_str_pair(item: @ast::meta_item) /// Search a list of attributes and return only those with a specific name pub fn find_attrs_by_name(attrs: &[ast::attribute], name: &str) -> ~[ast::attribute] { - do vec::filter_mapped(attrs) |a| { + do attrs.iter().filter_map |a| { if name == get_attr_name(a) { Some(*a) } else { None } - } + }.collect() } /// Search a list of meta items and return only those with a specific name @@ -277,14 +277,7 @@ pub fn sort_meta_items(items: &[@ast::meta_item]) -> ~[@ast::meta_item] { pub fn remove_meta_items_by_name(items: ~[@ast::meta_item], name: &str) -> ~[@ast::meta_item] { - - return vec::filter_mapped(items, |item| { - if name != get_meta_item_name(*item) { - Some(*item) - } else { - None - } - }); + items.consume_iter().filter(|item| name != get_meta_item_name(*item)).collect() } /** diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 4e145123996..96d7685353b 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -14,8 +14,6 @@ use codemap::{span, spanned}; use parse::token; use opt_vec::OptVec; -use std::vec; - pub trait ast_fold { fn fold_crate(@self, &crate) -> crate; fn fold_view_item(@self, @view_item) -> @view_item; @@ -700,7 +698,7 @@ pub fn noop_fold_ty(t: &ty_, fld: @ast_fold) -> ty_ { pub fn noop_fold_mod(m: &_mod, fld: @ast_fold) -> _mod { ast::_mod { view_items: m.view_items.iter().transform(|x| fld.fold_view_item(*x)).collect(), - items: vec::filter_mapped(m.items, |x| fld.fold_item(*x)), + items: m.items.iter().filter_map(|x| fld.fold_item(*x)).collect(), } } diff --git a/src/test/compile-fail/lint-unused-imports.rs b/src/test/compile-fail/lint-unused-imports.rs index e61de0ac11f..e7e01a40487 100644 --- a/src/test/compile-fail/lint-unused-imports.rs +++ b/src/test/compile-fail/lint-unused-imports.rs @@ -30,7 +30,7 @@ use std::io::WriterUtil; // Make sure this import is warned about when at least one of its imported names // is unused -use std::vec::{filter, from_elem}; //~ ERROR unused import +use std::vec::{from_fn, from_elem}; //~ ERROR unused import mod foo { pub struct Point{x: int, y: int}