clojure-koans/src/koans/vectors.clj

31 lines
789 B
Clojure
Raw Normal View History

2010-02-06 22:00:08 +00:00
(meditations
2010-07-26 23:20:42 +00:00
"You can use vectors in clojure to create an 'Array' like structure"
2010-10-29 15:45:47 +00:00
(= __ (count [42]))
2010-02-06 22:00:08 +00:00
"You can create a vector in several ways"
(= __ (vec nil))
2010-02-06 22:00:08 +00:00
2010-07-26 23:20:42 +00:00
"And populate it in either of these ways"
(= __ (vec '(1)))
2010-02-06 22:00:08 +00:00
"There is another way as well"
(= __ (vector nil))
2010-07-26 23:20:42 +00:00
"But you can populate it with any number of elements at once"
(= [1 __] (vec '(1 2)))
2010-02-06 22:00:08 +00:00
2010-07-26 23:20:42 +00:00
"And add to it as well"
(= __ (conj (vec nil) 333))
2010-02-06 22:00:08 +00:00
2010-07-26 23:20:42 +00:00
"You can get the first element of a vector like so"
(= __ (first [:peanut :butter :and :jelly]))
2010-02-06 22:00:08 +00:00
2010-07-26 23:20:42 +00:00
"And the last in a similar fashion"
(= __ (last [:peanut :butter :and :jelly]))
"Or any index if you wish"
(= __ (nth [:peanut :butter :and :jelly] 3))
"You can also slice a vector"
(= __ (subvec [:peanut :butter :and :jelly] 1 3)))