From ead7f24c060df370183d2b07b123e240ab4fdae4 Mon Sep 17 00:00:00 2001 From: Artur Meyster Date: Wed, 6 Apr 2016 15:07:59 -0700 Subject: [PATCH] Completed vectors, sets, maps, functions --- src/koans/04_vectors.clj | 20 ++++++++++---------- src/koans/05_sets.clj | 12 ++++++------ src/koans/06_maps.clj | 30 +++++++++++++++--------------- src/koans/07_functions.clj | 20 ++++++++++---------- 4 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/koans/04_vectors.clj b/src/koans/04_vectors.clj index 71970f6..8232c25 100644 --- a/src/koans/04_vectors.clj +++ b/src/koans/04_vectors.clj @@ -3,31 +3,31 @@ (meditations "You can use vectors in clojure as array-like structures" - (= __ (count [42])) + (= 1 (count [42])) "You can create a vector from a list" - (= __ (vec '(1))) + (= [1] (vec '(1))) "Or from some elements" - (= __ (vector nil nil)) + (= [nil, nil] (vector nil nil)) "But you can populate it with any number of elements at once" - (= [1 __] (vec '(1 2))) + (= [1, 2] (vec '(1 2))) "Conjoining to a vector is different than to a list" - (= __ (conj [111 222] 333)) + (= [111, 222, 333] (conj [111 222] 333)) "You can get the first element of a vector like so" - (= __ (first [:peanut :butter :and :jelly])) + (= :peanut (first [:peanut :butter :and :jelly])) "And the last in a similar fashion" - (= __ (last [:peanut :butter :and :jelly])) + (= :jelly (last [:peanut :butter :and :jelly])) "Or any index if you wish" - (= __ (nth [:peanut :butter :and :jelly] 3)) + (= :jelly (nth [:peanut :butter :and :jelly] 3)) "You can also slice a vector" - (= __ (subvec [:peanut :butter :and :jelly] 1 3)) + (= [:butter, :and] (subvec [:peanut :butter :and :jelly] 1 3)) "Equality with collections is in terms of values" - (= (list 1 2 3) (vector 1 2 __))) + (= (list 1 2 3) (vector 1 2 3))) \ No newline at end of file diff --git a/src/koans/05_sets.clj b/src/koans/05_sets.clj index a6e631f..83bc99d 100644 --- a/src/koans/05_sets.clj +++ b/src/koans/05_sets.clj @@ -4,19 +4,19 @@ (meditations "You can create a set by converting another collection" - (= #{3} (set __)) + (= #{3} (set #{3})) "Counting them is like counting other collections" - (= __ (count #{1 2 3})) + (= 3 (count #{1 2 3})) "Remember that a set is a *mathematical* set" - (= __ (set '(1 1 2 2 3 3 4 4 5 5))) + (= #{1 2 3 4 5} (set '(1 1 2 2 3 3 4 4 5 5))) "You can ask clojure for the union of two sets" - (= __ (set/union #{1 2 3 4} #{2 3 5})) + (= #{1 2 3 4 5} (set/union #{1 2 3 4} #{2 3 5})) "And also the intersection" - (= __ (set/intersection #{1 2 3 4} #{2 3 5})) + (= #{2, 3} (set/intersection #{1 2 3 4} #{2 3 5})) "But don't forget about the difference" - (= __ (set/difference #{1 2 3 4 5} #{2 3 5}))) + (= #{1 4} (set/difference #{1 2 3 4 5} #{2 3 5}))) diff --git a/src/koans/06_maps.clj b/src/koans/06_maps.clj index 3466864..62d2563 100644 --- a/src/koans/06_maps.clj +++ b/src/koans/06_maps.clj @@ -3,48 +3,48 @@ (meditations "Don't get lost when creating a map" - (= {:a 1 :b 2} (hash-map :a 1 __ __)) + (= {:a 1 :b 2} (hash-map :a 1 :b 2)) "A value must be supplied for each key" - (= {:a 1} (hash-map :a __)) + (= {:a 1} (hash-map :a 1)) "The size is the number of entries" - (= __ (count {:a 1 :b 2})) + (= 2 (count {:a 1 :b 2})) "You can look up the value for a given key" - (= __ (get {:a 1 :b 2} :b)) + (= 2 (get {:a 1 :b 2} :b)) "Maps can be used as functions to do lookups" - (= __ ({:a 1 :b 2} :a)) + (= 1 ({:a 1 :b 2} :a)) "And so can keywords" - (= __ (:a {:a 1 :b 2})) + (= 1 (:a {:a 1 :b 2})) "But map keys need not be keywords" - (= __ ({2010 "Vancouver" 2014 "Sochi" 2018 "PyeongChang"} 2014)) + (= "Sochi" ({2010 "Vancouver" 2014 "Sochi" 2018 "PyeongChang"} 2014)) "You may not be able to find an entry for a key" - (= __ (get {:a 1 :b 2} :c)) + (= nil (get {:a 1 :b 2} :c)) "But you can provide your own default" - (= __ (get {:a 1 :b 2} :c :key-not-found)) + (= :key-not-found (get {:a 1 :b 2} :c :key-not-found)) "You can find out if a key is present" - (= __ (contains? {:a nil :b nil} :b)) + (= true (contains? {:a nil :b nil} :b)) "Or if it is missing" - (= __ (contains? {:a nil :b nil} :c)) + (= false (contains? {:a nil :b nil} :c)) "Maps are immutable, but you can create a new and improved version" - (= {1 "January" 2 __} (assoc {1 "January"} 2 "February")) + (= {1 "January" 2 "February"} (assoc {1 "January"} 2 "February")) "You can also create a new version with an entry removed" - (= {__ __} (dissoc {1 "January" 2 "February"} 2)) + (= {1 "January"} (dissoc {1 "January" 2 "February"} 2)) "Often you will need to get the keys, but the order is undependable" - (= (list __ __ __) + (= (list 2010 2014 2018) (sort (keys { 2014 "Sochi" 2018 "PyeongChang" 2010 "Vancouver"}))) "You can get the values in a similar way" - (= (list __ __ __) + (= (list "PyeongChang" "Sochi" "Vancouver") (sort (vals {2010 "Vancouver" 2014 "Sochi" 2018 "PyeongChang"})))) diff --git a/src/koans/07_functions.clj b/src/koans/07_functions.clj index 4bf40a0..4f0b5fe 100644 --- a/src/koans/07_functions.clj +++ b/src/koans/07_functions.clj @@ -8,33 +8,33 @@ (meditations "Calling a function is like giving it a hug with parentheses" - (= __ (square 9)) + (= 81 (square 9)) "Functions are usually defined before they are used" - (= __ (multiply-by-ten 2)) + (= 20 (multiply-by-ten 2)) "But they can also be defined inline" - (= __ ((fn [n] (* 5 n)) 2)) + (= 10 ((fn [n] (* 5 n)) 2)) "Or using an even shorter syntax" - (= __ (#(* 15 %) 4)) + (= 60 (#(* 15 %) 4)) "Even anonymous functions may take multiple arguments" - (= __ (#(+ %1 %2 %3) 4 5 6)) + (= 15 (#(+ %1 %2 %3) 4 5 6)) "Arguments can also be skipped" - (= __ (#(* 15 %2) 1 2)) + (= 30 (#(* 15 %2) 1 2)) "One function can beget another" - (= 9 (((fn [] ___)) 4 5)) + (= 9 ((#(fn [a b] (+ a b))) 4 5)) "Functions can also take other functions as input" (= 20 ((fn [f] (f 4 5)) - ___)) + *)) "Higher-order functions take function arguments" - (= 25 (___ + (= 25 ((fn [aFun] (aFun 5)) (fn [n] (* n n)))) "But they are often better written using the names of functions" - (= 25 (___ square))) + (= 25 ((fn [squre] (squre 5)) square)))