Skip to content
Open

solved #1186

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 52 additions & 35 deletions src/structured_data.clj
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
(ns structured-data)

(defn do-a-thing [x]
:-)
(let [xx (+ x x)]
(Math/pow xx xx)))

(defn spiff [v]
:-)
(+ (get v 0) (get v 2)))

(defn cutify [v]
:-)
(conj v "<3"))

(defn spiff-destructuring [v]
:-)
(let [[a _ b] v ]
(+ a b)))

(defn point [x y]
[x y])
Expand All @@ -19,96 +21,111 @@
[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]
:-)
(= (width rectangle) (height rectangle)))

(defn area [rectangle]
:-)
(* (width rectangle) (height rectangle)))

(defn contains-point? [rectangle point]
:-)
(let [[[x1 y1] [x2 y2]] rectangle
[x3 y3] point]
(and (<= x1 x3 x2) (<= y1 y3 y2))))

(defn contains-rectangle? [outer inner]
:-)
(let [[down up] inner]
(and (contains-point? outer down)
(contains-point? outer up))))

(defn title-length [book]
:-)
(count (:title book)))

(defn author-count [book]
:-)
(count (:authors book)))

(defn multiple-authors? [book]
:-)
(> (author-count book) 1))

(defn add-author [book new-author]
:-)
(assoc book :authors (conj (:authors book) new-author)))

(defn alive? [author]
:-)
(nil? (:death-year author)))

(defn element-lengths [collection]
:-)
(map #(count %) collection))

(defn second-elements [collection]
:-)
(map #(get % 1) 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 a-seq) (count (set 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]
:-)
(str (:name author)
(when (:birth-year author)
(str " (" (:birth-year author) " - " (:death-year author) ")"))))

(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]
:-)
(if (zero? (count books))
"No books."
(str (count books) " book" (when (> (count books) 1)
"s") ". "
(apply 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( fn [author] (= (:name author) name)) authors)))

(defn living-authors [authors]
:-)
(filter alive? authors))

(defn has-a-living-author? [book]
:-)
(not (empty? (living-authors (:authors book)))))

(defn books-by-living-authors [books]
:-)
(filter has-a-living-author? books))

; %________%