Skip to content

Instantly share code, notes, and snippets.

@noidi
Forked from krisajenkins/code.clj
Last active August 29, 2015 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noidi/60b7456d27c3bbf22eb2 to your computer and use it in GitHub Desktop.
Save noidi/60b7456d27c3bbf22eb2 to your computer and use it in GitHub Desktop.
;;; Changes:
;;; - Annotate the parameter of the lambda given to filter. This was the cause of the error.
;;; - Change the last parameter of lookup-by from Seq to Seqable to allow it to work with vectors.
;;; - Replace (IFn [a -> b]) with [a -> b]. They're equivalent.
(t/ann lookup-by (t/All [a b]
[b [a -> b] (t/Option (t/Seqable a)) -> (t/Option a)]))
(defn lookup-by
"Convenience filter. Returns the first item in coll where (= value (lookup-fn item))"
[value lookup-fn coll]
(first (filter (t/fn _ [x :- a]
(= value (lookup-fn x)))
coll)))
; Example
(lookup-by "Kris" :name [{:name "foo"} {:name "Kris"} {:name "bar"}])
@krisajenkins
Copy link

👍 Thanks dude!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment