2016-02-29 87 views
0

我有三個載體:連詞到Clojure的矢量,如果矢量大小小於一定量

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

我需要確保該矢量的大小始終是5個元素。我希望有一個功能,通過每個到會檢查大小和連詞空字符串作爲佔位符當尺寸小於5這樣,我得到:

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

感謝。

回答

3

,你可以讓你的載體++重複空字符串的懶惰序列,並採取5種元素,從它:

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

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

是完美!謝謝 – Zuriar