You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/documentation/functions-and-recursion.md
+25-2Lines changed: 25 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,11 +7,18 @@ weight = 7
7
7
8
8
```phel
9
9
(fn [params*] expr*)
10
+
11
+
(fn
12
+
([params1*] expr1*)
13
+
([params2*] expr2*)
14
+
...)
10
15
```
11
16
12
17
Defines a function. A function consists of a list of parameters and a list of expression. The value of the last expression is returned as the result of the function. All other expression are only evaluated for side effects. If no expression is given, the function returns `nil`.
13
18
14
-
Function also introduce a new lexical scope that is not accessible outside the function.
19
+
Functions can define multiple arities. When calling such a function, the clause matching the number of provided arguments is chosen. Variadic clauses are supported for at most one arity and must be the one with the most parameters. If no arity matches, a readable compile-time or runtime error is thrown.
20
+
21
+
Function also introduces a new lexical scope that is not accessible outside the function.
15
22
16
23
```phel
17
24
(fn []) # Function with no arguments that returns nil
@@ -24,7 +31,13 @@ Function can also be defined as variadic function with an infinite amount of arg
24
31
25
32
```phel
26
33
(fn [& args] (count args)) # A variadic function that counts the arguments
34
+
27
35
(fn [a b c &]) # A variadic function with extra arguments ignored
36
+
37
+
(fn # A multi-arity function
38
+
([] "hi")
39
+
([name] (str "hi " name))
40
+
([greeting name & rest] (str greeting " " name rest)))
28
41
```
29
42
30
43
There is a shorter form to define an anonymous function. This omits the parameter list and names parameters based on their position.
@@ -44,13 +57,23 @@ There is a shorter form to define an anonymous function. This omits the paramete
44
57
45
58
```phel
46
59
(defn name docstring? attributes? [params*] expr*)
60
+
61
+
(defn name docstring? attributes?
62
+
([params1*] expr1*)
63
+
([params2*] expr2*)
64
+
...)
47
65
```
48
66
49
-
Global functions can be defined using `defn`.
67
+
Global functions can be defined using `defn`. Like anonymous functions, they may provide multiple arities. The most specific clause based on the number of arguments is chosen at call time. A single variadic clause is allowed and must declare the maximum number of arguments.
50
68
51
69
```phel
52
70
(defn my-add-function [a b]
53
71
(+ a b))
72
+
73
+
(defn greet
74
+
([] "hi")
75
+
([name] (str "hi " name))
76
+
([greeting name] (str greeting " " name)))
54
77
```
55
78
56
79
Each global function can take an optional doc comment and attribute map.
0 commit comments