2012-12-08 14:22:43 -06:00
|
|
|
fn foldl<T, U: Copy>(
|
|
|
|
values: &[T],
|
|
|
|
initial: U,
|
|
|
|
function: &fn(partial: U, element: &T) -> U
|
|
|
|
) -> U {
|
|
|
|
match values {
|
|
|
|
[head, ..tail] =>
|
|
|
|
foldl(tail, function(initial, &head), function),
|
|
|
|
_ => copy initial
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-01 21:43:17 -06:00
|
|
|
pub fn main() {
|
2012-12-08 14:22:43 -06:00
|
|
|
let x = [1, 2, 3, 4, 5];
|
|
|
|
match x {
|
|
|
|
[a, b, c, d, e, f] => {
|
2013-01-07 20:54:28 -06:00
|
|
|
::core::util::unreachable();
|
2012-12-08 14:22:43 -06:00
|
|
|
}
|
|
|
|
[a, b, c, d, e] => {
|
|
|
|
assert a == 1;
|
|
|
|
assert b == 2;
|
|
|
|
assert c == 3;
|
|
|
|
assert d == 4;
|
|
|
|
assert e == 5;
|
|
|
|
}
|
|
|
|
_ => {
|
2013-01-07 20:54:28 -06:00
|
|
|
::core::util::unreachable();
|
2012-12-08 14:22:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let product = foldl(x, 1, |a, b| a * *b);
|
|
|
|
assert product == 120;
|
|
|
|
}
|