Skip to content

iter: add fold, sum and product #6544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ much easier to implement.
#[cfg(not(stage0))] use cmp::Ord;
#[cfg(not(stage0))] use option::{Option, Some, None};
#[cfg(not(stage0))] use vec::OwnedVector;
#[cfg(not(stage0))] use num::{One, Zero};
#[cfg(not(stage0))] use ops::{Add, Mul};

#[cfg(stage0)]
pub trait Times {
Expand Down Expand Up @@ -212,6 +214,81 @@ pub fn min<T: Ord>(iter: &fn(f: &fn(T) -> bool) -> bool) -> Option<T> {
result
}

/**
* Reduce an iterator to an accumulated value.
*
* # Example:
*
* ~~~~
* assert_eq!(fold(0i, |f| int::range(1, 5, f), |a, x| *a += x), 10);
* ~~~~
*/
#[cfg(not(stage0))]
#[inline]
pub fn fold<T, U>(start: T, iter: &fn(f: &fn(U) -> bool) -> bool, f: &fn(&mut T, U)) -> T {
let mut result = start;
for iter |x| {
f(&mut result, x);
}
result
}

/**
* Reduce an iterator to an accumulated value.
*
* `fold_ref` is usable in some generic functions where `fold` is too lenient to type-check, but it
* forces the iterator to yield borrowed pointers.
*
* # Example:
*
* ~~~~
* fn product<T: One + Mul<T, T>>(iter: &fn(f: &fn(&T) -> bool) -> bool) -> T {
* fold_ref(One::one::<T>(), iter, |a, x| *a = a.mul(x))
* }
* ~~~~
*/
#[cfg(not(stage0))]
#[inline]
pub fn fold_ref<T, U>(start: T, iter: &fn(f: &fn(&U) -> bool) -> bool, f: &fn(&mut T, &U)) -> T {
let mut result = start;
for iter |x| {
f(&mut result, x);
}
result
}

/**
* Return the sum of the items yielding by an iterator.
*
* # Example:
*
* ~~~~
* let xs: ~[int] = ~[1, 2, 3, 4];
* assert_eq!(do sum |f| { xs.each(f) }, 10);
* ~~~~
*/
#[cfg(not(stage0))]
#[inline(always)]
pub fn sum<T: Zero + Add<T, T>>(iter: &fn(f: &fn(&T) -> bool) -> bool) -> T {
fold_ref(Zero::zero::<T>(), iter, |a, x| *a = a.add(x))
}

/**
* Return the product of the items yielded by an iterator.
*
* # Example:
*
* ~~~~
* let xs: ~[int] = ~[1, 2, 3, 4];
* assert_eq!(do product |f| { xs.each(f) }, 24);
* ~~~~
*/
#[cfg(not(stage0))]
#[inline(always)]
pub fn product<T: One + Mul<T, T>>(iter: &fn(f: &fn(&T) -> bool) -> bool) -> T {
fold_ref(One::one::<T>(), iter, |a, x| *a = a.mul(x))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -254,4 +331,33 @@ mod tests {
let xs = ~[8, 2, 3, 1, -5, 9, 11, 15];
assert_eq!(min(|f| xs.each(f)).unwrap(), &-5);
}

#[test]
fn test_fold() {
assert_eq!(fold(0i, |f| int::range(1, 5, f), |a, x| *a += x), 10);
}

#[test]
fn test_sum() {
let xs: ~[int] = ~[1, 2, 3, 4];
assert_eq!(do sum |f| { xs.each(f) }, 10);
}

#[test]
fn test_empty_sum() {
let xs: ~[int] = ~[];
assert_eq!(do sum |f| { xs.each(f) }, 0);
}

#[test]
fn test_product() {
let xs: ~[int] = ~[1, 2, 3, 4];
assert_eq!(do product |f| { xs.each(f) }, 24);
}

#[test]
fn test_empty_product() {
let xs: ~[int] = ~[];
assert_eq!(do product |f| { xs.each(f) }, 1);
}
}