From aadc28d3718f584c91ff1ac714465a86936efa70 Mon Sep 17 00:00:00 2001 From: Lee Spector Date: Sun, 22 Nov 2020 16:35:46 -0500 Subject: [PATCH] Make plushy->push use an empty argmap if none was passed. --- src/propeller/genome.cljc | 47 ++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/src/propeller/genome.cljc b/src/propeller/genome.cljc index 8d2bbed..d032081 100755 --- a/src/propeller/genome.cljc +++ b/src/propeller/genome.cljc @@ -11,26 +11,27 @@ (defn plushy->push "Returns the Push program expressed by the given plushy representation." - [plushy argmap] - (let [plushy (if (:diploid argmap) (map first (partition 2 plushy)) plushy) - opener? #(and (vector? %) (= (first %) 'open))] ;; [open ] marks opens - (loop [push () ;; iteratively build the Push program from the plushy - plushy (mapcat #(if-let [n (get push/opens %)] [% ['open n]] [%]) plushy)] - (if (empty? plushy) ;; maybe we're done? - (if (some opener? push) ;; done with plushy, but unclosed open - (recur push '(close)) ;; recur with one more close - push) ;; otherwise, really done, return push - (let [i (first plushy)] - (if (= i 'close) - (if (some opener? push) ;; process a close when there's an open - (recur (let [post-open (reverse (take-while (comp not opener?) - (reverse push))) - open-index (- (count push) (count post-open) 1) - num-open (second (nth push open-index)) - pre-open (take open-index push)] - (if (= 1 num-open) - (concat pre-open [post-open]) - (concat pre-open [post-open ['open (dec num-open)]]))) - (rest plushy)) - (recur push (rest plushy))) ;; unmatched close, ignore - (recur (concat push [i]) (rest plushy)))))))) ;; anything else + ([plushy] (plushy->push plushy {})) + ([plushy argmap] + (let [plushy (if (:diploid argmap) (map first (partition 2 plushy)) plushy) + opener? #(and (vector? %) (= (first %) 'open))] ;; [open ] marks opens + (loop [push () ;; iteratively build the Push program from the plushy + plushy (mapcat #(if-let [n (get push/opens %)] [% ['open n]] [%]) plushy)] + (if (empty? plushy) ;; maybe we're done? + (if (some opener? push) ;; done with plushy, but unclosed open + (recur push '(close)) ;; recur with one more close + push) ;; otherwise, really done, return push + (let [i (first plushy)] + (if (= i 'close) + (if (some opener? push) ;; process a close when there's an open + (recur (let [post-open (reverse (take-while (comp not opener?) + (reverse push))) + open-index (- (count push) (count post-open) 1) + num-open (second (nth push open-index)) + pre-open (take open-index push)] + (if (= 1 num-open) + (concat pre-open [post-open]) + (concat pre-open [post-open ['open (dec num-open)]]))) + (rest plushy)) + (recur push (rest plushy))) ;; unmatched close, ignore + (recur (concat push [i]) (rest plushy))))))))) ;; anything else