clojure-koans/src/koans/03_vectors.clj

34 lines
891 B
Clojure
Raw Normal View History

(ns koans.03-vectors
(:require [koan-engine.core :refer :all]))
2010-02-06 22:00:08 +00:00
(meditations
2013-03-04 23:06:46 +00:00
"You can use vectors in clojure as array-like structures"
2010-10-29 15:45:47 +00:00
(= __ (count [42]))
2010-02-06 22:00:08 +00:00
2013-03-04 23:06:46 +00:00
"You can create a vector from a list"
(= __ (vec '(1)))
2010-02-06 22:00:08 +00:00
2013-03-04 23:06:46 +00:00
"Or from some elements"
(= __ (vector nil 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
2013-03-04 23:10:04 +00:00
"Conjoining to a vector is different than to a list"
(= __ (conj [111 222] 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"
2010-11-04 13:01:34 +00:00
(= __ (subvec [:peanut :butter :and :jelly] 1 3))
"Equality with collections is in terms of values"
(= (list 1 2 3) (vector 1 2 __)))