4
\$\begingroup\$

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)))
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 18, 2014 at 16:31
\$\endgroup\$
2
  • \$\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\$ Commented 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\$ Commented Feb 19, 2014 at 1:00

1 Answer 1

3
\$\begingroup\$

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))
answered Mar 19, 2014 at 13:49
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.