Created
November 8, 2013 21:34
-
-
Save rcampbell/7378038 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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