Make plushy->push use an empty argmap if none was passed.

This commit is contained in:
Lee Spector 2020-11-22 16:35:46 -05:00
parent 0a5e17195e
commit aadc28d371

View File

@ -11,26 +11,27 @@
(defn plushy->push (defn plushy->push
"Returns the Push program expressed by the given plushy representation." "Returns the Push program expressed by the given plushy representation."
[plushy argmap] ([plushy] (plushy->push plushy {}))
(let [plushy (if (:diploid argmap) (map first (partition 2 plushy)) plushy) ([plushy argmap]
opener? #(and (vector? %) (= (first %) 'open))] ;; [open <n>] marks opens (let [plushy (if (:diploid argmap) (map first (partition 2 plushy)) plushy)
(loop [push () ;; iteratively build the Push program from the plushy opener? #(and (vector? %) (= (first %) 'open))] ;; [open <n>] marks opens
plushy (mapcat #(if-let [n (get push/opens %)] [% ['open n]] [%]) plushy)] (loop [push () ;; iteratively build the Push program from the plushy
(if (empty? plushy) ;; maybe we're done? plushy (mapcat #(if-let [n (get push/opens %)] [% ['open n]] [%]) plushy)]
(if (some opener? push) ;; done with plushy, but unclosed open (if (empty? plushy) ;; maybe we're done?
(recur push '(close)) ;; recur with one more close (if (some opener? push) ;; done with plushy, but unclosed open
push) ;; otherwise, really done, return push (recur push '(close)) ;; recur with one more close
(let [i (first plushy)] push) ;; otherwise, really done, return push
(if (= i 'close) (let [i (first plushy)]
(if (some opener? push) ;; process a close when there's an open (if (= i 'close)
(recur (let [post-open (reverse (take-while (comp not opener?) (if (some opener? push) ;; process a close when there's an open
(reverse push))) (recur (let [post-open (reverse (take-while (comp not opener?)
open-index (- (count push) (count post-open) 1) (reverse push)))
num-open (second (nth push open-index)) open-index (- (count push) (count post-open) 1)
pre-open (take open-index push)] num-open (second (nth push open-index))
(if (= 1 num-open) pre-open (take open-index push)]
(concat pre-open [post-open]) (if (= 1 num-open)
(concat pre-open [post-open ['open (dec num-open)]]))) (concat pre-open [post-open])
(rest plushy)) (concat pre-open [post-open ['open (dec num-open)]])))
(recur push (rest plushy))) ;; unmatched close, ignore (rest plushy))
(recur (concat push [i]) (rest plushy)))))))) ;; anything else (recur push (rest plushy))) ;; unmatched close, ignore
(recur (concat push [i]) (rest plushy))))))))) ;; anything else