Hi,

Am 25.12.2008 um 17:24 schrieb wwmorgan:

A better implementation would split the different steps of the program
into separate functions. This increases readability and testability of
the source code, and encourages the reuse of code in new programs.

Yes. One can think of the data flowing through the different
functions being modified as it goes. I also made the experience,
that normalising the input for one function in another one can
drastically reduce complexity in the final worker.

(defn topwords
 "Compute the frequencies of each word in in-filepath. Output the
results to
  out-filepath"
 [in-filepath out-filepath]
   (spit out-filepath
         (str (freq-format (freqs (word-seq in-filepath)))
              "\r\n")))

Here you see this data flow: the output of one function is the
input of another. Clojure has a very nice macro showing this
also visually: ->. (And it saves a lot of parens...) :)

(defn topwords
  "Compute ..."
  [in-filepath out-filepath]
  (let [nl (System/getProperty "lineSeparator")
        result (-> in-filepath word-seq freqs freq-format (str nl))]
    (spit out-filepath result)))

Of course this has no functional effect, just cosmetics.
(Although I replaced the ugly "\r\n"...)

Similar for word-seq:

(defn word-seq
  "Given ..."
  [file]
  (-> file (slurp "\\s+") .toLowerCase .split seq))

Sincerely
Meikel

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to