Skip to content
Open
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
105 changes: 64 additions & 41 deletions src/structured_data.clj
Original file line number Diff line number Diff line change
@@ -1,114 +1,137 @@
(ns structured-data)

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

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

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

(defn spiff-destructuring [v]
:-)
(defn spiff-destructuring [[x _ z]]
(+ x z))

(defn point [x y]
[x y])

(defn rectangle [bottom-left top-right]
[bottom-left top-right])

(defn width [rectangle]
:-)
(defn width [[[x0 _] [x1 _]]]
(- x1 x0))

(defn height [rectangle]
:-)
(defn height [[[_ y0] [_ y1]]]
(- y1 y0))

(defn square? [rectangle]
:-)
(= (height rectangle) (width rectangle)))

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

(defn contains-point? [rectangle point]
:-)
(defn contains-point? [[[x0 y0] [x1 y1]] [px py]]
(and (<= x0 px x1) (<= y0 py y1)))

(defn contains-rectangle? [outer inner]
:-)
(defn contains-rectangle? [outer [[inx0 iny0] [inx1 iny1]]]
(and (contains-point? outer [inx0 iny0])
(contains-point? outer [inx1 iny1])))

(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]
:-)
(defn add-author [book new-author]
(assoc book
:authors
(conj (:authors book) new-author)))

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

(defn element-lengths [collection]
:-)
(map count 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]
:-)
(not= (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]
:-)
(let [name (:name author)
birth (:birth-year author)
death (:death-year author)]
(str name
(if birth
(str " (" birth " - " (or death "") ")")
""))))

(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 (empty? books)
"No books."
(let [n (count books)
header (str n " " (if (= n 1) "book." "books."))
bodies (apply str (interpose ". " (map book->string books)))]
(str header " " bodies "."))))

(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 alive? authors))

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

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

; %________%