diff --git a/src/structured_data.clj b/src/structured_data.clj index ebfe1ce4..2691da08 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -1,16 +1,19 @@ (ns structured-data) (defn do-a-thing [x] - :-) + (let [xx (+ x x)] + (Math/pow xx xx))) (defn spiff [v] - :-) + (let [el1 (first v) + el3 (get v 2)] + (+ el1 el3))) (defn cutify [v] - :-) + (conj v "<3")) -(defn spiff-destructuring [v] - :-) +(defn spiff-destructuring [[a b c]] + (+ a c)) (defn point [x y] [x y]) @@ -19,96 +22,114 @@ [bottom-left top-right]) (defn width [rectangle] - :-) + (let [[[x1 y1] [x2 y2]] rectangle] + (- x2 x1))) (defn height [rectangle] - :-) + (let [[[x1 y1] [x2 y2]] rectangle] + (- y2 y1))) (defn square? [rectangle] - :-) + (let [[[x1 y1] [x2 y2]] rectangle] + (= (Math/abs(- x2 x1)) (Math/abs(- y2 y1))))) (defn area [rectangle] - :-) + (let [[[x1 y1] [x2 y2]] rectangle] + (* (- y2 y1) (- x2 x1)))) (defn contains-point? [rectangle point] - :-) + (let [[[x1 y1] [x2 y2]] rectangle + [p1 p2] point] + (and (<= x1 p1 x2) (<= y1 p2 y2)))) (defn contains-rectangle? [outer inner] - :-) + (let [[p1 p2] inner] + (and (contains-point? outer p1) (contains-point? outer p2)))) (defn title-length [book] - :-) + (count (:title book))) (defn author-count [book] - :-) + (count (:authors book))) (defn multiple-authors? [book] - :-) + (> (count (:authors book)) 1)) (defn add-author [book new-author] - :-) + (let [authors (:authors book)] + (assoc book :authors (conj authors new-author)) + )) (defn alive? [author] - :-) + (let [death-year (:death-year author)] + (nil? death-year))) (defn element-lengths [collection] - :-) + (map (fn [x] (count x)) collection)) (defn second-elements [collection] - :-) + (map #(second %) collection)) (defn titles [books] - :-) + (map :title books)) (defn monotonic? [a-seq] - :-) + (or (apply <= a-seq) (apply >= a-seq))) (defn stars [n] - :-) + (apply str (repeat n "*"))) (defn toggle [a-set elem] - :-) + (if (contains? a-set elem) + (disj a-set elem) + (conj a-set elem))) (defn contains-duplicates? [a-seq] - :-) + (< (count (set a-seq)) (count a-seq))) (defn old-book->new-book [book] - :-) + (assoc book :authors (set (:authors book)) )) (defn has-author? [book author] - :-) + (contains? (:authors book) author )) (defn authors [books] - :-) + (apply clojure.set/union (map :authors books))) (defn all-author-names [books] - :-) + (set (map :name (authors books)))) (defn author->string [author] - :-) + (let [{:keys [name birth-year death-year]} author] + (str name (when birth-year (str " (" birth-year " - " death-year ")"))))) (defn authors->string [authors] - :-) + (apply str (interpose ", " (map author->string authors)))) (defn book->string [book] - :-) + (str (:title book) ", written by " (authors->string (:authors book)))) (defn books->string [books] - :-) + (let [book-count (count books) + book-number-str (cond + (= book-count 1) (str book-count " book. ") + (< book-count 1) (str "No books") + (> book-count 1) (str book-count " books. "))] + (str (apply str book-number-str (interpose ". " (map book->string books))) "."))) (defn books-by-author [author books] - :-) + (filter #(has-author? % author) books)) (defn author-by-name [name authors] - :-) + (first (filter #(= (:name %) name) authors))) (defn living-authors [authors] - :-) + (filter #(nil? (:death-year %)) authors)) (defn has-a-living-author? [book] - :-) + (> (count (living-authors (:authors book))) 0)) (defn books-by-living-authors [books] - :-) + (filter #(has-a-living-author? %) books)) ; %________%