Anonymous functions syntax cheatsheet
Here’s the syntax to define and call anonymous functions (lambdas) in a variety of programming languages (which I’m more or less interested in).
- Elixir
- Erlang
- Julia
- Swift
- Ruby
- CoffeeScript
- JavaScript
- Dart
- Go
- C#
- C++11
- Java 8
- Groovy
- Haskell
- Python
- Lisp (Scheme)
- Clojure
- Lua
- Mathematica / Wolfram Language
- R
- Scala
- Smalltalk
- Matlab / GNU Octave
- Perl 5
- Perl 6
- Magik
- OCaml
- HP PPL CAS (GIAC/XCAS)
- PHP (>=5.3)
- TCL
- Maxima
- Clang block extension (C / C++ / Objective-C)
- Visual Basic.NET
- Factor
- RPL
Elixir
Assign anonymous function to f
defined
as the sum of its two arguments:
f = fn x, y -> x + y end
Call the function assigned to f
with arguments 1
and 2
:
f.(1, 2)
Shortcut notation:
f = &(&1 + &2)
Erlang
Definition:
F = fun(X, Y) -> X + Y end
Use (invocation):
F(1, 2)
Julia
Definition:
f = (x, y) -> x + y
Multiline block(x,y)
definition:
f = (x, y) -> begin
block(x,y)
end
Invocation:
f(1, 2)
Single argument case (parentheses can be omitted):
x -> expr(x)
Block syntax: a closure first argument
(of some function receiver
) can be passed
outside the parenthesis with do notation:
receiver() do x, y
block(x,y)
end
named function definition:
f(x,y) = x + y
function f(x,y)
x + y
end
Swift
let f = { (x: Int, y: Int) -> Int in x + y }
let f = { (x: Int, y: Int) -> Int in x + y }
Implicit return type:
{ (x: Int, y: Int) in x + y }
Shorthand:
let f:(Int, Int) -> Int = { $0 + $1 }
Trailing syntax: a closure last argument ca be passed outside the parentheses: (here the parentheses could be ommitted since there aren’t any other arguments)
receiver() {
block($0,$1)
}
Or, with explicit arguments:
receiver {
(x: Int, y: Int) -> Int in
block(x,y)
}
Ruby
Assignment, with several alternative syntaxes:
f = ->(x, y){ x + y }
f = lambda{ |x,y| x + y }
f = proc{ |x,y| x + y }
f = lambda do |x,y| x + y end
f = proc do |x,y| x + y end
Invocation, with alternative syntaxes:
f[1,2]
f.call(1,2)
f.(1,2)
Passing as a block to a receiver
method with do notation:
receiver do |x,y|
block(x,y)
end
With curly braces:
receiver { |x,y|
block(x,y)
}
Passing an anonymous function as a block:
receiver &f
CoffeeScript
Definition, single line:
f = (x, y) -> x + y
Definition, indented block:
f = (x, y) ->
block(x,y)
Invocation:
f(1, 2)
JavaScript
Definition:
f = function(x, y) { return x + y }
Invocation:
f(1, 2)
Dart
Definition:
f = (x, y) => x + y
Invocation:
f(1, 2)
Go
Definition:
f := func(x, y int) int { return x + y }
Invocation:
f(1, 2)
C#
Definition:
delegate int f_type(int x, int y);
f_type f = (x, y) => x + y;
Definition with multiline block:
delegate int f_type(int x, int y);
f_type f = (x, y) => { block(x,y); }
Invocation:
f(1, 2)
C++11
Definition:
auto f = [=] (intx, int y) -> int { return x + y; };
Definition with implicit return type
auto f = [=] (intx, int y) { return x + y; };
Invocation:
f(1, 2)
Java 8
Definition:
interface Ftype {
public Integer call(Integer x, Integer y);
}
Ftype f = (x, y) -> x + y;
Invocation:
f.call(1, 2)
Groovy
Definition:
def f = { x ,y -> x + y }
Invocation:
f(1, 2)
Haskell
Definition:
f = \x y -> x + y
Invocation:
f 1, 2
Python
Definition:
f = lambda x, y: x + y
Invocation:
f(1, 2)
Lisp (Scheme)
Definition:
(define f (lambda (x y) (+ x y)))
Invocation:
(f 1 2)
Clojure
Definition:
(def f (fn [x y] (+ x y)))
Invocation:
(f 1 2)
Lua
Definition:
f = function(x,y) return x + y end
Invocation:
f(1, 2)
Mathematica / Wolfram Language
Definition:
f = Function[{x, y}, x + y]
Shortcut:
f = (#1 + #2)&
Invocation:
f[1, 2]
R
Definition:
f <- function(x, y) x + y
Invocation:
f(1, 2)
Scala
Definition:
val f = (x: Integer, y: Integer) => x + y
Invocation:
f(1, 2)
Smalltalk
Definition:
f := [ :x :y | x + y ].
Invocation:
f value: 1 value: 2
Matlab / GNU Octave
Definition:
f = @(x,y) x + y
Invocation:
f(1, 2)
Perl 5
Definition:
my $f = sub { my $x = shift; my $ y = shift; return x + y; };
Invocation:
$f->(1, 2)
Perl 6
Definition:
my $f = -> $x, $y { x + y };
Shortcut (twigil parameters)
my $f = { $^x + $^y };
Invocation:
$f->(1, 2)
Magik
Definition:
f << _proc(x, y)
_return x + y
_endproc #_
Invocation:
f(1, 2)
Alternative invocation:
f.invoke(1, 2)
OCaml
Definition:
let f x y = x + y;;
Invocation:
f 1 2
HP PPL CAS (GIAC/XCAS)
Definition: (entered form)
f(x,y) := x + y
Definition: (alternative)
f := (x,y) -> x + y
Definition: (shown / edition form)
f := (x,y) -> (x + y)
Definition: (block form as entered)
f(x,y) := begin block(x,y) end
Definition: (block form as shown)
f := (x,y)->[block(x,y)]
Definition: (block form as edited)
f := (x,y)->BEGIN block(x,y); END;
For a single argument, parentheses can be ommitted:
f := x -> expr(x)
Invocation:
f(1, 2)
PHP (>=5.3)
Definition:
$f = function($x, $y) { return $x+$y; };
Invocation:
$f(1, 2)
TCL
Definition:
set f { {x y} {expr {($x+$y)} } }
Invocation:
apply $f 1 2
Maxima
Definition:
f: lambda([x, y], x + y);
Invocation:
f(1, 2)
Clang block extension (C / C++ / Objective-C)
Definition (note: f_lambda is an arbitrary identifier):
int (*f)(int x, int y) = ({ int f_lambda(int x, int y) { return x + y; } &f_lambda; }); //*
Invocation:
(*f)(1, 2) //*
Visual Basic.NET
Definition
Dim f = Function(x,y) x + y
Definition (multiline block)
Dim f = Function(x,y)
block(x,y)
End Function
Invocation:
f(1, 2)
Factor
Definition
CONSTANT: f [ + ]
Invocation:
1 2 f call
RPL
Definition
\<< + \>> 'F' STO
Invocation
1 2 F
If F
is a local variable
1 2 F EVAL
Alternative invocation forms:
1 2 'F' EVAL
'F(1,2)' EVAL
Definition using local variables:
\<< \-> X Y 'X+Y' \>> 'F' STO
Definition with RPL block:
\<< \-> X Y \<< block(X,Y) \>> \>> 'F' STO
Algebraic definition:
'F(X,Y)=X+Y' DEFINE