From 09e4fed64dd4346bb2b73bcf04d2f7983d436341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20R=C3=A4ntil=C3=A4?= Date: Fri, 3 Jul 2015 23:02:24 +0300 Subject: [PATCH 1/7] Exercises upto points and rectangles. --- src/structured_data.clj | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index ebfe1ce4..65c5d35e 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -1,16 +1,18 @@ (ns structured-data) (defn do-a-thing [x] - :-) + (let [x-twice (+ x x)] + (Math/pow x-twice x-twice))) (defn spiff [v] - :-) + (+ (get v 0) (get v 2))) (defn cutify [v] - :-) + (conj v "<3")) (defn spiff-destructuring [v] - :-) + (let [[first second third] v] + (+ first third))) (defn point [x y] [x y]) From b495241c16aa97c59529d7811a7c8dce69ea5635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20R=C3=A4ntil=C3=A4?= Date: Sat, 4 Jul 2015 20:04:09 +0300 Subject: [PATCH 2/7] Rectangle's width, height and square?. --- src/structured_data.clj | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index 65c5d35e..c9bb85d0 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -21,13 +21,15 @@ [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] :-) From e12f268e9428dffdf3e48bfb3767ab670b6d3a20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20R=C3=A4ntil=C3=A4?= Date: Sat, 4 Jul 2015 20:17:11 +0300 Subject: [PATCH 3/7] Rectangle's area, contains-point? and contains-rectangle?. --- src/structured_data.clj | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index c9bb85d0..cf3b3dc7 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -32,13 +32,16 @@ (= (width rectangle) (height rectangle))) (defn area [rectangle] - :-) + (* (width rectangle) (height rectangle))) (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] - :-) + (every? (fn [inner-point] + (contains-point? outer inner-point)) inner)) (defn title-length [book] :-) From 8e9f2ff9bf2f2d0902dafa2a324ee8eeee9d1df1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20R=C3=A4ntil=C3=A4?= Date: Sat, 4 Jul 2015 20:50:56 +0300 Subject: [PATCH 4/7] Look-up, map and apply related function implementations. --- src/structured_data.clj | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index cf3b3dc7..5e9edebb 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -44,34 +44,39 @@ (contains-point? outer inner-point)) inner)) (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] - :-) + ; Built-in (second coll) would have done the job as well, supposedly. + (let [pick-second (fn [collection] (get collection 1))] + (map pick-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] :-) From 65034788b139364b79a95bf765e8b5482449c772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20R=C3=A4ntil=C3=A4?= Date: Sat, 4 Jul 2015 21:07:32 +0300 Subject: [PATCH 5/7] Set related functions implemented. --- src/structured_data.clj | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index 5e9edebb..75bf3c1d 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -79,22 +79,24 @@ (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] :-) From a28322f73df17dddd889ed1e805ce857b50e45df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20R=C3=A4ntil=C3=A4?= Date: Sat, 4 Jul 2015 21:32:03 +0300 Subject: [PATCH 6/7] Stringifier functions. --- src/structured_data.clj | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index 75bf3c1d..abbb7250 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -99,16 +99,28 @@ (set (map :name (authors books)))) (defn author->string [author] - :-) + (str (:name author) + (if (: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] - :-) + ; This doesn't seem all so elegant, but oh well. :--D + (str (cond + (empty? books) "No books" + (= (count books) 1) "1 book. " + :else (str (count books) " books. ")) + (apply str (interpose ". " (map book->string books))) + ".")) (defn books-by-author [author books] :-) From 9a5dd41d7116a62ed8a401dbcafc0ccc4438de5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20R=C3=A4ntil=C3=A4?= Date: Sat, 4 Jul 2015 22:17:39 +0300 Subject: [PATCH 7/7] The rest of aggregate actions. --- src/structured_data.clj | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/structured_data.clj b/src/structured_data.clj index abbb7250..eae09785 100644 --- a/src/structured_data.clj +++ b/src/structured_data.clj @@ -123,18 +123,18 @@ ".")) (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)) ; %________%