(defn boring-location [a b]
(< (haversine a b) 5))
(defn dedupe-if
([f]
(fn [xf]
(let [prev (volatile! ::none)]
(fn
([] (xf))
([result] (xf result))
([result input]
(let [prior @prev]
(if (and (not= ::none prior) (f prior input))
result
(do
(vreset! prev input)
(xf result input)))))))))
([f coll] (sequence (dedupe-if f) coll)))
(defn remove-boring-locations [locations]
(dedupe-if boring-location locations))
How do I write this better? locations
is a set of GPS coordinates. The intent is to dedupe
when an adjacent GPS coordinate is less than 5km’s away.do you want to recursively dedupe or just one pass?
you can use partition
and mapcat
(def r (range 1 10))
(->> r (partition 2 2)
(mapcat (fn [[a b]] ;;apply haversine here
(if ( < a 5) [a b] [b]))))
but if [a b c d]
are all close , this will generate [a c]
or similar, not [a]
I would name f
something more meaningful.
Except this, the function looks right.
I would also rename xf
to rf