mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-06 23:17:37 +00:00
Demode iter::foldl and friends
This commit is contained in:
parent
fdc6062136
commit
a3a257cc3b
@ -675,7 +675,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_dlist_foldl() {
|
fn test_dlist_foldl() {
|
||||||
let l = from_vec(vec::from_fn(101, |x|x));
|
let l = from_vec(vec::from_fn(101, |x|x));
|
||||||
assert iter::foldl(l, 0, |accum,elem| accum+elem) == 5050;
|
assert iter::foldl(&l, 0, |accum,elem| *accum+*elem) == 5050;
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_dlist_break_early() {
|
fn test_dlist_break_early() {
|
||||||
|
@ -16,8 +16,8 @@ impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
|
|||||||
pure fn eachi(blk: fn(uint, v: &A) -> bool) { iter::eachi(&self, blk) }
|
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 all(blk: fn(&A) -> bool) -> bool { iter::all(&self, blk) }
|
||||||
pure fn any(blk: fn(&A) -> bool) -> bool { iter::any(&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 {
|
pure fn foldl<B>(+b0: B, blk: fn(&B, &A) -> B) -> B {
|
||||||
iter::foldl(self, move b0, blk)
|
iter::foldl(&self, move b0, blk)
|
||||||
}
|
}
|
||||||
pure fn position(f: fn(A) -> bool) -> Option<uint> {
|
pure fn position(f: fn(A) -> bool) -> Option<uint> {
|
||||||
iter::position(self, f)
|
iter::position(self, f)
|
||||||
@ -26,7 +26,7 @@ impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
|
|||||||
|
|
||||||
impl<A: Eq> IMPL_T<A>: iter::EqIter<A> {
|
impl<A: Eq> IMPL_T<A>: iter::EqIter<A> {
|
||||||
pure fn contains(x: &A) -> bool { iter::contains(self, x) }
|
pure fn contains(x: &A) -> bool { iter::contains(self, x) }
|
||||||
pure fn count(x: &A) -> uint { iter::count(self, x) }
|
pure fn count(x: &A) -> uint { iter::count(&self, x) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {
|
impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {
|
||||||
@ -36,7 +36,7 @@ impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {
|
|||||||
pure fn map_to_vec<B>(op: fn(+v: A) -> B) -> ~[B] {
|
pure fn map_to_vec<B>(op: fn(+v: A) -> B) -> ~[B] {
|
||||||
iter::map_to_vec(&self, op)
|
iter::map_to_vec(&self, op)
|
||||||
}
|
}
|
||||||
pure fn to_vec() -> ~[A] { iter::to_vec(self) }
|
pure fn to_vec() -> ~[A] { iter::to_vec(&self) }
|
||||||
|
|
||||||
pure fn flat_map_to_vec<B:Copy,IB:BaseIter<B>>(op: fn(+a: A) -> IB)
|
pure fn flat_map_to_vec<B:Copy,IB:BaseIter<B>>(op: fn(+a: A) -> IB)
|
||||||
-> ~[B] {
|
-> ~[B] {
|
||||||
@ -47,7 +47,7 @@ impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<A: Copy Ord> IMPL_T<A>: iter::CopyableOrderedIter<A> {
|
impl<A: Copy Ord> IMPL_T<A>: iter::CopyableOrderedIter<A> {
|
||||||
pure fn min() -> A { iter::min(self) }
|
pure fn min() -> A { iter::min(&self) }
|
||||||
pure fn max() -> A { iter::max(self) }
|
pure fn max() -> A { iter::max(&self) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ trait ExtendedIter<A> {
|
|||||||
pure fn eachi(blk: fn(uint, v: &A) -> bool);
|
pure fn eachi(blk: fn(uint, v: &A) -> bool);
|
||||||
pure fn all(blk: fn(&A) -> bool) -> bool;
|
pure fn all(blk: fn(&A) -> bool) -> bool;
|
||||||
pure fn any(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 foldl<B>(+b0: B, blk: fn(&B, &A) -> B) -> B;
|
||||||
pure fn position(f: fn(A) -> bool) -> Option<uint>;
|
pure fn position(f: fn(A) -> bool) -> Option<uint>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,16 +118,17 @@ pure fn flat_map_to_vec<A:Copy,B:Copy,IA:BaseIter<A>,IB:BaseIter<B>>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn foldl<A,B,IA:BaseIter<A>>(self: IA, +b0: B, blk: fn(B, A) -> B) -> B {
|
pure fn foldl<A,B,IA:BaseIter<A>>(self: &IA, +b0: B, blk: fn(&B, &A) -> B)
|
||||||
|
-> B {
|
||||||
let mut b <- b0;
|
let mut b <- b0;
|
||||||
for self.each |a| {
|
for self.each |a| {
|
||||||
b = blk(b, *a);
|
b = blk(&b, a);
|
||||||
}
|
}
|
||||||
move b
|
move b
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn to_vec<A:Copy,IA:BaseIter<A>>(self: IA) -> ~[A] {
|
pure fn to_vec<A:Copy,IA:BaseIter<A>>(self: &IA) -> ~[A] {
|
||||||
foldl::<A,~[A],IA>(self, ~[], |r, a| vec::append(copy r, ~[a]))
|
foldl::<A,~[A],IA>(self, ~[], |r, a| vec::append(*r, ~[*a]))
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn contains<A:Eq,IA:BaseIter<A>>(self: IA, x: &A) -> bool {
|
pure fn contains<A:Eq,IA:BaseIter<A>>(self: IA, x: &A) -> bool {
|
||||||
@ -137,12 +138,12 @@ pure fn contains<A:Eq,IA:BaseIter<A>>(self: IA, x: &A) -> bool {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn count<A:Eq,IA:BaseIter<A>>(self: IA, x: &A) -> uint {
|
pure fn count<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> uint {
|
||||||
do foldl(self, 0) |count, value| {
|
do foldl(self, 0) |count, value| {
|
||||||
if value == *x {
|
if *value == *x {
|
||||||
count + 1
|
*count + 1
|
||||||
} else {
|
} else {
|
||||||
count
|
*count
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -170,16 +171,13 @@ pure fn repeat(times: uint, blk: fn() -> bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX bad copies
|
pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
|
||||||
pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: IA) -> A {
|
|
||||||
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
|
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
|
||||||
match a {
|
match a {
|
||||||
Some(copy a_) if a_ < b => {
|
&Some(a_) if a_ < *b => {
|
||||||
// FIXME (#2005): Not sure if this is successfully optimized to
|
*(move a)
|
||||||
// a move
|
|
||||||
a
|
|
||||||
}
|
}
|
||||||
_ => Some(b)
|
_ => Some(*b)
|
||||||
}
|
}
|
||||||
} {
|
} {
|
||||||
Some(move val) => val,
|
Some(move val) => val,
|
||||||
@ -187,16 +185,13 @@ pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: IA) -> A {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX bad copies
|
pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
|
||||||
pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: IA) -> A {
|
|
||||||
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
|
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
|
||||||
match a {
|
match a {
|
||||||
Some(copy a_) if a_ > b => {
|
&Some(a_) if a_ > *b => {
|
||||||
// FIXME (#2005): Not sure if this is successfully optimized to
|
*(move a)
|
||||||
// a move.
|
|
||||||
a
|
|
||||||
}
|
}
|
||||||
_ => Some(b)
|
_ => Some(*b)
|
||||||
}
|
}
|
||||||
} {
|
} {
|
||||||
Some(move val) => val,
|
Some(move val) => val,
|
||||||
|
@ -1993,8 +1993,8 @@ impl<A> &[A]: iter::ExtendedIter<A> {
|
|||||||
pure fn eachi(blk: fn(uint, v: &A) -> bool) { iter::eachi(&self, blk) }
|
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 all(blk: fn(&A) -> bool) -> bool { iter::all(&self, blk) }
|
||||||
pure fn any(blk: fn(&A) -> bool) -> bool { iter::any(&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 {
|
pure fn foldl<B>(+b0: B, blk: fn(&B, &A) -> B) -> B {
|
||||||
iter::foldl(self, move b0, blk)
|
iter::foldl(&self, move b0, blk)
|
||||||
}
|
}
|
||||||
pure fn position(f: fn(A) -> bool) -> Option<uint> {
|
pure fn position(f: fn(A) -> bool) -> Option<uint> {
|
||||||
iter::position(self, f)
|
iter::position(self, f)
|
||||||
@ -2003,7 +2003,7 @@ impl<A> &[A]: iter::ExtendedIter<A> {
|
|||||||
|
|
||||||
impl<A: Eq> &[A]: iter::EqIter<A> {
|
impl<A: Eq> &[A]: iter::EqIter<A> {
|
||||||
pure fn contains(x: &A) -> bool { iter::contains(self, x) }
|
pure fn contains(x: &A) -> bool { iter::contains(self, x) }
|
||||||
pure fn count(x: &A) -> uint { iter::count(self, x) }
|
pure fn count(x: &A) -> uint { iter::count(&self, x) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A: Copy> &[A]: iter::CopyableIter<A> {
|
impl<A: Copy> &[A]: iter::CopyableIter<A> {
|
||||||
@ -2013,7 +2013,7 @@ impl<A: Copy> &[A]: iter::CopyableIter<A> {
|
|||||||
pure fn map_to_vec<B>(op: fn(+v: A) -> B) -> ~[B] {
|
pure fn map_to_vec<B>(op: fn(+v: A) -> B) -> ~[B] {
|
||||||
iter::map_to_vec(&self, op)
|
iter::map_to_vec(&self, op)
|
||||||
}
|
}
|
||||||
pure fn to_vec() -> ~[A] { iter::to_vec(self) }
|
pure fn to_vec() -> ~[A] { iter::to_vec(&self) }
|
||||||
|
|
||||||
// FIXME--bug in resolve prevents this from working (#2611)
|
// 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] {
|
// fn flat_map_to_vec<B:copy,IB:base_iter<B>>(op: fn(A) -> IB) -> ~[B] {
|
||||||
@ -2024,8 +2024,8 @@ impl<A: Copy> &[A]: iter::CopyableIter<A> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<A: Copy Ord> &[A]: iter::CopyableOrderedIter<A> {
|
impl<A: Copy Ord> &[A]: iter::CopyableOrderedIter<A> {
|
||||||
pure fn min() -> A { iter::min(self) }
|
pure fn min() -> A { iter::min(&self) }
|
||||||
pure fn max() -> A { iter::max(self) }
|
pure fn max() -> A { iter::max(&self) }
|
||||||
}
|
}
|
||||||
// ___________________________________________________________________________
|
// ___________________________________________________________________________
|
||||||
|
|
||||||
|
@ -1014,7 +1014,7 @@ impl Liveness {
|
|||||||
fn propagate_through_opt_expr(opt_expr: Option<@expr>,
|
fn propagate_through_opt_expr(opt_expr: Option<@expr>,
|
||||||
succ: LiveNode) -> LiveNode {
|
succ: LiveNode) -> LiveNode {
|
||||||
do opt_expr.foldl(succ) |succ, expr| {
|
do opt_expr.foldl(succ) |succ, expr| {
|
||||||
self.propagate_through_expr(expr, succ)
|
self.propagate_through_expr(*expr, *succ)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -364,8 +364,8 @@ fn combine_impl_and_methods_origins(bcx: block,
|
|||||||
|
|
||||||
// Flatten out to find the number of vtables the method expects.
|
// Flatten out to find the number of vtables the method expects.
|
||||||
let m_vtables = m_boundss.foldl(0, |sum, m_bounds| {
|
let m_vtables = m_boundss.foldl(0, |sum, m_bounds| {
|
||||||
m_bounds.foldl(sum, |sum, m_bound| {
|
m_bounds.foldl(*sum, |sum, m_bound| {
|
||||||
sum + match m_bound {
|
(*sum) + match (*m_bound) {
|
||||||
ty::bound_copy | ty::bound_owned |
|
ty::bound_copy | ty::bound_owned |
|
||||||
ty::bound_send | ty::bound_const => 0,
|
ty::bound_send | ty::bound_const => 0,
|
||||||
ty::bound_trait(_) => 1
|
ty::bound_trait(_) => 1
|
||||||
|
@ -2137,25 +2137,25 @@ fn type_size(cx: ctxt, ty: t) -> uint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ty_rec(flds) => {
|
ty_rec(flds) => {
|
||||||
flds.foldl(0, |s, f| s + type_size(cx, f.mt.ty))
|
flds.foldl(0, |s, f| *s + type_size(cx, f.mt.ty))
|
||||||
}
|
}
|
||||||
|
|
||||||
ty_class(did, ref substs) => {
|
ty_class(did, ref substs) => {
|
||||||
let flds = class_items_as_fields(cx, did, substs);
|
let flds = class_items_as_fields(cx, did, substs);
|
||||||
flds.foldl(0, |s, f| s + type_size(cx, f.mt.ty))
|
flds.foldl(0, |s, f| *s + type_size(cx, f.mt.ty))
|
||||||
}
|
}
|
||||||
|
|
||||||
ty_tup(tys) => {
|
ty_tup(tys) => {
|
||||||
tys.foldl(0, |s, t| s + type_size(cx, t))
|
tys.foldl(0, |s, t| *s + type_size(cx, *t))
|
||||||
}
|
}
|
||||||
|
|
||||||
ty_enum(did, ref substs) => {
|
ty_enum(did, ref substs) => {
|
||||||
let variants = substd_enum_variants(cx, did, substs);
|
let variants = substd_enum_variants(cx, did, substs);
|
||||||
variants.foldl( // find max size of any variant
|
variants.foldl( // find max size of any variant
|
||||||
0,
|
0,
|
||||||
|m, v| uint::max(m,
|
|m, v| uint::max(*m,
|
||||||
// find size of this variant:
|
// find size of this variant:
|
||||||
v.args.foldl(0, |s, a| s + type_size(cx, a))))
|
v.args.foldl(0, |s, a| *s + type_size(cx, *a))))
|
||||||
}
|
}
|
||||||
|
|
||||||
ty_param(_) | ty_self => {
|
ty_param(_) | ty_self => {
|
||||||
|
@ -111,14 +111,14 @@ fn replace_bound_regions_in_fn_ty(
|
|||||||
|
|
||||||
// For each type `ty` in `tys`...
|
// For each type `ty` in `tys`...
|
||||||
do tys.foldl(isr) |isr, ty| {
|
do tys.foldl(isr) |isr, ty| {
|
||||||
let mut isr = isr;
|
let mut isr = *isr;
|
||||||
|
|
||||||
// Using fold_regions is inefficient, because it
|
// Using fold_regions is inefficient, because it
|
||||||
// constructs new types, but it avoids code duplication in
|
// constructs new types, but it avoids code duplication in
|
||||||
// terms of locating all the regions within the various
|
// terms of locating all the regions within the various
|
||||||
// kinds of types. This had already caused me several
|
// kinds of types. This had already caused me several
|
||||||
// bugs so I decided to switch over.
|
// bugs so I decided to switch over.
|
||||||
do ty::fold_regions(tcx, ty) |r, in_fn| {
|
do ty::fold_regions(tcx, *ty) |r, in_fn| {
|
||||||
if !in_fn { isr = append_isr(isr, to_r, r); }
|
if !in_fn { isr = append_isr(isr, to_r, r); }
|
||||||
r
|
r
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
fn add(&&x: float, &&y: uint) -> float { x + (y as float) }
|
fn add(x: &float, y: &uint) -> float { *x + ((*y) as float) }
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
assert [1u, 3u]/_.foldl(20f, add) == 24f;
|
assert [1u, 3u]/_.foldl(20f, add) == 24f;
|
||||||
|
Loading…
Reference in New Issue
Block a user