0

I'm having trouble creating a string in clojure, I need to insert a series of blank spaces depending on the length on one of the string variables - 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)
))

However this returns the following, without the spaces:

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

What I want to get is:

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

Any help with this much appreciated

1
  • 1
    Nevermind, just realised I had the cond checking for string not numbers! DOH!!If anyone knows a more clojuresqu way of doing this though I would be very interested.. Commented Aug 23, 2013 at 11:02

1 Answer 1

2

this is an attempt to make it a bit more idiomatic

I applied a more standard clojure style to the formatting, generated the number of blanks based on the count rather than hard coding the strings, and replaced the repeated equality checks with a case statement.

(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)))
Sign up to request clarification or add additional context in comments.

1 Comment

I think this still isn't right, for example putting the function blanks in the result string of the third case is probably an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.