More optimisation

This commit is contained in:
Simon Brooke 2014-07-22 02:40:17 +01:00
parent 87198076bc
commit 8a53adcd11

View file

@ -36,20 +36,11 @@
(cond (cond
(ifn? rule) (apply-rule cell world rule nil) (ifn? rule) (apply-rule cell world rule nil)
(seq? rule) (let [[afn src] rule] (apply-rule cell world afn src)))) (seq? rule) (let [[afn src] rule] (apply-rule cell world afn src))))
;; {:afn afn :src src})))
;; (apply-rule cell world (first rule) (first (rest rule)))))
([cell world rule source] ([cell world rule source]
(try (let [result (apply rule (list cell world))]
(let [result (apply rule (list cell world))] (cond
(cond (and result source) (merge result {:rule source})
(and result source) (merge result {:rule source}) true result))))
true result))
(catch Exception e
(merge cell {:error (format "%s at generation %d when in state %s"
(.getMessage e)
(:generation cell)
(:state cell))
:error-rule source})))))
(defn- apply-rules (defn- apply-rules
"Derive a cell from this cell of this world by applying these rules." "Derive a cell from this cell of this world by applying these rules."
@ -72,19 +63,20 @@
(format "%s at generation %d when in state %s" (format "%s at generation %d when in state %s"
(.getMessage e) (.getMessage e)
(:generation cell) (:generation cell)
(:state cell))})))) (:state cell))}))))
(defn- transform-world-row (defn- transform-world-row
"Return a row derived from this row of this world by applying these rules to each cell." "Return a row derived from this row of this world by applying these rules to each cell."
[row world rules] [row world rules]
(map #(transform-cell % world rules) row)) (apply vector (map #(transform-cell % world rules) row)))
(defn transform-world (defn transform-world
"Return a world derived from this world by applying these rules to each cell." "Return a world derived from this world by applying these rules to each cell."
[world rules] [world rules]
(map (apply vector
#(transform-world-row % world rules) (map
world)) #(transform-world-row % world rules)
world)))
(defn- transform-world-state (defn- transform-world-state
"Consider this single argument as a map of `:world` and `:rules`; apply the rules "Consider this single argument as a map of `:world` and `:rules`; apply the rules