From 70e9f81f84987d146aa793bf55e71d694dd0357a Mon Sep 17 00:00:00 2001 From: Lee Spector Date: Fri, 12 Jan 2024 16:25:59 -0500 Subject: [PATCH] Implement :close options :specified (default until recently, and now again), :balanced (default just prior to this commit), :none (which also removes instructions that open multiple code blocks) --- src/propeller/utils.cljc | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/propeller/utils.cljc b/src/propeller/utils.cljc index 1c5538d..30d2c0a 100755 --- a/src/propeller/utils.cljc +++ b/src/propeller/utils.cljc @@ -51,16 +51,30 @@ "Returns a random instruction from a supplied pool of instructions, evaluating ERC-producing functions to a constant literal." [instructions argmap] - (let [instructions (remove #(= % 'close) instructions) - p (/ (apply + (filter identity - (map #(get parentheses/opens %) instructions))) - (count instructions))] - (if (< (rand) p) - 'close - (let [instruction (rand-nth instructions)] - (if (fn? instruction) - (instruction) - instruction))))) + (case (:closes argmap) + :specified (let [instruction (rand-nth instructions)] + (if (fn? instruction) + (instruction) + instruction)) + :balanced (let [source (remove #(= % 'close) instructions) + p (/ (apply + (filter identity + (map #(get parentheses/opens %) source))) + (count source))] + (if (< (rand) p) + 'close + (let [instruction (rand-nth source)] + (if (fn? instruction) + (instruction) + instruction)))) + :none (let [multi-block-instructions (set (filter (fn [i] + (let [opens (get parentheses/opens i)] + (and opens (> opens 1)))) + instructions)) + source (remove (set (conj multi-block-instructions 'close)) instructions) + instruction (rand-nth source)] + (if (fn? instruction) + (instruction) + instruction)))) (defn count-points "Returns the number of points in tree, where each atom and each pair of parentheses