A general rule of thumb is to prefer higher-order functions to explicit
recursion. When you are trying to accumulate something (here, you are
accumulating a list of zeroes and ones), you can usually use reduce. This
is a little bit more tricky here as you need to keep track of which bits of
the input string you have consumed as well as your accumulator:
(defn days-to-numbers
[day-string]
(first (reduce (fn [[accum day-string] day]
(let [next-day-string (clojure.string/replace day-string
day "")]
[(conj accum (if (= day-string next-day-string) 0 1))
next-day-string]))
['() day-string]
["SU" "S" "F" "TH" "W" "T" "M"])))
We'd have to work a bit harder if we weren't so lucky with the order the
two-character days appear above, as we *must* inspect the string for SU
before S and TH before T. But here we can get away with it.
Equivalently, but with an explicit loop/recur:
(defn days-to-numbers
[day-string]
(loop [day-string day-string days ["SU" "S" "F" "TH" "W" "T" "M"] accum
'()]
(if-let [day (first days)]
(let [next-day-string (clojure.string/replace day-string day "")]
(recur next-day-string
(rest days)
(conj accum (if (= day-string next-day-string) 0 1))))
accum)))
Another note on your code, you'll commonly see the thread-first macro used
to avoid nested calls to functions like clojure.string/replace; if you
(require '[clojure.string :as str]) you could then write:
(-> day-string (str/replace "SU" "N") (str/replace "TH" "R"))
Ray.
On 22 September 2014 19:45, J David Eisenberg <[email protected]>
wrote:
> As part of a larger program, I'm testing a function that will turn a
> string of days on which a class occurs (such as "MWF") into a list of seven
> numbers: (1 0 1 0 1 0 0).
> I first translate"TH" (Thursday) to "R" and "SU" (Sunday) to "N" to make
> things a bit easier.
>
> I came up with the following code:
>
> (defn days-number-maker
> "Recursively compare first item in days of week with
> first item in string of days. If matching, add a 1,
> else add a zero to the result"
> [all-days day-string result]
> (if (empty? all-days) (reverse result)
> (if (= (first all-days) (first day-string))
> (recur (rest all-days)(rest day-string) (conj result 1))
> (recur (rest all-days) day-string (conj result 0)))))
>
> (defn days-to-numbers
> "Change string like MTTH to (1 1 0 1 0 0 0)"
> [day-string]
> (let [days (clojure.string/replace
> (clojure.string/replace day-string #"TH" "R") #"SU" "N")]
> (days-number-maker "MTWRFSN" days (list))))
>
> The good news: the code works. The bad news: I'm convinced I'm doing it
> wrong, in the moral purity sense of the word. Something inside of me says,
> "You could have just used (map...) to do this the *right* way," but I can't
> see how to do it with (map). So, my two questions are:
>
> 1) Is there such a thing as "the Clojure way," and if so,
> 2) How can I rewrite the code to be more Clojure-ish?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.