Conditionally add a new key-value to a map
(conj m (when value [key value]))
Usage example:
(conj {
:type "if"
:cond cond_val
:then then_val
} (when else_val [:else else_val]))
In the above example, the "else"-part is added to the token only if it is not empty.
Source.Replace nth element in a vector
Clojure> (assoc [1 2 3] 1 5) [1 5 3]
Usage example:
(defn dialogue[num]
(let [dacode (map #(- (int %) 48) (Integer/toString num 2))
phrases [(s/split"It would be extremely painful... for you!"#" ")
(s/split"If I pull that off will you die? You're a big guy."#" ")]
actors ["BANE" "CIA"]]
(loop [idxs [0 0] finaldial "" code dacode prevbit 2]
(if (< (count code) 1) finaldial
(let [bit (first code) idx (idxs bit)]
(recur (assoc idxs bit (inc idx)) (str finaldial (if (= bit prevbit) " " (str (when (not= 2 prevbit) "\n")
(actors bit) ": ")) ((phrases bit) idx)) (rest code) bit))))))
Important part:
(assoc idxs bit (inc idx))
In this example there are two counters, stored in idxs vector (initially 0, 0). In each step we update one of the two, indexed by bit
Source.