Accumulator factory: Difference between revisions

Content added Content deleted
(Added Wren)
m (Modfiy rust solution to use impl Trait instead of Box<dyn Trait>)
Line 2,843: Line 2,843:
Changing "x = foo(1.)" to "x = foo(1)" in the code below should not change the output (it does).
Changing "x = foo(1.)" to "x = foo(1)" in the code below should not change the output (it does).


<lang rust>// rustc -V
<lang rust>// rustc 1.26.0 or later
// rustc 1.2.0-nightly (0cc99f9cc 2015-05-17) (built 2015-05-18)


use std::ops::Add;
use std::ops::Add;


fn foo<Num>(n: Num) -> Box<FnMut(Num) -> Num>
fn foo<Num>(n: Num) -> impl FnMut(Num) -> Num
where Num: Add<Output=Num> + Copy + 'static {
where Num: Add<Output=Num> + Copy + 'static {
let mut acc = n;
let mut acc = n;
Box::new(move |i: Num| {
move |i: Num| {
acc = acc + i;
acc = acc + i;
acc
acc
})
}
}
}