Skip to content

Commit

Permalink
Implement :close options :specified (default until recently, and now …
Browse files Browse the repository at this point in the history
…again), :balanced (default just prior to this commit), :none (which also removes instructions that open multiple code blocks)
  • Loading branch information
lspector committed Jan 12, 2024
1 parent ddb788a commit 70e9f81
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/propeller/utils.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 70e9f81

Please sign in to comment.