I need to generate a list of all possible American zipcodes, MD5-
digested. Later on, I will need to do much more involving stuff,
processor-wize, with this. But already, generating a naive list of all
possible zipcodes is taking quite a deal of time:
user> (time (dorun (take 1000000 (all-zips))))
"Elapsed time: 3294.478 msecs"
nil
user> (time (dorun (take 1000000 (all-zips-MD5))))
"Elapsed time: 28150.076 msecs"
nil
If I allow the whole all-zips to be realized, it takes nearly 1 hour.
I have tried a few optimizations, but before proceeding any further
with it, I wondered if you'd have any advice re: idiomatic clojure and
optimization.
Here's my code:
(defn all-zips-MD5 []
"Returns a lazy list of all possible American zipcodes, as MD5
digests."
(let [digester (java.security.MessageDigest/getInstance "MD5")]
(map
(fn [to-digest]
(.update digester (.getBytes to-digest))
(String. (.digest digester)))
(all-zips))))
(defn all-zips []
"Returns a lazy list of all possible American zipcodes."
(let [zips5 (zip-formatted-range 5)
zips4 (zip-formatted-range 4)
cross-prod-zips4 (fn [zip5] (map #(str zip5 "-" %) zips4))
zips5-4 (mapcat cross-prod-zips4 zips5)]
(concat zips5 zips5-4)))
(defn zip-formatted-range [n-digits]
"Returns a lazy list of stringified numbers, which are pre-padded
with zeroes, starting from 0 and going up to the max number that can
be represented with a number of digits of n-digits. Thus, if n-digits
is 3, it will produce [001, 002, ... 999]."
(let [zip-formatter #(format (str "%0" n-digits "d") %)
n-to-format (Math/pow 10 (dec n-digits))
n-max (Math/pow 10 n-digits)]
(concat (map zip-formatter (take n-to-format (range n-max)))
(map str (drop n-to-format (range n-max))))))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---