Skip to content

Commit

Permalink
Break just before an instruction that opens code blocks or just after…
Browse files Browse the repository at this point in the history
… a close, unless can't
  • Loading branch information
lspector committed Jan 9, 2024
1 parent b0aee91 commit 71d378f
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/propeller/utils.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,29 @@

(defn break-up
"A utility function for bmx-related genetic operators. Returns the provided
:gap-free plushy with gaps randomly inserted to ensure that no gene is longer
than the provided limit."
:gap-free plushy with gaps randomly inserted to ensure that no gene is longer
than the provided limit. Will break just before an instruction that opens code
blocks or just after a close, unless there are no opportunities to do so"
[gene limit]
(if (> (count gene) limit)
(let [i (inc (rand-int (dec (count gene))))]
(let [openers (map first
(filter #(and (second %)
(not (zero? (second %))))
parentheses/opens))
i (if (or (some (set openers) (rest gene))
(some #{'close} (butlast gene)))
(rand-nth (filter identity
(concat (map-indexed (fn [ix item]
(if (some #{item} openers)
(inc ix)
nil))
(rest gene))
(map-indexed (fn [ix item]
(if (= item 'close)
(inc ix)
nil))
(butlast gene)))))
(inc (rand-int (dec (count gene)))))]
(concat (break-up (take i gene) limit)
[:gap]
(break-up (drop i gene) limit)))
Expand Down

0 comments on commit 71d378f

Please sign in to comment.