Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

structured data done. #1200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
101 changes: 66 additions & 35 deletions src/structured_data.clj
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
(ns structured-data)

(defn train
[]
(println "Choo choo!"))

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

(defn spiff [v]
:-)
(let [first (get v 0)
third (get v 2)]
(if (or (nil? first) (nil? third))
nil
(+ first third))))

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

(defn spiff-destructuring [v]
:-)
(let [[first _ third] v]
(if (or (nil? first) (nil? third))
nil
(+ first third))))

(defn point [x y]
[x y])
Expand All @@ -19,96 +31,115 @@
[bottom-left top-right])

(defn width [rectangle]
:-)
(let [[[x1] [x2]] rectangle]
(- x2 x1)))

(defn height [rectangle]
:-)
(let [[[_ y1] [_ y2]] rectangle]
(- y2 y1)))

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

(defn area [rectangle]
:-)
(let [[[x1 y1] [x2 y2]] rectangle]
(* (- x2 x1) (- y2 y1))))

(defn contains-point? [rectangle point]
:-)
(let [[[x1 y1] [x2 y2]] rectangle
[x y] point]
(and (<= x1 x x2) (<= y1 y 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]
:-)
(> (author-count book) 1))

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

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

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

(defn second-elements [collection]
:-)
(let [second (fn [coll] (first (rest coll)))]
(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]
:-)
(let [new-authors (set (:authors book))]
(assoc book :authors new-authors)))

(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-year (:birth-year author)
death-year (:death-year author)
years (if birth-year (str "(" birth-year " - " death-year ")"))]
(if years (str name " " years) name)))

(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 [num-books (count books)
zero-books (= 0 num-books)]
(if zero-books
"No books."
(str num-books (if (= 1 num-books) " book. " " books. ") (apply str (interpose ". " (map book->string books))) "."))))

(defn books-by-author [author books]
:-)
(filter (fn [book] (has-author? book author)) books))

(defn author-by-name [name authors]
:-)
(first (filter (fn [author] (= name (:name author))) 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))

; %________%