#closures #syntax #scala #lambda #macro #placeholder-syntax #λ

macro placeholder_closure

Scala's Placeholder Syntax for Rust

1 unstable release

0.1.0 Jun 19, 2021

#13 in #scala

CC0 license

7KB

placeholder_closure

CI Crates.io Crates.io

Scala's Placeholder Syntax for Rust.

use placeholder_closure::λ;

let xs = xs.into_iter().map(λ!($ + 1)).collect::<Vec<_>>();

fn dot<F: FnOnce(Y) -> Z, G: FnOnce(X) -> Y, X, Y, Z>(f: F, g: G) -> impl FnOnce(X) -> Z {
    λ!(move { f(g($)) })
}

Usage

See the documentation on Docs.rs.

License

Licensed under CC0-1.0.


lib.rs:

Scala's Placeholder Syntax for Rust.

use placeholder_closure::λ;

let xs = xs.into_iter().map(λ!($ + 1)).collect::<Vec<_>>();

How it works

The λ! (or lambda!) macro replaces 0 or more $ characters with closure arguments.

let f = λ!($ + 1);

will be:

let f = |__0| __0 + 1;

Constructing move closures

You can also construct move closures.

fn dot<F: FnOnce(Y) -> Z, G: FnOnce(X) -> Y, X, Y, Z>(f: F, g: G) -> impl FnOnce(X) -> Z {
    λ!(move { f(g($)) })
}

Dependencies

~1.5MB
~35K SLoC