-
-
Save oakmac/571cba7e36e25a117341afdf3bcad5a0 to your computer and use it in GitHub Desktop.
Hobbit Clojure thing
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
(ns hobbit.core | |
(:require | |
[clojure.string :as str])) | |
;; CO: let's break up this data structure so things can be composed... | |
; (defn asym-hobbit-body-parts | |
; [{:name "head" :size 3} | |
; {:name "left-eye" :size 1} | |
; {:name "left-ear" :size 1} | |
; {:name "mouth" :size 1} | |
; {:name "nose" :size 1} | |
; {:name "neck" :size 2} | |
; {:name "left-shoulder" :size 3} | |
; {:name "left-upper-arm" :size 3} | |
; {:name "chest" :size 10} | |
; {:name "back" :size 10} | |
; {:name "left-forearm" :size 3} | |
; {:name "abdomen" :size 6} | |
; {:name "left-kidney" :size 1} | |
; {:name "left-hand" :size 2} | |
; {:name "left-knee" :size 2} | |
; {:name "left-thigh" :size 4} | |
; {:name "left-lower-leg" :size 3} | |
; {:name "left-achilles" :size 1} | |
; {:name "left-foot" :size 2}]) | |
;; CO: use a map so we ensure that we don't repeat any part names | |
;; Note that is it a trivial change to convert it from a map into the above format | |
;; should we desire to have it organized like that. | |
(def center-parts | |
{"head" 3 | |
"mouth" 1 | |
"nose" 1 | |
"neck" 2 | |
"chest" 10 | |
"back" 10 | |
"abdomen" 6}) | |
(def left-parts | |
{"left-eye" 1 | |
"left-ear" 1 | |
"left-shoulder" 3 | |
"left-upper-arm" 3 | |
"left-forearm" 3 | |
"left-kidney" 1 | |
"left-hand" 2 | |
"left-knee" 2 | |
"left-thigh" 4 | |
"left-lower-leg" 3 | |
"left-achilles" 1 | |
"left-foot" 2}) | |
;; CO: now the right parts are a simple transformation of the left parts | |
(def right-parts | |
(zipmap (map #(str/replace % "left-" "right-") (keys left-parts)) | |
(vals left-parts))) | |
;; CO: And all of the parts are joined easiliy. This is the "composability" I was | |
;; referring to earlier. | |
(def all-parts | |
(merge center-parts | |
left-parts | |
right-parts)) | |
(def all-parts-total-size | |
(reduce + 0 (vals all-parts))) | |
;; CO: I do not understand what you are trying to do with this function. | |
;; We take a random hit value between 0 and the total size, and then subtract | |
;; that value from body parts? Does that need to happen in a certain order? | |
;; Seems like there is just problem domain logic here I do not fully understand. | |
; (defn hit | |
; [asym-body-parts] | |
; let [sym-parts (better-symmetrize-body-parts asym-body-parts) | |
; body-part-size-sum (reduce + (map :size sym-parts)) | |
; target (rand body-part-size-sum)] | |
; (loop [[part & remaining] sym-parts | |
; accumulated-size (:size part)] | |
; (if (> accumulated-size target) | |
; part | |
; (recur remaining (+ accumulated-size (:size (first remaining))))))) | |
(defn -main | |
"Application entrypoint." | |
[& args] | |
(println (str "Total body parts size: " all-parts-total-size))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment