Last active
January 3, 2016 05:49
-
-
Save digal/8418398 to your computer and use it in GitHub Desktop.
Hobbit symmetrization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn symmetrize-body-parts | |
"Expects a seq of maps which have a :name and :size" | |
[asym-body-parts] | |
(loop [unprocessed-parts asym-body-parts | |
processed-parts nil] | |
(println (str "processed: " processed-parts)) | |
(println (str "unprocessed: " unprocessed-parts)) | |
(if unprocessed-parts | |
(let [[current-part & remaining-parts] unprocessed-parts | |
processed-with-left (conj processed-parts current-part)] | |
(println (str "current: " current-part)) | |
(if (left-match current-part) | |
(recur remaining-parts (conj processed-with-left {:name (left-match-replace current-part) :size (:size current-part)})) | |
(recur remaining-parts processed-with-left) | |
) | |
) | |
processed-parts | |
) | |
) ;loop | |
) ;defn | |
(defn symmetrize-body-parts-with-reduce | |
"Same shit, more functionally" | |
[asym-body-parts] | |
(reduce (fn [processed-parts current-part] | |
(println (str "current: " current-part)) | |
(let [processed-with-left (conj processed-parts current-part)] | |
(if (left-match current-part) | |
(conj processed-with-left {:name (left-match-replace current-part) :size (:size current-part)}) | |
processed-with-left) | |
) | |
) | |
nil ;initial collection | |
asym-body-parts ;reduced collection | |
) ;reduce | |
) ;defn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment