Skip to content

Instantly share code, notes, and snippets.

@lilactown
Created April 7, 2021 16:10
Show Gist options
  • Save lilactown/d4f87bad50a81384eb9fe609cfad60ef to your computer and use it in GitHub Desktop.
Save lilactown/d4f87bad50a81384eb9fe609cfad60ef to your computer and use it in GitHub Desktop.
(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]
(lazy-seq
(let [s1 (seq c1)
s2 (seq c2)]
(cond
(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)))
(comment
(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)))
(comment
(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