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.











Dyalog APL

8 {⍺+⍵} 7

J

8 (+) 7

Idris

(\ x y => x + y ) 8 7
(+) 8 7

Coq

(fun x y => x + y) 8 7
(fun (x y : Int) => x + y) 8 7

Haskell

(\x y -> x + y) 8 7
(+) 8 7

OCaml

(fun x y -> x + y) 8 7

Emacs Lisp (Closure)

(let ((lexical-binding t)) ((lambda (x y) (+ x y)) 8 7))

Emacs Lisp (Lambda)

(let ((x 8) (y 7)) ((lambda () (+ x y))))

Joy

8 7 [+] i.

ATS

(lam (x : int) (y : int) => x + y) 3 4

Scala

(( _ :Int ) + ( _ :Int ))(8, 7)
((x:Int, y:Int) => x + y)(8, 7)

Java 8

((int x, int y) ->  x + y;)(8, 7);

C++ (Lambda)

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

C++ (Closure)

int x = 8; int y = 7; ([=]() { return x + y; })();

Rust

(|x, y| { x + y })(8, 7)

Perl 6

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

Javascript

((x, y) => x + y)(8, 7);
(function(x, y) { return x + y })(8, 7)

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)

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)

Python

(lambda x, y: x + y)(8, 7)

Erlang

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

Ruby (Lambda)

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

Ruby (Closure)

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

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.