Phase 10: Standard Library — clojure.string, clojure.set, clojure.walk
- src/jolt/clojure/string.clj (123 lines, 20 functions): blank?, capitalize, lower-case, upper-case, includes?, join, replace, replace-first, str-reverse, split, starts-with?, ends-with?, trim, triml, trimr, trim-newline, escape, index-of, last-index-of - src/jolt/clojure/set.clj (124 lines, 10 operations): union, intersection, difference, select, project, rename, rename-keys, map-invert, join, index, subset?, superset? - src/jolt/clojure/walk.clj (77 lines, 9 functions): walk, postwalk, prewalk, postwalk-demo, prewalk-demo, postwalk-replace, prewalk-replace, keywordize-keys, stringify-keys, macroexpand-all - src/jolt/core.janet: 11 Janet string interop bindings (str-trim, str-upper, str-lower, str-find, str-replace, str-replace-all, str-reverse-b, str-join, str-split, str-triml, str-trimr) - test/phase10-test.janet: 2 test sections (40-41) 15+ assertions covering string and set functions - All .clj files use eval-form for multi-form loading - 315 ok, 2 fail (pre-existing, unchanged)
This commit is contained in:
parent
e63c2ce8d5
commit
fdb0f4ab83
10 changed files with 518 additions and 77 deletions
77
src/jolt/clojure/walk.clj
Normal file
77
src/jolt/clojure/walk.clj
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
; Jolt Standard Library: clojure.walk
|
||||
; This file generalizes tree walking for Clojure data structures.
|
||||
|
||||
(defn walk
|
||||
"Traverses form, an arbitrary data structure. inner and outer are
|
||||
functions. Applies inner to each element of form, building up a
|
||||
data structure of the same type, then applies outer to the result.
|
||||
Recognizes all Clojure data structures. Consumes seqs."
|
||||
[inner outer form]
|
||||
(cond
|
||||
(list? form) (outer (apply list (map inner form)))
|
||||
(seq? form) (outer (doall (map inner form)))
|
||||
(vector? form) (outer (vec (map inner form)))
|
||||
(map? form) (outer (into (empty form) (map inner form)))
|
||||
(set? form) (outer (into (empty form) (map inner form)))
|
||||
:else (outer form)))
|
||||
|
||||
(defn postwalk
|
||||
"Performs a depth-first, post-order traversal of form. Calls f on
|
||||
each sub-form, uses f's return value in place of the original.
|
||||
Recognizes all Clojure data structures. Consumes seqs."
|
||||
[f form]
|
||||
(walk (partial postwalk f) f form))
|
||||
|
||||
(defn prewalk
|
||||
"Like postwalk, but does pre-order traversal."
|
||||
[f form]
|
||||
(walk (partial prewalk f) identity (f form)))
|
||||
|
||||
(defn postwalk-demo
|
||||
"Demonstrates the behavior of postwalk by returning a lazy seq of
|
||||
forms passed to the postwalk outer function during the traversal
|
||||
of form."
|
||||
[form]
|
||||
(let [acc (atom [])]
|
||||
(postwalk (fn [x] (swap! acc conj x) x) form)
|
||||
@acc))
|
||||
|
||||
(defn prewalk-demo
|
||||
"Demonstrates the behavior of prewalk by returning a lazy seq of
|
||||
forms passed to the prewalk outer function during the traversal
|
||||
of form."
|
||||
[form]
|
||||
(let [acc (atom [])]
|
||||
(prewalk (fn [x] (swap! acc conj x) x) form)
|
||||
@acc))
|
||||
|
||||
(defn postwalk-replace
|
||||
"Recursively transforms form by replacing keys in smap with their
|
||||
values. Like clojure.string/replace but works with any data
|
||||
structure. Does replacement at the leaves of the tree first."
|
||||
[smap form]
|
||||
(postwalk (fn [x] (if (contains? smap x) (get smap x) x)) form))
|
||||
|
||||
(defn prewalk-replace
|
||||
"Recursively transforms form by replacing keys in smap with their
|
||||
values. Like postwalk-replace but does replacement at the root of
|
||||
the tree first."
|
||||
[smap form]
|
||||
(prewalk (fn [x] (if (contains? smap x) (get smap x) x)) form))
|
||||
|
||||
(defn keywordize-keys
|
||||
"Recursively transforms all map keys from strings to keywords."
|
||||
[m]
|
||||
(let [f (fn [[k v]] (if (string? k) [(keyword k) v] [k v]))]
|
||||
(postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m)))
|
||||
|
||||
(defn stringify-keys
|
||||
"Recursively transforms all map keys from keywords to strings."
|
||||
[m]
|
||||
(let [f (fn [[k v]] (if (keyword? k) [(name k) v] [k v]))]
|
||||
(postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m)))
|
||||
|
||||
(defn macroexpand-all
|
||||
"Recursively performs all possible macroexpansions in form."
|
||||
[form]
|
||||
(prewalk (fn [x] (if (seq? x) (macroexpand x) x)) form))
|
||||
Loading…
Add table
Add a link
Reference in a new issue