1
0
mirror of https://github.com/janet-lang/janet synced 2025-03-16 05:18:10 +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)))}) {:more more :next (fn [] (f (next)))})
(defn reduce [f start s] (defn reduce [f start s]
(def {:more more :next next} (seq s)) (def s (seq s))
(def {:more more :next next} s)
(if (more) (if (more)
(reduce f (f start (next)) s) (reduce f (f start (next)) s)
start)) start))
@ -209,6 +210,28 @@ If no match is found, returns nil"
newenv) newenv)
(put _env '_env nil) (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 (def run-context
"Run a context. This evaluates expressions of dst in an environment, "Run a context. This evaluates expressions of dst in an environment,
and is encapsulates the parsing, compilation, and evaluation of dst. and is encapsulates the parsing, compilation, and evaluation of dst.