From c129643b2bb8a636c4ff16dea58163cb1c59f651 Mon Sep 17 00:00:00 2001 From: James Raikes Date: Fri, 22 Sep 2017 02:32:58 +0100 Subject: [PATCH 1/2] Part 3 complete. --- src/structured_data.clj | 92 +++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index ebfe1ce4..699f0619 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -1,16 +1,18 @@ (ns structured-data) (defn do-a-thing [x] - :-) + (let [x2 (+ x x)] + (Math/pow x2 x2))) (defn spiff [v] - :-) + (+ (get v 0) (get v 2))) (defn cutify [v] - :-) + (conj v "<3")) (defn spiff-destructuring [v] - :-) + (let [[a b c] v] + (+ a c))) (defn point [x y] [x y]) @@ -19,96 +21,116 @@ [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 + [xp yp] point] + (and (<= x1 xp x2) (<= y1 yp y2)))) (defn contains-rectangle? [outer inner] - :-) + (let [[bl tr] inner] + (and (contains-point? outer bl) (contains-point? outer tr)))) (defn title-length [book] - :-) + (count (:title book))) (defn author-count [book] - :-) + (count (:authors book))) (defn multiple-authors? [book] - :-) + (< 1 (author-count book))) (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] - :-) + (str (:name author) + (if (contains? author :birth-year) + (str " (" + (:birth-year author) + " - " + (:death-year author) + ")")))) (defn authors->string [authors] - :-) + (apply str (interpose ", " (map author->string authors)))) (defn book->string [book] - :-) + (apply str (str (:title book) + ", written by " + (authors->string (:authors book))))) (defn books->string [books] - :-) + (str (cond + (empty? books) "No books" + (= (count books) 1) "1 book. " + (< 1 (count books)) (str (count books) " 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 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)) ; %________% From 9fa4150ec3bd5a7470270fa9895891ea6e1e69c8 Mon Sep 17 00:00:00 2001 From: James Raikes Date: Thu, 28 Sep 2017 23:49:23 +0100 Subject: [PATCH 2/2] Travis fix --- .travis.yml | 4 ++-- src/structured_data.clj | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 455f3c0e..45c29f60 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: clojure -lein: lein2 -script: lein2 midje :config .midje-grading-config.clj +lein: lein +script: lein midje :config .midje-grading-config.clj jdk: - openjdk7 notifications: diff --git a/src/structured_data.clj b/src/structured_data.clj index 699f0619..4a0fe0b3 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -119,10 +119,10 @@ ".")) (defn books-by-author [author books] - (filter (fn [book] (has-author? book author)) books)) + (filter #(has-author? % author) books)) (defn author-by-name [name authors] - (first (filter (fn [author] (= (:name author) name)) authors))) + (first (filter #(= (:name %) name) authors))) (defn living-authors [authors] (filter alive? authors))