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
Phel includes two handy threading macros that let you express a sequence of transformations in a linear, readable style.
6
+
7
+
*`->` (**thread-first**): inserts the previous value as the **first argument** of the next form.
8
+
*`->>` (**thread-last**): inserts the previous value as the **last argument** of the next form.
9
+
10
+
## Thread-first `->`
11
+
12
+
Use `->` when the function you call expects the data as its first argument.
13
+
14
+
```phel
15
+
(-> 5
16
+
(+ 3) # becomes (+ 5 3)
17
+
(* 2)) # becomes (* 8 2)
18
+
=> 16
19
+
```
20
+
21
+
## Thread-last `->>`
22
+
23
+
Use `->>` when the function expects the data as its last argument. This is common with functions that operate on sequences such as `map`, `filter` or `reduce`.
24
+
25
+
```phel
26
+
(->> [1 2 3 4]
27
+
(map inc) # becomes (map inc [1 2 3 4])
28
+
(filter odd?) # becomes (filter odd? [2 3 4 5])
29
+
(reduce +)) # becomes (reduce + [3 5])
30
+
=> 8
31
+
```
32
+
33
+
## When to choose which
34
+
35
+
Pick the macro that matches the position of the data argument in the next form:
36
+
37
+
* Use `->` for functions like `inc`, `assoc` or your own functions where the data comes first.
38
+
* Use `->>` for collection-processing functions where the data comes last.
39
+
40
+
Knowing the difference keeps your pipelines clear and avoids confusing argument order.
0 commit comments