2014-01-26 00:04:22 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "You can use vectors in clojure as array-like structures"
|
2016-02-27 20:53:16 +00:00
|
|
|
(= 1 (count [42]))
|
2010-02-06 22:00:08 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "You can create a vector from a list"
|
2016-02-27 20:53:16 +00:00
|
|
|
(= [1] (vec '(1)))
|
2010-02-06 22:00:08 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Or from some elements"
|
2016-02-27 20:53:16 +00:00
|
|
|
(= [nil nil] (vector nil nil))
|
2010-10-27 05:04:32 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "But you can populate it with any number of elements at once"
|
2016-02-27 20:53:16 +00:00
|
|
|
(= [1 2] (vec '(1 2)))
|
2010-02-06 22:00:08 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Conjoining to a vector is different than to a list"
|
2013-03-04 23:10:04 +00:00
|
|
|
(= __ (conj [111 222] 333))
|
2010-02-06 22:00:08 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "You can get the first element of a vector like so"
|
2016-02-27 20:53:16 +00:00
|
|
|
(= :peanut (first [:peanut :butter :and :jelly]))
|
2010-02-06 22:00:08 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "And the last in a similar fashion"
|
2016-02-27 20:53:16 +00:00
|
|
|
(= :jelly (last [:peanut :butter :and :jelly]))
|
2010-07-26 23:20:42 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Or any index if you wish"
|
2016-02-27 20:53:16 +00:00
|
|
|
(= :jelly (nth [:peanut :butter :and :jelly] 3)) ;; J - index #'s still start with 0 in Clojure.
|
2010-07-26 23:20:42 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "You can also slice a vector"
|
2016-02-27 20:53:16 +00:00
|
|
|
(= [:butter :and] (subvec [:peanut :butter :and :jelly] 1 3)) ;; J - slicing with subvec includes first index arg and excludes second
|
2010-11-04 13:01:34 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Equality with collections is in terms of values"
|
2016-02-27 20:53:16 +00:00
|
|
|
(= true (list 1 2 3) (vector 1 2 3)) ;; J - lists and vectors are comparable.
|