# Clojure koans 14 - converting a map to a string

The koan is at the end of this snippet. It requires you to write a function to convert a map to a string, and then prepend a full name to it.

The following solution works fine. I am just wondering if there are simpler or more efficient ways of doing the same thing.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Solution
"Turn a map of address into a string in the format of STREET, CITY, STATE"
(str street-address ", " city ", " state)
)
)
(let [
name-string (str fn " " ln)
]
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

:city "Testerville"
:state "TX"})

"All together now! Fill in a function to make the follow expression eval to true."
(= "Test Testerson, 123 Test Lane, Testerville, TX"


Performance is not an issue here - there is just not enough to do to make that a concern. This review is also overkill for the same reason, so I'll talk about what should be changed should you want to use this code in the context of a larger program than about just 20 lines. And my naming suggestions may not be idiomatic Clojure, I wouldn't know.

So, for style, the first thing to note is that parentheses shouldn't be on their own like this. If your editor makes it difficult, get one which will automatically keep the structure correct and indents properly.

Next, the docstring to str-addr is missing a full stop and the single string ("All together now! ..") is above 80 characters per line.

I don't like abbreviations. addr is a bit shorter than address, but really, str-addr is not the easiest to decipher.

Now what that function does, is not really turning an address into a string (not a map of an address, an address in this program is implicitely defined to be a map with (at least) the three fields street-address, city, state, so treat it like a data type), but rather formatting it for human display. So better names would be format-address, print(able)-address, readable-address, or so.

Instead of the destructuring, you can use map or even juxt to directly extract the fields. That saves another line in that function at the cost of using keywords to specify the fields.

Then, use join from clojure.string to join strings with a separator, much cleaner if you have more parameters. If you have a more complex format, use format.

prefix-name-to-address could suggest that it is turning a prefix-name into an address - I'd probably use a different name altogether. What it does is formatting a full address line, so something like format-full-address. Also, your name data type, a vector, is very limited here. At least by not restricting the number of names a person can have, you get more flexible and at the same time smaller code.

So, if I combine these suggestions into code, it would look like the following:

(use 'clojure.string)

"Turn an address into a string in the format of 'STREET, CITY, STATE'."

(defn format-name [name]
"Turn a name (a sequence of first name to last name) into a string."
(join " " names))