pes*_*lla 14 idiomatic clojure
各种Clojure风格指南建议避免使用超过80个字符的行.我想知道是否有一种惯用的方法来避免长String文字.
虽然这些日子通常有宽屏幕,但我仍然同意应该避免长线.
以下是一些例子(我很想跟随第一个例子):
;; break the String literal with `str`
(println (str
    "The quick brown fox "
    "jumps over the lazy dog"))
;; break the String literal with `join`
(println (join " " [
    "The quick brown fox"
    "jumps over the lazy dog"]))
我知道Clojure支持多行String文字,但使用这种方法会有解释换行符的不良影响,例如使用repl:
user=> (println "The quick brown fox
  #_=>   jumps over the lazy dog")
The quick brown fox
  jumps over the lazy dog
您应该将该字符串存储在外部文本文件中,并从您的代码中读取该文件.如果您仍然觉得需要在代码中存储字符串,请继续使用str.
编辑:
根据要求,我将演示如何在编译时读取长字符串.
(defmacro compile-time-slurp [file]
  (slurp file))
像这样使用它:
(def long-string (compile-time-slurp "longString.txt"))
您可以创建类似的宏来处理Java属性文件,XML/JSON配置,SQL查询,HTML或您需要的任何其他内容.