Let's say I have a set of strings, and I want to return all strings longer than 4 characters, first sorted by shortest string.
You can solve this with something like:
(def strings ["this" "is" "super" "cool" "everybody" "isn't" "clojure" "great!?!?"]) (sort-by count < (filter #(> (count %) 4) strings)) ;; > ("super" "isn't" "clojure" "everybody" "great!?!?")
Note that we use count twice. This is probably good, but what if count not count ? What if, instead of count we call super-expensive-function , that we really wonβt run more than is absolutely necessary?
So:
- We have a collection of things
- We want to return an ordered collection of things
- Filtered and sorted using a computationally expensive function that should only be called once per piece
Is there an existing function that does this, or do I need to create my own?
source share