Skip to content

Instantly share code, notes, and snippets.

Created April 7, 2021 16:10
What would you like to do?
(ns lilactown.template
#?(:cljs (:require-macros [lilactown.template])))
(defn- interleave*
"A version of interleave that continues to build the lazy seq even if one of
the coll has no elements left"
([] ())
([c1] (lazy-seq c1))
([c1 c2]
(let [s1 (seq c1)
s2 (seq c2)]
(and s1 s2)
(cons (first s1)
(cons (first s2)
(interleave* (rest s1) (rest s2))))
s1 (cons (first s1)
(interleave* (rest s1)))
s2 (cons (first s2)
(interleave* (rest s2))))))))
(defn default-tag
[strings & args]
(apply str (interleave* strings args)))
(default-tag ["hello " ", how are you?"] "will"))
(defmacro t
[& body]
(let [[tag body] (if (not (string? (first body)))
[(first body) (rest body)]
[default-tag body])
strings (into [] (filter string?) body)
interpolations (into [] (remove string?) body)]
`(~tag ~strings ~@interpolations)))
(let [user "Will"]
(t "Hello, " user ", how are you today?"))
(defn custom-tag
[strings & args]
{:strings strings
:bindings args})
(let [user "Will"]
(t custom-tag "Hello, " user ", how are you today?")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment