There is beauty in all forms of intentional design. Programming languages are no exception.

Presented below is a collection of anonymous functions from various programming languages. Some languages provide the ability to form both closures, anonymous functions which save the context they were created in; and lambdas, anonymous functions which run under the context they are called from. Languages which provide both facilities are noted as seperate entries. Otherwise, it is glossed over. Seperate implementations will be kept on seperate lines.

Every function here is semantically identical: take two numbers and add them together. Such a simple function was chosen to place the focus on the function’s structure on the page over the ease/hassle of writing it. Once the function is created, we call it with the two arguments 8 and 7. 8 was chosen as it is my favorite number. 7 is lucky. Their sum, 15, is undisputable.

This menagerie is not in any specific order, though care was taken to keep similar languages clumped together. The names of the languages are hidden to encourage you to ponder the code sample before revealing what language the code is from. A rose by any other name may be sweeter than you thought.













This space was intentionally left blank.











Hover to reveal.

Dyalog APL

8 {⍺+⍵} 7
Hover to reveal.

J

8 (+) 7
Hover to reveal.

Idris

(\ x y => x + y ) 8 7
(+) 8 7
Hover to reveal.

Coq

(fun x y => x + y) 8 7
(fun (x y : Int) => x + y) 8 7
Hover to reveal.

Haskell

(\x y -> x + y) 8 7
(+) 8 7
Hover to reveal.

OCaml

(fun x y -> x + y) 8 7
Hover to reveal.

Emacs Lisp (Closure)

(let ((lexical-binding t)) ((lambda (x y) (+ x y)) 8 7))
Hover to reveal.

Emacs Lisp (Lambda)

(let ((x 8) (y 7)) ((lambda () (+ x y))))
Hover to reveal.

Joy

8 7 [+] i.
Hover to reveal.

ATS

(lam (x : int) (y : int) => x + y) 3 4
Hover to reveal.

Scala

(( _ :Int ) + ( _ :Int ))(8, 7)
((x:Int, y:Int) => x + y)(8, 7)
Hover to reveal.

Java 8

((int x, int y) ->  x + y;)(8, 7);
Hover to reveal.

C++ (Lambda)

([](auto x, auto y) -> auto { return x + y; })(8, 7);
([](int x, int y) { return x + y; })(8, 7);
Hover to reveal.

C++ (Closure)

int x = 8; int y = 7; ([=]() { return x + y; })();
Hover to reveal.

Rust

(|x, y| { x + y })(8, 7)
Hover to reveal.

Raku

{ $^x + $^y }(8, 7);
( * + * )(8, 7);
(sub ($x,$y) { return $x + $y })(8,7)
Hover to reveal.

Javascript

((x, y) => x + y)(8, 7);
(function(x, y) { return x + y })(8, 7)
Hover to reveal.

Crystal

(->(x, y){ x + y }).call(8, 7)
(->(x : Int32, y : Int32){ x + y }).call(8, 7)
(Proc(Int32, Int32, Int32).new { |x, y| x + y}).call(8, 7)
Hover to reveal.

Nim

(proc (x: int, y: int): int = x + y)(8, 7)
(proc (x: int, y: int) = x + y)(8, 7)
import sugar; ((x: int, y: int) => x + y)(8, 7)
Hover to reveal.

Python

(lambda x, y: x + y)(8, 7)
Hover to reveal.

Erlang

(fun(X, Y) -> X + Y end)(8, 7).
Hover to reveal.

Ruby (Lambda)

->(x,y){ x + y }[8, 7]
(Proc.new { |x, y| x + y })[8, 7]
(Kernel.lambda { |x, y| x + y })[8, 7]
Hover to reveal.

Ruby (Closure)

method(def _(x, y) x + y end)[8, 7] # not a true anonymous function, yet worth including
Hover to reveal.

Elixir

(fn (x, y) -> x + y end).(8, 7)
(&(&1 + &2)).(8, 7)

This is by no means an exhaustive list, so if you have any comments or additions, please email me below.


Special thanks to https://github.com/johnli0135/ for providing a nonnegligible chunk of these examples.