HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Exercism assignment for word-count in Clojure

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
clojureassignmentwordforcountexercism

Problem

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)))

Solution

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))

Code Snippets

(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)))
(def word-array (comp #(s/split % #"\W+") s/lower-case)
(def word-count (comp frequencies word-array))

Context

StackExchange Code Review Q#41978, answer score: 3

Revisions (0)

No revisions yet.