Skip to content

Instantly share code, notes, and snippets.

@uvtc
Created July 19, 2014 02:05
Show Gist options
  • Save uvtc/c1c4cbd6f57c85466e7e to your computer and use it in GitHub Desktop.
Save uvtc/c1c4cbd6f57c85466e7e to your computer and use it in GitHub Desktop.
Best way to find a particular map in a set of them?
#!/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