I regularly write: (some #(when (pred %) %) ...)
and often mistakingly write: (some #(pred %) ...)
instead. I think it would be worth having (some #(when (pred %) %) ...)
as a built-in in clojure.core.
Suggested name: first-by
. Feel free to suggest other names, I'll list them here.
(first-by #(= (:id %) 2) [{:id 1} {:id 2} {:id 3}])
;;=> {:id 2}
Statistics, in my local .m2 dir, I find 198 (some #(when ...) ..)
forms and 1503 (some #(foo ...) ...)
forms where foo is not when.
Total some + fn usage: 1701 of which 11% is of the some + when form.
Program that found these usages:
(ns grasp
(:require
[clojure.spec.alpha :as s]
[grasp.api :as g]))
(s/def ::some+when
(s/cat :some #{'some}
:fn (s/spec (s/cat :fn #{'fn 'fn*}
:args vector?
:when (s/spec (s/cat :when #{'when} :whatever (s/* any?)))))
:coll any?))
(defn keep-fn [{:keys [spec expr uri]}]
(let [conformed (s/conform spec expr)]
(when-not (s/invalid? conformed)
{:expr expr
:uri uri})))
(defn -main [& args]
(let [classpath (first args)
matches (g/grasp classpath ::some+when {:keep-fn keep-fn})]
(prn (count matches))))
{:deps {io.github.borkdude/grasp {:mvn/version "0.0.3"}}}
On grep.app I find about 8% of some usage of the some+when shape.