Skip to content

Instantly share code, notes, and snippets.

@rcampbell
Created November 8, 2013 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcampbell/7378038 to your computer and use it in GitHub Desktop.
Save rcampbell/7378038 to your computer and use it in GitHub Desktop.
(defn- with-index
"Maps a token's sequential position to its
entry."
[{:keys [count] :as accum} x]
(-> accum
(update-in [:by-index] assoc (inc count) x)
(update-in [:count] inc)))
(defn- with-key
"Maps a token's normalized string-based key
to its entry."
[accum {:keys [token] :as x}]
(let [key (lower-case token)]
(update-in accum [:by-key] assoc key x)))
(defn- index
"Indexes tokens by their sequential position
(:by-index) and their normalized string key
(:by-key)."
[xs]
(reduce (fn [accum x]
(-> accum
(with-index x)
(with-key x)))
{:count 0
:by-index {}
:by-key {}}
xs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment