Skip to content

Commit

Permalink
add thread-first thread-last thread-as aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Jan 17, 2025
1 parent cfb549c commit 84454e1
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 39 deletions.
57 changes: 29 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cirru_edn = "0.6.13"
cirru_parser = "0.1.31"
# cirru_parser = { path = "/Users/chenyong/repo/cirru/parser.rs" }
argh = "0.1.13"
dirs = "5.0.1"
dirs = "6.0.0"
notify = "7.0.0"
notify-debouncer-mini = "0.5.0"
walkdir = "2.5.0"
Expand Down
12 changes: 12 additions & 0 deletions calcit/test-macro.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@
macroexpand $ quote
-> a (b c) (d e f)
quote $ d (b a c) e f
assert=
macroexpand $ quote
thread-first a (b c) (d e f)
quote $ -> a (b c) (d e f)
assert=
macroexpand-all $ quote
<- (b c) (d e f) a
Expand All @@ -260,6 +264,10 @@
macroexpand $ quote
->> a (b c) (d e f)
quote $ d e f (b c a)
assert=
macroexpand $ quote
thread-last a (b c) (d e f)
quote $ ->> a (b c) (d e f)
assert=
macroexpand $ quote (->% a)
quote a
Expand All @@ -270,6 +278,10 @@
% a
% $ + % 1
* % 2
assert=
macroexpand $ quote
thread-as a (+ % 1) (* % 2)
quote $ ->% a (+ % 1) (* % 2)
assert= 35 $ ->% 3 (+ % 4) (* % 5)
assert= 36 $ ->% 3 (+ % %) (* % %)
assert= 18 $ %<- (+ % %) (* % %) 3
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"version": "0.9.7",
"main": "./lib/calcit.procs.mjs",
"devDependencies": {
"@types/node": "^22.10.5",
"typescript": "^5.7.2"
"@types/node": "^22.10.7",
"typescript": "^5.7.3"
},
"scripts": {
"compile": "rm -rfv lib/* && tsc",
Expand Down
12 changes: 12 additions & 0 deletions src/cirru/calcit-core.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,10 @@
&list:rest x0
, & $ &list:rest xs
recur ([] x0 base) & $ &list:rest xs
|thread-first $ %{} :CodeEntry (:doc "|a alias for `->`")
:code $ quote
defmacro thread-first (& xs)
quasiquote $ -> ~@xs
|->% $ %{} :CodeEntry (:doc "|pass value as `%` into several expressions")
:code $ quote
defmacro ->% (base & xs)
Expand All @@ -501,6 +505,10 @@
map (butlast xs)
defn %->% (x) ([] '% x)
quasiquote $ let ~pairs ~tail
|thread-as $ %{} :CodeEntry (:doc "|a alias for `->%`")
:code $ quote
defmacro thread-as (& xs)
quasiquote $ ->% ~@xs
|->> $ %{} :CodeEntry (:doc "|thread macro passing value at end of each expression")
:code $ quote
defmacro ->> (base & xs)
Expand All @@ -510,6 +518,10 @@
if (list? x0)
&call-spread recur (append x0 base) & $ &list:rest xs
&call-spread recur ([] x0 base) & $ &list:rest xs
|thread-last $ %{} :CodeEntry (:doc "|a alias for `->>`")
:code $ quote
defmacro thread-last (& xs)
quasiquote $ ->> ~@xs
|/ $ %{} :CodeEntry (:doc |dividing)
:code $ quote
defn / (x & ys)
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 84454e1

Please sign in to comment.