Fancy App:   Scala-Style Magic Lambdas
#%app
7.7

Fancy App: Scala-Style Magic Lambdas

Sam Tobin-Hochstadt <samth@ccs.neu.edu>

This package provides a simple shorthand for defining anonymous functions. When required, writing (+ _ 1) becomes equivalent to writing (λ (v) (+ v 1)).

syntax

(#%app expr ...)

Equivalent to normal function application as in racket/base unless any expr is a _. In that case, the expression is transformed into an anonymous function expression with one argument for each _ found among the given exprs. Uses of _ are converted to positional arguments from left to right.

Examples:
> (map (+ 1 _) (list 1 2 3))

'(2 3 4)

> (map (- _ 1) (list 1 2 3))

'(0 1 2)

> (map (- _ _) (list 10 20 30) (list 1 2 3))

'(9 18 27)

Note that a use of _ that is nested within one of the given exprs does not count. Additionally, using _ after a keyword is not treated specially and does not trigger creation of a keyword argument. Rest arguments are not supported.

Examples:
> (+ 1 (+ _ 2))

+: contract violation

  expected: number?

  given: #<procedure:...ncy-app/main.rkt:28:27>

  argument position: 2nd

  other arguments...:

   1

> (define (add/kw a #:to b) (+ a b))
> (define add2 (add/kw 2 #:to _))
> (add2 10)

12

> (add2 #:to 10)

application: procedure does not accept keyword arguments

  procedure: add2

  arguments...:

   #:to 10