提交 85bb1fc2 编写于 作者: E Erick Tryzelaar

Change iter::find's closure to take a ref

上级 88962eee
......@@ -40,6 +40,14 @@ impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
pure fn position(&self, f: fn(&A) -> bool) -> Option<uint> {
iter::position(self, f)
}
pure fn map_to_vec<B>(&self, op: fn(&A) -> B) -> ~[B] {
iter::map_to_vec(self, op)
}
pure fn flat_map_to_vec<B,IB:BaseIter<B>>(&self, op: fn(&A) -> IB)
-> ~[B] {
iter::flat_map_to_vec(self, op)
}
}
impl<A: Eq> IMPL_T<A>: iter::EqIter<A> {
......@@ -48,19 +56,11 @@ impl<A: Eq> IMPL_T<A>: iter::EqIter<A> {
}
impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {
pure fn filter_to_vec(&self, pred: fn(a: A) -> bool) -> ~[A] {
pure fn filter_to_vec(&self, pred: fn(&A) -> bool) -> ~[A] {
iter::filter_to_vec(self, pred)
}
pure fn map_to_vec<B>(&self, op: fn(v: A) -> B) -> ~[B] {
iter::map_to_vec(self, op)
}
pure fn to_vec(&self) -> ~[A] { iter::to_vec(self) }
pure fn flat_map_to_vec<B:Copy,IB:BaseIter<B>>(&self, op: fn(a: A) -> IB)
-> ~[B] {
iter::flat_map_to_vec(self, op)
}
pure fn find(&self, f: fn(A) -> bool) -> Option<A> {
pure fn find(&self, f: fn(&A) -> bool) -> Option<A> {
iter::find(self, f)
}
}
......
......@@ -33,6 +33,9 @@ pub trait ExtendedIter<A> {
pure fn any(&self, blk: fn(&A) -> bool) -> bool;
pure fn foldl<B>(&self, b0: B, blk: fn(&B, &A) -> B) -> B;
pure fn position(&self, f: fn(&A) -> bool) -> Option<uint>;
pure fn map_to_vec<B>(&self, op: fn(&A) -> B) -> ~[B];
pure fn flat_map_to_vec<B,IB: BaseIter<B>>(&self, op: fn(&A) -> IB)
-> ~[B];
}
pub trait EqIter<A:Eq> {
......@@ -45,12 +48,9 @@ pub trait Times {
}
pub trait CopyableIter<A:Copy> {
pure fn filter_to_vec(&self, pred: fn(a: A) -> bool) -> ~[A];
pure fn map_to_vec<B>(&self, op: fn(v: A) -> B) -> ~[B];
pure fn flat_map_to_vec<B:Copy,IB: BaseIter<B>>(&self, op: fn(A) -> IB)
-> ~[B];
pure fn filter_to_vec(&self, pred: fn(&A) -> bool) -> ~[A];
pure fn to_vec(&self) -> ~[A];
pure fn find(&self, p: fn(A) -> bool) -> Option<A>;
pure fn find(&self, p: fn(&A) -> bool) -> Option<A>;
}
pub trait CopyableOrderedIter<A:Copy Ord> {
......@@ -82,11 +82,11 @@ pub trait Buildable<A> {
* onto the sequence being constructed.
*/
static pure fn build_sized(size: uint,
builder: fn(push: pure fn(v: A))) -> self;
builder: fn(push: pure fn(A))) -> self;
}
pub pure fn eachi<A,IA:BaseIter<A>>(self: &IA,
blk: fn(uint, v: &A) -> bool) {
blk: fn(uint, &A) -> bool) {
let mut i = 0;
for self.each |a| {
if !blk(i, a) { break; }
......@@ -111,30 +111,30 @@ pub trait Buildable<A> {
}
pub pure fn filter_to_vec<A:Copy,IA:BaseIter<A>>(
self: &IA, prd: fn(a: A) -> bool) -> ~[A] {
self: &IA, prd: fn(&A) -> bool) -> ~[A] {
do vec::build_sized_opt(self.size_hint()) |push| {
for self.each |a| {
if prd(*a) { push(*a); }
if prd(a) { push(*a); }
}
}
}
pub pure fn map_to_vec<A:Copy,B,IA:BaseIter<A>>(self: &IA,
op: fn(v: A) -> B)
pub pure fn map_to_vec<A,B,IA:BaseIter<A>>(self: &IA,
op: fn(&A) -> B)
-> ~[B] {
do vec::build_sized_opt(self.size_hint()) |push| {
for self.each |a| {
push(op(*a));
push(op(a));
}
}
}
pub pure fn flat_map_to_vec<A:Copy,B:Copy,IA:BaseIter<A>,IB:BaseIter<B>>(
self: &IA, op: fn(a: A) -> IB) -> ~[B] {
pub pure fn flat_map_to_vec<A,B,IA:BaseIter<A>,IB:BaseIter<B>>(
self: &IA, op: fn(&A) -> IB) -> ~[B] {
do vec::build |push| {
for self.each |a| {
for op(*a).each |b| {
push(*b);
for op(a).each |&b| {
push(b);
}
}
}
......@@ -223,9 +223,9 @@ pub trait Buildable<A> {
}
pub pure fn find<A: Copy,IA:BaseIter<A>>(self: &IA,
f: fn(A) -> bool) -> Option<A> {
f: fn(&A) -> bool) -> Option<A> {
for self.each |i| {
if f(*i) { return Some(*i) }
if f(i) { return Some(*i) }
}
return None;
}
......@@ -243,7 +243,7 @@ pub trait Buildable<A> {
* onto the sequence being constructed.
*/
#[inline(always)]
pub pure fn build<A,B: Buildable<A>>(builder: fn(push: pure fn(v: A)))
pub pure fn build<A,B: Buildable<A>>(builder: fn(push: pure fn(A)))
-> B {
Buildable::build_sized(4, builder)
}
......@@ -264,7 +264,7 @@ pub trait Buildable<A> {
#[inline(always)]
pub pure fn build_sized_opt<A,B: Buildable<A>>(
size: Option<uint>,
builder: fn(push: pure fn(v: A))) -> B {
builder: fn(push: pure fn(A))) -> B {
Buildable::build_sized(size.get_default(4), builder)
}
......
......@@ -2040,6 +2040,13 @@ impl<A> &[A]: iter::ExtendedIter<A> {
pub pure fn position(&self, f: fn(&A) -> bool) -> Option<uint> {
iter::position(self, f)
}
pure fn map_to_vec<B>(&self, op: fn(&A) -> B) -> ~[B] {
iter::map_to_vec(self, op)
}
pure fn flat_map_to_vec<B,IB:BaseIter<B>>(&self, op: fn(&A) -> IB)
-> ~[B] {
iter::flat_map_to_vec(self, op)
}
}
impl<A: Eq> &[A]: iter::EqIter<A> {
......@@ -2048,20 +2055,11 @@ impl<A: Eq> &[A]: iter::EqIter<A> {
}
impl<A: Copy> &[A]: iter::CopyableIter<A> {
pure fn filter_to_vec(&self, pred: fn(a: A) -> bool) -> ~[A] {
pure fn filter_to_vec(&self, pred: fn(&A) -> bool) -> ~[A] {
iter::filter_to_vec(self, pred)
}
pure fn map_to_vec<B>(&self, op: fn(v: A) -> B) -> ~[B] {
iter::map_to_vec(self, op)
}
pure fn to_vec(&self) -> ~[A] { iter::to_vec(self) }
pure fn flat_map_to_vec<B:Copy,IB:BaseIter<B>>(&self, op: fn(A) -> IB)
-> ~[B] {
iter::flat_map_to_vec(self, op)
}
pub pure fn find(&self, f: fn(A) -> bool) -> Option<A> {
pub pure fn find(&self, f: fn(&A) -> bool) -> Option<A> {
iter::find(self, f)
}
}
......
......@@ -70,7 +70,7 @@ fn warn_if_multiple_versions(e: env, diag: span_handler,
if crate_cache.len() != 0u {
let name = loader::crate_name_from_metas(*crate_cache.last().metas);
let (matches, non_matches) =
partition(crate_cache.map_to_vec(|entry| {
partition(crate_cache.map_to_vec(|&entry| {
let othername = loader::crate_name_from_metas(*entry.metas);
if name == othername {
Left(entry)
......
......@@ -861,7 +861,7 @@ fn consider_candidates(&self,
-> Option<method_map_entry>
{
let relevant_candidates =
candidates.filter_to_vec(|c| self.is_relevant(self_ty, &c));
candidates.filter_to_vec(|c| self.is_relevant(self_ty, c));
let relevant_candidates = self.merge_candidates(relevant_candidates);
......
......@@ -115,7 +115,7 @@ fn parse_desc_should_parse_simple_doc_attributes() {
pub fn parse_hidden(+attrs: ~[ast::attribute]) -> bool {
do doc_metas(attrs).find |meta| {
match attr::get_meta_item_list(meta) {
match attr::get_meta_item_list(*meta) {
Some(metas) => {
let hiddens = attr::find_meta_items_by_name(metas, ~"hidden");
vec::is_not_empty(hiddens)
......
......@@ -218,8 +218,8 @@ fn visit<Tproto, Tstate, Tmessage, V: visitor<Tproto, Tstate, Tmessage>>(
proto: protocol, visitor: V) -> Tproto {
// 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 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;
visitor.visit_message(name, span, tys, this, next)
};
......
......@@ -8,17 +8,14 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// xfail-fast
#[legacy_modes];
use iter::BaseIter;
trait FlatMapToVec<A> {
fn flat_map_to_vec<B:Copy, IB:BaseIter<B>>(op: fn(+a: A) -> IB) -> ~[B];
fn flat_map_to_vec<B, IB:BaseIter<B>>(op: fn(&A) -> IB) -> ~[B];
}
impl<A:Copy> BaseIter<A>: FlatMapToVec<A> {
fn flat_map_to_vec<B:Copy, IB:BaseIter<B>>(op: fn(+a: A) -> IB) -> ~[B] {
fn flat_map_to_vec<B, IB:BaseIter<B>>(op: fn(&A) -> IB) -> ~[B] {
iter::flat_map_to_vec(&self, op)
}
}
......
......@@ -9,10 +9,9 @@
// except according to those terms.
// xfail-test -- flat_map_to_vec currently disable
fn repeat(x: &uint) -> ~[uint] { ~[x, x] }
fn repeat(&&x: uint) -> ~[uint] { ~[x, x] }
fn incd_if_even(&&x: uint) -> option<uint> {
fn incd_if_even(x: &uint) -> option<uint> {
if (x % 2u) == 0u {some(x + 1u)} else {none}
}
......@@ -28,4 +27,4 @@ fn main() {
assert none.flat_map_to_vec(incd_if_even) == ~[];
assert some(1u).flat_map_to_vec(incd_if_even) == ~[];
assert some(2u).flat_map_to_vec(incd_if_even) == ~[3u];
}
\ No newline at end of file
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册