在clojure中的向量列表中编号元素

时间:2014-02-10 20:29:10

标签: list vector clojure

我有一个我要编号的矢量列表。例如,我有以下内容 载体列表

(["blah1" "blah2" "blah3"]
 ["clah1" "clah2" "clah3"]
 ["flah1" "flah2" "flah3"])

并希望这样:

(["line 1" "blah1" "blah2" "blah3"]
 ["line 2" "clah1" "clah2" "clah3"]
 ["line 3" "flah1" "flah2" "flah3"]) 

2 个答案:

答案 0 :(得分:2)

map将多个序列作为最后一个参数:

user=> (map vector (iterate inc 1) '(a b c d))
([1 a] [2 b] [3 c] [4 d])

答案 1 :(得分:2)

map-indexed可以解决问题。

(map-indexed (fn [i v]
               (into [(str "line " (inc i))] v))
             [["blah1" "blah2" "blah3"]
              ["clah1" "clah2" "clah3"]
              ["flah1" "flah2" "flah3"]])
;; => (["line 1" "blah1" "blah2" "blah3"] ["line 2" "clah1" "clah2" "clah3"] ["line 3" "flah1" "flah2" "flah3"])