I would appreciate some insights / comments from Clojure regulars out there about my submission here.
(ns phrase)
(require '[clojure.string :as s])
(defn word-array
[phrase]
(-> (s/lower-case phrase)
(s/split #"\W+")))
(defn word-count
[phrase]
(-> (word-array phrase)
(frequencies)))
-
\$\begingroup\$ It seems like a copy of the example code provided in the linked repository. What kind of feedback would you like about it ? \$\endgroup\$omiel– omiel2014年02月18日 18:32:17 +00:00Commented Feb 18, 2014 at 18:32
-
\$\begingroup\$ Well I think it's a little bit shorter than the example. I just reworked my solution until I got the shortest form of it. I just wanted some comments on if this is like succinct clojure code and is it idiomatic. \$\endgroup\$Low Kian Seong– Low Kian Seong2014年02月19日 01:00:36 +00:00Commented Feb 19, 2014 at 1:00
1 Answer 1
I would format it like this:
(ns phrase
(:require [clojure.string :as s]))
(defn word-array [phrase]
(s/split (s/lower-case phrase) #"\W+"))
(defn word-count [phrase]
(frequencies (word-array phrase)))
Notice that I included the require
statement as part of the ns
definition.
Whether or not you use threading macros (->
, ->>
) is generally a matter of personal preference, and there's nothing wrong with using them here, but I think in this case since you're only using 2 functions, I find the above easier to read. You might also consider using comp
:
(def word-array (comp #(s/split % #"\W+") s/lower-case)
(def word-count (comp frequencies word-array))