#How to do anonymous functions

5 messages · Page 1 of 1 (latest)

spiral yew
#

I've been trying to do currying as in functional programming languages in Rust but I can't get it to work. By currying i mean taking only one argument each function and returning an anonymous function. But my problem is that I don't know how to use a values outside of the anonymous function inside it. For example if i wanted to do that, how could I do it?

fun sum (a : f32) -> λ(f32) -> f32 {
    λ(b : f32){
        return a + b;
    }
}
dapper bane
#

?eval

fn sum(a: f32) -> impl Fn(f32) -> f32 {
  move |b| a + b
}

sum(3.)(5.)
late oasisBOT
#
     Running `target/debug/playground`

8.0
dapper bane
#

This is an impl Trait return type, which you can think of as "I return some specific concrete type, but I'm not telling you which one"

#

The reason I'm using it is that Rust closures all have a unique type, different from that of every other closure (even if the signature is the same), and unnameable (you can't write the actual type)