File tree Expand file tree Collapse file tree 2 files changed +24
-11
lines changed Expand file tree Collapse file tree 2 files changed +24
-11
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
16
16
* Added context information to the ` CompilerException ` string output (#493 )
17
17
* Added Array (Python list) functions (#504 , #509 )
18
18
* Added shell function in ` basilisp.shell ` namespace (#515 )
19
+ * Added ` apply-template ` function to ` basilisp.core.template ` namespace (#516 )
19
20
20
21
### Changed
21
22
* Change the default user namespace to ` basilisp.user ` (#466 )
Original file line number Diff line number Diff line change 2
2
(:require
3
3
[basilisp.walk :refer [postwalk-replace]]))
4
4
5
+ (defn apply-template
6
+ "Given a template expression `expr` and bindings (as `argv`), replace all instances
7
+ of elements from `argv` in `expr` with the corresponding elements from `values`.
8
+
9
+ For example:
10
+
11
+ (apply-template '[x y] '(= x y) '[1 2])
12
+
13
+ produces
14
+
15
+ (= 1 2)"
16
+ [argv expr values]
17
+ (as-> values $
18
+ (interleave argv $)
19
+ (apply hash-map $)
20
+ (postwalk-replace $ expr)))
21
+
5
22
(defmacro do-template
6
- "Given a template expression expr and bindings, produce a do expression with
7
- the repeated templated expressions replacing names in argv with elements from
8
- args.
23
+ "Given a template expression ` expr` and bindings, produce a `do` expression with
24
+ the repeated templated expressions replacing names in ` argv` with elements from
25
+ ` args` .
9
26
10
27
For example:
11
28
19
36
(= 1 (dec 2))
20
37
(= 2 (inc 1)))"
21
38
[argv expr & args]
22
- (let [n (count argv)
23
- arg-groups (partition n args)
24
- template-expr (fn [arg-group]
25
- (as-> arg-group $
26
- (interleave argv $)
27
- (apply hash-map $)
28
- (postwalk-replace $ expr)))]
39
+ (let [n (count argv)
40
+ arg-groups (partition n args)]
29
41
`(do
30
- ~@(map template- expr arg-groups))) )
42
+ ~@(map #(apply-template argv expr %) arg-groups))))
You can’t perform that action at this time.
0 commit comments