Skip to content

Commit

Permalink
Add as-> macro (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwrooijen authored Dec 11, 2020
1 parent 693713f commit 562bca1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
14 changes: 1 addition & 13 deletions src/sci/impl/analyzer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
(def special-syms '#{try finally do if new recur quote catch throw def . var set!})

;; Built-in macros.
(def macros '#{do if and or as-> let fn fn* def defn
(def macros '#{do if and or let fn fn* def defn
comment loop lazy-seq for doseq case try defmacro
declare expand-dot* expand-constructor new . import in-ns ns var
set! resolve macroexpand-1 macroexpand})
Expand Down Expand Up @@ -290,16 +290,6 @@
(let [let-bindings (destructure let-bindings)]
(expand-let* ctx let-bindings exprs)))

(defn expand-as->
"The ->> macro from clojure.core."
[ctx [_as expr nm & forms]]
(let [[let-bindings & body] `([~nm ~expr
~@(interleave (repeat nm) (butlast forms))]
~(if (empty? forms)
nm
(last forms)))]
(expand-let* ctx let-bindings body)))

(declare expand-declare)

(defn expand-def
Expand Down Expand Up @@ -684,8 +674,6 @@
(defn defmacro) (let [ret (expand-defn ctx expr)]
ret)
;; TODO: implement as normal macro in namespaces.cljc
as-> (expand-as-> ctx expr)
;; TODO: implement as normal macro in namespaces.cljc
comment (expand-comment ctx expr)
loop (expand-loop ctx expr)
lazy-seq (expand-lazy-seq ctx expr)
Expand Down
8 changes: 8 additions & 0 deletions src/sci/impl/namespaces.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@
(recur threaded (next forms)))
x)))

(defn as->*
[_ _ expr name & forms]
`(let [~name ~expr
~@(interleave (repeat name) (butlast forms))]
~(if (empty? forms)
name
(last forms))))
(defn dotimes*
[_ _ bindings & body]
(assert (vector? bindings))
Expand Down Expand Up @@ -721,6 +728,7 @@
'== (copy-core-var ==)
'-> (macrofy ->*)
'->> (macrofy ->>*)
'as-> (macrofy as->*)
'add-watch (copy-core-var add-watch)
'remove-watch (copy-core-var remove-watch)
'aget (copy-core-var aget)
Expand Down

0 comments on commit 562bca1

Please sign in to comment.