Let's say I have a collection of strings, and I want to return all strings over 4 characters long, sorted by shortest string first.
You could solve that with something along the lines of:
(def strings ["this" "is" "super" "cool" "everybody" "isn't" "clojure" "great!?!?"])
(sort-by count < (filter #(> (count %) 4) strings))
;; > ("super" "isn't" "clojure" "everybody" "great!?!?")
Notice we're using count
twice. This is probably fine here, but what if count
wasn't count
? What if instead of count
we called super-expensive-function
that we'd really rather not run more than absolutely necessary?
So:
Is there an existing function that does this, or do I need to build my own?
The simplest solution would be to pair up each item together with its expensive-to-compute property, then filter and sort, then discard the parameter:
(->> strings
(map (juxt identity count))
(filter (fn [[_ c]] (> c 4)))
(sort-by peek)
(map first))
If computing the property in question is really all that expensive, the overhead of allocating the vectors should pretty much vanish.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With