Functions are the main building block of a Phel program. This page shows how to define them with defn and fn, give one function several arities, accept any number of arguments, loop with recur, and choose an implementation at run time with multimethods.
Global functions
(defn name docstring? attributes? [params*] expr*)
defn defines a global function. It returns the value of its last expression:
(defn add
"Adds a and b."
[a b]
(+ a b))
(add 1 2) ; => 3
The docstring is optional. The REPL shows it with (doc add).
Multiple arities and variadics
A function can have one clause per argument count. The call picks the clause that matches:
(defn greet
([] "hi")
([name] (str "hi " name))
([greeting name] (str greeting " " name)))
(greet) ; => "hi"
(greet "Ada") ; => "hi Ada"
(greet "hello" "Ada") ; => "hello Ada"
& collects the remaining arguments into a sequence:
(defn sum-all [x & more]
(apply + x more))
(sum-all 1 2 3) ; => 6
Only one clause can be variadic, and it must have the most parameters. Calling a defn with an argument count it does not accept fails at compile time with PHEL002.
Parameters can take vectors and maps apart by shape, as in (defn area [{:keys [width height]}] ...). See Destructuring.
Private functions
defn- defines a function that other namespaces cannot use. It is the same as adding {:private true} as the attribute map:
(defn- helper [x] (* x 2))
(defn helper
{:private true}
[x]
(* x 2))Anonymous function (fn)
fn creates a function without a global name. Pass it to another function, or return it:
(map (fn [x] (* x 2)) [1 2 3]) ; => (2 4 6)
(defn make-adder [n]
(fn [x] (+ x n)))
((make-adder 10) 5) ; => 15
fn supports the same arities and & as defn. The function closes over the locals around it, like n above.
#(...) is a shorter form for small functions. % (or %1) is the first argument, %2 the second, and %& the rest:
(map #(* % 2) [1 2 3]) ; => (2 4 6)
(#(+ %1 %2) 1 2) ; => 3
(#(apply + %&) 1 2 3) ; => 6
Use fn when the body is more than one short expression or when a name for the argument helps the reader.
PHP Coming from PHP?
Unlike PHP closures, a Phel fn captures every local it uses without a use (...) clause.
Apply and compose
apply calls a function with the elements of a collection as its arguments. The last argument must be a collection, a string, or nil:
(apply + [1 2 3]) ; => 6
(apply + 1 2 [3]) ; => 6
partial fixes the first arguments. comp chains functions from right to left:
((partial + 10) 5) ; => 15
((comp inc #(* % 2)) 5) ; => 11, same as (inc (* 5 2))
The core API lists more helpers such as juxt, complement, and constantly.
Recursion
A function can call itself. Each call adds a PHP stack frame, so deep recursion can hit PHP's nesting limit.
recur jumps back to the start of the function, or of the closest loop, with new argument values. It compiles to a PHP while loop, so it runs in constant stack space. recur must be the last thing the function does:
(defn factorial [n]
(loop [acc 1
n n]
(if (<= n 1)
acc
(recur (* acc n) (dec n)))))
(factorial 5) ; => 120
(defn countdown [n]
(if (<= n 0)
:done
(recur (dec n))))
(countdown 100000) ; => :done
The loop form itself is covered in Control flow. For most collection work, reduce, map, and filter are shorter than explicit recursion.
Multimethods
A multimethod picks an implementation from the result of a dispatch function. defmulti sets the dispatch function. defmethod adds one implementation per dispatch value, and :default catches the rest:
(defmulti area :shape)
(defmethod area :rectangle [{w :width h :height}]
(* w h))
(defmethod area :circle [{r :radius}]
(* 3 r r))
(defmethod area :default [shape]
(throw (InvalidArgumentException. "Unknown shape")))
(area {:shape :rectangle :width 4 :height 3}) ; => 12
(area {:shape :circle :radius 2}) ; => 12
The dispatch function can be any function, such as #(get % :language) or (fn [x] (type x)). Other namespaces can add methods later without changing the original code. Dispatch on type hierarchies: Interfaces.
Defn metadata shortcuts
Metadata on a defn can wrap the body:
(defn ^:memoize fib [n]
(if (< n 2) n (+ (fib (dec n)) (fib (- n 2)))))
(defn ^{:memoize-lru 128} lookup [k]
(slow-lookup k))
^:memoize caches every result forever. ^{:memoize-lru N} keeps the N most recent results. They desugar to memoize and memoize-lru, and recursive self-calls also use the cache. ^:async is covered in Async.
Return and parameter types (:tag)
Type hints become PHP type declarations, and the compiler checks them:
(defn ^int add-ints [^int a ^int b] (+ a b))
(defn greet-name ^{:tag "?string"} [^string name]
(when (seq name) (str "hi " name)))
Write a hint as ^int, ^"?int", ^"\\Foo\\Bar", or ^{:tag "..."}. The compiler also infers types for single-arity functions and reports mismatches at compile time.
To pass a PHP array by reference (&$arr), see PHP interop.
Try what this page covers in Names and Functions, then Recursion and Loops.