Zuriar
Zuriar

Reputation: 11734

Conj to clojure vector if vector size is less than a certain amount

I have three vectors:

(def v1 ["one" "two" "three"])
(def v2 ["one" "two" "three" "four"])
(def v3 ["one" "two" "three" "four" "five"])

I need to ensure that the size of the vectors is always 5 elements. I want a function to pass each to which will check the size and conj empty strings as placeholders for when the size is less than 5 such that I get:

(def new-v1 ["one" "two" "three" "" ""])
(def new-v2 ["one" "two" "three" "four" ""])
(def new-v3 ["one" "two" "three" "four" "five"])

Thanks.

Upvotes: 0

Views: 137

Answers (1)

leetwinski
leetwinski

Reputation: 17859

you can make a lazy seq of your vector ++ repeated empty string, and take 5 elements from it:

(defn process [items]
  (into [] (take 5 (concat items (repeat "")))))

user> (process ["a" "b"])
["a" "b" "" "" ""]

Upvotes: 3

Related Questions