Skip to content

Commit 5676084

Browse files
committed
Some new exercises
1 parent 2e96e3b commit 5676084

File tree

2 files changed

+148
-74
lines changed

2 files changed

+148
-74
lines changed

src/structured_data.clj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@
6969
(defn contains-duplicates? [a-seq]
7070
:-)
7171

72+
(defn old-book->new-book [book]
73+
:-)
74+
75+
(defn has-author? [book author]
76+
:-)
77+
78+
(defn authors [books]
79+
:-)
80+
81+
(defn all-author-names [books]
82+
:-)
83+
7284
(defn author->string [author]
7385
:-)
7486

test/structured_data_test.clj

Lines changed: 136 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -82,40 +82,53 @@
8282

8383
(def books [cities, wild-seed, embassytown, little-schemer])
8484

85-
(facts "title-length"
86-
(title-length cities) => 21
87-
(title-length wild-seed) => 9
88-
(title-length little-schemer) => 18)
89-
90-
(facts "author-count"
91-
(author-count cities) => 1
92-
(author-count wild-seed) => 1
93-
(author-count little-schemer) => 2)
94-
95-
(facts "add-author"
96-
(add-author little-schemer {:name "Gerald J. Sussman"})
97-
=> {:title "The Little Schemer"
98-
:authors [{:birth-year 1944, :name "Daniel Friedman"}
99-
{:name "Matthias Felleisen"}
100-
{:name "Gerald J. Sussman"}]}
101-
(add-author {:authors [{:name "Juhana"}]} {:name "Jani"})
102-
=> {:authors [{:name "Juhana"} {:name "Jani"}]})
103-
104-
(facts "alive?"
105-
(alive? china) => true
106-
(alive? octavia) => false)
107-
108-
(facts "element-lengths"
109-
(element-lengths ["foo" "bar" "" "quux"]) => [3 3 0 4]
110-
(element-lengths ["x" [:a :b :c] {:y 42}]) => [1 3 1])
111-
112-
(facts "second-elements"
113-
(second-elements [[1 2] [2 3] [3 4]]) => [2 3 4]
114-
(second-elements [[1 2 3 4] [1] ["a" "s" "d" "f"]]) => [2 nil "s"])
115-
116-
(facts "titles"
117-
(titles [cities]) => ["The City and the City"]
118-
(titles books) => ["The City and the City" "Wild Seed" "Embassytown"])
85+
(let [china {:name "China Miéville", :birth-year 1972}
86+
octavia {:name "Octavia E. Butler"
87+
:birth-year 1947
88+
:death-year 2006}
89+
friedman {:name "Daniel Friedman" :birth-year 1944}
90+
felleisen {:name "Matthias Felleisen"}
91+
cities {:title "The City and the City" :authors [china]}
92+
wild-seed {:title "Wild Seed", :authors [octavia]}
93+
embassytown {:title "Embassytown", :authors [china]}
94+
little-schemer {:title "The Little Schemer"
95+
:authors [friedman, felleisen]}
96+
books [cities, wild-seed, embassytown, little-schemer]]
97+
98+
(facts "title-length"
99+
(title-length cities) => 21
100+
(title-length wild-seed) => 9
101+
(title-length little-schemer) => 18)
102+
103+
(facts "author-count"
104+
(author-count cities) => 1
105+
(author-count wild-seed) => 1
106+
(author-count little-schemer) => 2)
107+
108+
(facts "add-author"
109+
(add-author little-schemer {:name "Gerald J. Sussman"})
110+
=> {:title "The Little Schemer"
111+
:authors [{:birth-year 1944, :name "Daniel Friedman"}
112+
{:name "Matthias Felleisen"}
113+
{:name "Gerald J. Sussman"}]}
114+
(add-author {:authors [{:name "Juhana"}]} {:name "Jani"})
115+
=> {:authors [{:name "Juhana"} {:name "Jani"}]})
116+
117+
(facts "alive?"
118+
(alive? china) => true
119+
(alive? octavia) => false)
120+
121+
(facts "element-lengths"
122+
(element-lengths ["foo" "bar" "" "quux"]) => [3 3 0 4]
123+
(element-lengths ["x" [:a :b :c] {:y 42}]) => [1 3 1])
124+
125+
(facts "second-elements"
126+
(second-elements [[1 2] [2 3] [3 4]]) => [2 3 4]
127+
(second-elements [[1 2 3 4] [1] ["a" "s" "d" "f"]]) => [2 nil "s"])
128+
129+
(facts "titles"
130+
(titles [cities]) => ["The City and the City"]
131+
(titles books) => ["The City and the City" "Wild Seed" "Embassytown"]))
119132

120133
(facts "monotonic?"
121134
(monotonic? [1 2 3]) => true
@@ -138,45 +151,94 @@
138151
(contains-duplicates? [1 2 3 -40]) => false
139152
(contains-duplicates? [1 2 3 "a" "a"]) => true)
140153

141-
(facts "author->string"
142-
(author->string felleisen) => "Matthias Felleisen"
143-
(author->string friedman) => "Daniel Friedman (1944 - )"
144-
(author->string octavia) => "Octavia E. Butler (1947 - 2006)")
145-
146-
(facts "authors->string"
147-
(authors->string (:authors little-schemer)) => "Daniel Friedman (1944 - ), Matthias Felleisen"
148-
(authors->string [octavia]) => "Octavia E. Butler (1947 - 2006)"
149-
(authors->string []) => ""
150-
(authors->string [octavia, friedman]) => "Octavia E. Butler (1947 - 2006), Daniel Friedman (1944 - )")
151-
152-
(facts "book->string"
153-
(book->string wild-seed) => "Wild Seed, written by Octavia E. Butler"
154-
(book->string little-schemer)
155-
=> "The Little Schemer, written by Daniel Friedman (1944 - ), Matthias Felleisen")
156-
157-
(facts "books->string"
158-
(books->string []) => "No books."
159-
(books->string [cities])
160-
=> "1 book. The City and the City, written by China Miéville (1972 - )."
161-
(books->string [little-schemer, cities, wild-seed])
162-
=> "3 books. The Little Schemer, written by Daniel Friedman (1944 - ), Matthias Felleisen. The City and the City, written by China Miéville (1972 - ). Wild Seed, written by Octavia E. Butler (1947 - 2006).")
163-
164-
(facts "books-by-author"
165-
(books-by-author "China Miéville" books) => [cities, embassytown])
166-
167-
(facts "book-titles-by-author"
168-
(book-titles-by-author "China Miéville" books)
169-
=> ["The City and the City" "Embassytown"]
170-
(book-titles-by-author "Octavia E. Butler" books)
171-
=> ["Wild Seed"])
172-
173-
(facts "author-names"
174-
(author-names [china octavia]) => #{"Octavia E. Butler" "China Miéville"})
175-
176-
(facts "authors"
177-
(authors books) => #{china octavia})
178-
179-
(facts "books->author-names"
180-
(books->author-names books) => #{"Octavia E. Butler" "China Miéville"})
154+
(facts "old-book->new-book"
155+
(old-book->new-book {:title "The Little Schemer"
156+
:authors [friedman, felleisen]})
157+
=> {:title "The Little Schemer" :authors #{friedman, felleisen}}
158+
(old-book->new-book {:title "Wild Seed", :authors [octavia]})
159+
=> {:title "Wild Seed", :authors #{octavia}})
160+
161+
162+
(let [china {:name "China Miéville", :birth-year 1972}
163+
octavia {:name "Octavia E. Butler"
164+
:birth-year 1947
165+
:death-year 2006}
166+
friedman {:name "Daniel Friedman" :birth-year 1944}
167+
felleisen {:name "Matthias Felleisen"}
168+
cities {:title "The City and the City" :authors #{china}}
169+
wild-seed {:title "Wild Seed", :authors #{octavia}}
170+
embassytown {:title "Embassytown", :authors #{china}}
171+
little-schemer {:title "The Little Schemer"
172+
:authors #{friedman, felleisen}}
173+
books [cities, wild-seed, embassytown, little-schemer]]
174+
175+
(facts "has-author?"
176+
(has-author? cities china) => true
177+
(has-author? cities felleisen) => false
178+
(has-author? little-schemer felleisen) => true
179+
(has-author? little-schemer friedman) => true
180+
(has-author? little-schemer octavia) => false)
181+
182+
(facts "authors"
183+
(authors [cities, wild-seed]) => #{china, octavia}
184+
(authors [cities, wild-seed, embassytown]) => #{china, octavia}
185+
(authors [little-schemer, cities]) => #{china, friedman, felleisen})
186+
187+
(facts "all-author-names"
188+
(all-author-names []) => #{}
189+
(all-author-names [cities, wild-seed])
190+
=> #{"China Miéville" "Octavia E. Butler"}
191+
(all-author-names books)
192+
=> #{"Matthias Felleisen" "China Miéville"
193+
"Octavia E. Butler" "Daniel Friedman"})
194+
195+
(facts "author->string"
196+
(author->string felleisen) => "Matthias Felleisen"
197+
(author->string friedman) => "Daniel Friedman (1944 - )"
198+
(author->string octavia) => "Octavia E. Butler (1947 - 2006)")
199+
200+
(facts "authors->string"
201+
(authors->string (:authors little-schemer))
202+
=> (every-checker (contains "Daniel Friedman (1944 - )")
203+
(contains "Matthias Felleisen")
204+
(contains ", "))
205+
(authors->string #{octavia}) => "Octavia E. Butler (1947 - 2006)"
206+
(authors->string #{}) => ""
207+
(authors->string #{octavia, friedman})
208+
=> (every-checker (contains "Octavia E. Butler (1947 - 2006)")
209+
(contains "Daniel Friedman (1944 - )")
210+
(contains ", ")))
211+
212+
(facts "book->string"
213+
(book->string wild-seed) => "Wild Seed, written by Octavia E. Butler"
214+
(book->string little-schemer)
215+
=> (every-checker (has-prefix "The Little Schemer, written by ")
216+
(has-suffix #"Daniel Friedman \(1944 - \), Matthias Felleisen|Matthias Felleisen, Daniel Friedman \(1944 - \)")))
217+
218+
(facts "books->string"
219+
(books->string []) => "No books."
220+
(books->string [cities])
221+
=> "1 book. The City and the City, written by China Miéville (1972 - )."
222+
(books->string [little-schemer, cities, wild-seed])
223+
=> #"3 books. The Little Schemer, written by (Daniel Friedman \(1944 - \), Matthias Felleisen|Matthias Felleisen, Daniel Friedman \(1944 - \)). The City and the City, written by China Miéville \(1972 - \). Wild Seed, written by Octavia E. Butler \(1947 - 2006\).")
224+
225+
(facts "books-by-author"
226+
(books-by-author china books) => (cities embassytown)
227+
(books-by-author octavia books) => (wild-seed))
228+
229+
(facts "book-titles-by-author"
230+
(book-titles-by-author "China Miéville" books)
231+
=> ["The City and the City" "Embassytown"]
232+
(book-titles-by-author "Octavia E. Butler" books)
233+
=> ["Wild Seed"])
234+
235+
(facts "author-names"
236+
(author-names [china octavia]) => #{"Octavia E. Butler" "China Miéville"})
237+
238+
(facts "authors"
239+
(authors books) => #{china octavia})
240+
241+
(facts "books->author-names"
242+
(books->author-names books) => #{"Octavia E. Butler" "China Miéville"}))
181243

182244
; %____%

0 commit comments

Comments
 (0)