Skip to content

Instantly share code, notes, and snippets.

@digal
Last active January 3, 2016 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digal/8418398 to your computer and use it in GitHub Desktop.
Save digal/8418398 to your computer and use it in GitHub Desktop.
Hobbit symmetrization
(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