【发布时间】:2018-01-27 02:10:29
【问题描述】:
我正在编写一个函数,对于任何给定的字符串,用相同数量的“。”替换该字符串中的任何数字。字符。
例子:
AT2X -> AT..X
QW3G45 -> QW...G.......
T3Z1 -> T...Z.
我编写了以下 Clojure 函数,但收到一个我不太明白的错误:
java.lang.ClassCastException: clojure.lang.LazySeq (in module: Unnamed Module) can be case to java.lang.Charsequence
我从错误解释为我需要将惰性序列的评估强制返回到字符串(或 CharSequence),但我不知道在哪里这样做或者这是否正确。
(defn dotify
;;Replaces digits with the same number of '.'s for use in traditional board formats
[FEN]
(let [values (doall (filter isDigit (seq FEN)))]
(fn [values]
(let [value (first values)]
(str/replace FEN value (fn dots [number]
(fn [s times]
(if (> times 0)
(recur (str s ".") (dec times)))) "" (Character/digit number 10)) value))
(recur (rest values))) values))
【问题讨论】:
标签: clojure