diff --git a/04_vectors.clj b/04_vectors.clj index 2c87c4e..5d5ed27 100644 --- a/04_vectors.clj +++ b/04_vectors.clj @@ -1,30 +1,30 @@ ; "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)) ; "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)) ;; J - index #'s still start with 0 in Clojure. ; "You can also slice a vector" - (= __ (subvec [:peanut :butter :and :jelly] 1 3)) + (= [:butter :and] (subvec [:peanut :butter :and :jelly] 1 3)) ;; J - slicing with subvec includes first index arg and excludes second ; "Equality with collections is in terms of values" - (= (list 1 2 3) (vector 1 2 __)) + (= true (list 1 2 3) (vector 1 2 3)) ;; J - lists and vectors are comparable. diff --git a/05_sets.clj b/05_sets.clj index 744cd4d..63f920b 100644 --- a/05_sets.clj +++ b/05_sets.clj @@ -1,18 +1,18 @@ ; "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))) ;; J - This creates a unique ORDERED set so it's always sorted. ; "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})) ;; J - Uniting sets still orders them and keeps elements unique ; "And also the intersection" - (= __ (set/intersection #{1 2 3 4} #{2 3 5})) + (= #{2 3} (set/intersection #{1 2 3 4} #{2 3 5})) ;; J - Creates a new set of common elements. ; "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/06_maps.clj b/06_maps.clj index b9f8b61..1e745cb 100644 --- a/06_maps.clj +++ b/06_maps.clj @@ -1,47 +1,49 @@ ; "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)) ;; J - Hash maps are key-value pairs defined as a linear + ;; sequence. {key value key2 value2} etc. ; "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)) ;; J - This is a bit confusing, but it essentially amounts to a keyword placed + ;; on either side of a hash-map returning the key-word's matching value, the difference + ;; being that the left object is called as a function on the right. ; "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)) ;; J - This caused a considerable delay in the REPL. ; "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)) ;; J - The third argument for get is the default. ; "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)) ;; J - This can take any number of keys to dissociate. ; "Often you will need to get the keys, but the order is undependable" - (= (list __ __ __) - (sort (keys { 2014 "Sochi" 2018 "PyeongChang" 2010 "Vancouver"}))) - + (= (list 2010 2014 2018) + (sort (keys { 2014 "Sochi" 2018 "PyeongChang" 2010 "Vancouver"}))) ;; J - If you don't sort these, they are + ;; essentially returned in random order. ; "You can get the values in a similar way" - (= (list __ __ __) - (sort (vals {2010 "Vancouver" 2014 "Sochi" 2018 "PyeongChang"}))) + (= (list "PyeongChang" "Vancouver" "PyeongChang") + (sort (vals {2010 "Vancouver" 2014 "Sochi" 2018 "PyeongChang"}))) ;; J - Sorts numerically or alphabetically