Completed vectors, sets, maps, functions

This commit is contained in:
Artur Meyster 2016-04-06 15:07:59 -07:00
parent e14804ce4f
commit ead7f24c06
4 changed files with 41 additions and 41 deletions

View file

@ -3,31 +3,31 @@
(meditations (meditations
"You can use vectors in clojure as array-like structures" "You can use vectors in clojure as array-like structures"
(= __ (count [42])) (= 1 (count [42]))
"You can create a vector from a list" "You can create a vector from a list"
(= __ (vec '(1))) (= [1] (vec '(1)))
"Or from some elements" "Or from some elements"
(= __ (vector nil nil)) (= [nil, nil] (vector nil nil))
"But you can populate it with any number of elements at once" "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" "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" "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" "And the last in a similar fashion"
(= __ (last [:peanut :butter :and :jelly])) (= :jelly (last [:peanut :butter :and :jelly]))
"Or any index if you wish" "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" "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" "Equality with collections is in terms of values"
(= (list 1 2 3) (vector 1 2 __))) (= (list 1 2 3) (vector 1 2 3)))

View file

@ -4,19 +4,19 @@
(meditations (meditations
"You can create a set by converting another collection" "You can create a set by converting another collection"
(= #{3} (set __)) (= #{3} (set #{3}))
"Counting them is like counting other collections" "Counting them is like counting other collections"
(= __ (count #{1 2 3})) (= 3 (count #{1 2 3}))
"Remember that a set is a *mathematical* set" "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" "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" "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" "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})))

View file

@ -3,48 +3,48 @@
(meditations (meditations
"Don't get lost when creating a map" "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 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" "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" "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" "Maps can be used as functions to do lookups"
(= __ ({:a 1 :b 2} :a)) (= 1 ({:a 1 :b 2} :a))
"And so can keywords" "And so can keywords"
(= __ (:a {:a 1 :b 2})) (= 1 (:a {:a 1 :b 2}))
"But map keys need not be keywords" "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" "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" "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" "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" "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" "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" "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" "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"}))) (sort (keys { 2014 "Sochi" 2018 "PyeongChang" 2010 "Vancouver"})))
"You can get the values in a similar way" "You can get the values in a similar way"
(= (list __ __ __) (= (list "PyeongChang" "Sochi" "Vancouver")
(sort (vals {2010 "Vancouver" 2014 "Sochi" 2018 "PyeongChang"})))) (sort (vals {2010 "Vancouver" 2014 "Sochi" 2018 "PyeongChang"}))))

View file

@ -8,33 +8,33 @@
(meditations (meditations
"Calling a function is like giving it a hug with parentheses" "Calling a function is like giving it a hug with parentheses"
(= __ (square 9)) (= 81 (square 9))
"Functions are usually defined before they are used" "Functions are usually defined before they are used"
(= __ (multiply-by-ten 2)) (= 20 (multiply-by-ten 2))
"But they can also be defined inline" "But they can also be defined inline"
(= __ ((fn [n] (* 5 n)) 2)) (= 10 ((fn [n] (* 5 n)) 2))
"Or using an even shorter syntax" "Or using an even shorter syntax"
(= __ (#(* 15 %) 4)) (= 60 (#(* 15 %) 4))
"Even anonymous functions may take multiple arguments" "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" "Arguments can also be skipped"
(= __ (#(* 15 %2) 1 2)) (= 30 (#(* 15 %2) 1 2))
"One function can beget another" "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" "Functions can also take other functions as input"
(= 20 ((fn [f] (f 4 5)) (= 20 ((fn [f] (f 4 5))
___)) *))
"Higher-order functions take function arguments" "Higher-order functions take function arguments"
(= 25 (___ (= 25 ((fn [aFun] (aFun 5))
(fn [n] (* n n)))) (fn [n] (* n n))))
"But they are often better written using the names of functions" "But they are often better written using the names of functions"
(= 25 (___ square))) (= 25 ((fn [squre] (squre 5)) square)))