-2
\$\begingroup\$

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
(defn str-addr [addr]
  "Turn a map of address into a string in the format of STREET, CITY, STATE"
  (let [{:keys [street-address city state]} addr]
    (str street-address ", " city ", " state)
    )
  )
(defn prefix-name-to-addr [[fn ln] addr]
  "Prepend names to an address."
  (let [
        name-string (str fn " " ln)
        addr-string (str-addr addr)
        ]
    (str name-string ", " addr-string)
    )
  )
; (prefix-name-to-addr ["Victoria" "Leong"] test-address)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(def test-address
  {:street-address "123 Test Lane"
   :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"
   (prefix-name-to-addr ["Test" "Testerson"] test-address))
\$\endgroup\$
2
\$\begingroup\$

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)

(defn format-address [address]
  "Turn an address into a string in the format of 'STREET, CITY, STATE'."
  (join ", " (map address [:street-address :city :state])))

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

(defn format-full-address [name address]
  "Prepend a name to an address."
  (join ", " [(format-name name) (format-address address)]))

(def test-address
  {:street-address "123 Test Lane"
   :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"
   (format-full-address ["Test" "Testerson"] test-address))
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.