Skip to content

RFC: Partial application sugar #5893

Closed
Closed
@Kimundi

Description

@Kimundi

At the moment, if you want to partially apply a function the only way is to write a closure:

let part = |x, z| foo(x, 42, z);

That isn't to bad, but still more verbose than necessary, so I propose a special sugar for this usage:

let part = foo(_, 42, _);

The rules are simple:

  • For the first _ in an expression, transform the whole expression into a closure by putting an |...| at the front and replacing the _ with the first argument in there.
  • For each following _ in a expression, append an argument to the argument list, and replace the _ with that, too.

This would also allow things like v.map(_ + 5).filter(3 < _) to work, which read really nice.

Things would get a bit hairy if you involve if or match, but each arm would just need the rule to have the same number of _s. And it would be unidiomatic to use it for bigger code structures anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions