简体   繁体   English

Clojure中的向量列表中的元素编号

[英]Numbering elements in a list of vectors in clojure

I have a list of vectors I want to number. 我有一个要编号的向量列表。 For instance, I have the following list of vectors 例如,我有以下向量列表

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

and would like something this: 并想要这样的东西:

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

map takes several sequences as last argument: map采用几个序列作为最后一个参数:

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

map-indexed can do the trick. 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"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM