Merge pull request #6 from dlane-latacora/04-05-Vector-Sets

Vector and Sets clj completed
This commit is contained in:
dlane-latacora 2021-05-26 11:53:45 -05:00 committed by GitHub
commit e0894e2788
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 16 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 4 3 2 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})))