Clojure 如何将字符串集连接到一个位置预先指定的字符串中?

Clojure 如何将字符串集连接到一个位置预先指定的字符串中?,clojure,Clojure,假设我有这个 (def base ["one" "two" "three"]) 我想将其转换为: 1. one 2. two 3. three (又名1.1\n2.2\n3.3) 使用join,我不确定能否在加入之前附加计数器: (clojure.string/join " \n" base) => "one \ntwo \nthree" 使用doseq或类似工具,再加上一个atom,我确实得到了单个字符串,但随后将不得不连接起来,比如 (def base ["one" "two"

假设我有这个

(def base ["one" "two" "three"])
我想将其转换为:

1. one
2. two
3. three
(又名
1.1\n2.2\n3.3

使用
join
,我不确定能否在加入之前附加计数器:

(clojure.string/join " \n" base)
=> "one \ntwo \nthree"
使用
doseq
或类似工具,再加上一个atom,我确实得到了单个字符串,但随后将不得不连接起来,比如

(def base ["one" "two" "three"])

(def pos (atom 0))

(defn add-pos
  [base]
  (for [b base]
    (do 
      (swap! pos inc)
      (str @pos ". " b))))

(let [pos-base (add-pos base)]
  (clojure.string/join " \n" pos-base))

=> "1. one \n2. two \n3. three"
虽然它可以工作,但我不知道使用带有
for
语句的atom是否是实现这一点的最佳方法,它看起来不像clojure

请问有没有更好的方法可以做到这一点?

这是一项适合以下人员的工作:


schaueho保持索引的一个次要替代方案是(发现模式?)

很明显,这是一份工作


伟大的看起来clojure有很多功能,我最大的问题是找到/发现这些功能!除此之外,,您可以注册4clojure,然后查看其他人在使用过程中所做的解决方案——这对于核心Clojure的API发现非常有用,并可以查看某些函数的使用时间/位置使用
keep index
对传递给它的lambda返回的每个值运行逻辑真理检查,最终将其忽略。这种检查在这种情况下是完全无用的,因此不必要的开销<代码>地图索引就足够了。
user> (keep-indexed #(str (inc %1) ". " %2) ["one" "two" "three"])
("1. one" "2. two" "3. three")
user> (clojure.string/join "\n"
         (keep-indexed 
            #(str (inc %1) ". " %2) 
            ["one" "two" "three"]))
"1. one\n2. two\n3. three"
(def base ["one" "two" "three"])

(defn numbered-list [s]
  (->> s
       (map-indexed #(str (inc %1) ". " %2))
       (interpose \newline)
       (apply str)))

(numbered-list base) ; => "1. one\n2. two\n3. three"
(->> (interleave (rest (range)) (repeat ". ") base (repeat " \n"))
     (apply str))

;-> "1. one \n2. two \n3. three \n"