在clojure中创建具有可变数量空格的字符串

时间:2013-08-23 11:00:03

标签: clojure

我在clojure中创建一个字符串时遇到问题,我需要插入一系列空格,具体取决于其中一个字符串变量的长度 - book。

(defn blanks [book]
    (let [ x(count book)]
    (cond 
            (= "4" x) "    "
            (= "5" x) "     "
            (= "6" x ) "      "
)))

(defn Key1 [x date book bookid]
    (cond 
            (= "AB.LN.TUV" x) (str date".                  AB.LN.  TUV. JKL.      FOO.           FOO.   GRAVITY.       "book".book."(str (blanks book)) bookid)
            (= "DEF.NY.ZXY" x) (str date". DEF.NY. ZXY. JKL.     .          .QPR.             "book".  POS.        "book"."bookid)
            (= "DEF.LN.TUV" x ) (str date".                DEF.LN.  TUV. JKL.      FOO.           FOO.   GRAVITY.       "book".book."blanks bookid)
))



(defn ShowSelectedParams [& props] 
    (let [
              entity "AB.LN.TUV"


              book "ABCD"
              date "21030823"
              bookid "1234abcd"
              ]

              (Key1 entity date book bookid)
))

然而,这将返回以下内容,不带空格:

“21030823.AB.LN. TUV.JKL.FOO.FOO.GRAVITY.ABCD。 book.1234abcd

我想得到的是:

“21030823.AB.LN. TUV.JKL.FOO.FOO.GRAVITY.ABCD。* strong text * book.1234abcd”

对此非常感谢的任何帮助

1 个答案:

答案 0 :(得分:1)

这是试图让它更具惯用性

我将更标准的clojure样式应用于格式化,根据计数生成空白数而不是对字符串进行硬编码,并用case语句替换重复的等式检查。

(defn blanks
  [book]
  (apply str (repeat (count book) \space)))


(defn Key1
  [x date book bookid]
  (case x
    "AB.LN.TUV" (str date
                     ".AB.LN.TUV.JKL.FOO.FOO.GRAVITY."
                     book ".book."
                     (blanks book)
                     bookid)
    "DEF.NY.ZXY" (str date
                      ".DEF.NY.ZXY.JKL...QPR."
                      book
                      ".POS."
                      book
                      "."
                      bookid)
    "DEF.LN.TUV" (str date
                      ".DEF.LN.TUV.JKL.FOO.FOO.GRAVITY."
                      book
                      ".book."
                      blanks
                      bookid)))

(defn ShowSelectedParams
  [& props] 
  (let [entity "AB.LN.TUV"
        book "ABCD"
        date "21030823"
        bookid "1234abcd"]
    (Key1 entity date book bookid)))