Skip to content

Commit 9fdf4f4

Browse files
committed
blog: 2025-08-24-threading-macros
1 parent f23513a commit 9fdf4f4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
+++
2+
title = "Thread-first (->) vs Thread-last (->>)"
3+
+++
4+
5+
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

Comments
 (0)