Created
July 19, 2014 02:05
-
-
Save uvtc/c1c4cbd6f57c85466e7e to your computer and use it in GitHub Desktop.
Best way to find a particular map in a set of them?
This file contains 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
#!/usr/bin/env lein-exec | |
(def people | |
#{{:id 1 :name "Gorf"} | |
{:id 2 :name "Riff"} | |
{:id 3 :name "Raff"} | |
;; ... snip lots more | |
{:id 10000 :name "Grum"}}) | |
;; Quick, find Raff's id! | |
;; Method one: crazy wasteful | |
(let [id (first (filter (fn [person] | |
(= (person :name) "Raff")) | |
people))] | |
(println id)) | |
;; Method two: better | |
(let [id (first | |
(drop-while (fn [person] | |
(not= (person :name) "Raff")) | |
people))] | |
(println id)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment