1
0
mirror of https://github.com/janet-lang/janet synced 2024-09-28 07:08:14 +00:00

Add clojure style threading macros.

This commit is contained in:
Calvin Rose 2018-03-12 12:47:05 -04:00
parent e393e3dda0
commit 61645c82b1

View File

@ -137,7 +137,8 @@ If no match is found, returns nil"
{:more more :next (fn [] (f (next)))})
(defn reduce [f start s]
(def {:more more :next next} (seq s))
(def s (seq s))
(def {:more more :next next} s)
(if (more)
(reduce f (f start (next)) s)
start))
@ -209,6 +210,28 @@ If no match is found, returns nil"
newenv)
(put _env '_env nil)
(defmacro ->
[x & forms]
(defn fop [last nextform]
(def n (ast-unwrap1 nextform))
(def [h t] (if (= :tuple (type n))
[(get n 0) (array-slice n 1)]
[n []]))
(def parts (array-concat [h last] t))
(apply tuple parts))
(reduce fop x forms))
(defmacro ->>
[x & forms]
(defn fop [last nextform]
(def n (ast-unwrap1 nextform))
(def [h t] (if (= :tuple (type n))
[(get n 0) (array-slice n 1)]
[n []]))
(def parts (array-concat [h] t last))
(apply tuple parts))
(reduce fop x forms))
(def run-context
"Run a context. This evaluates expressions of dst in an environment,
and is encapsulates the parsing, compilation, and evaluation of dst.