Fork me on GitHub
#code-reviews
<
2019-11-28
>
danieroux18:11:37

(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.

jaihindhreddy05:11:58

Looks just fine to me. But dunno why dedupe-if is being called with two args at the bottom there.

mping14:12:35

do you want to recursively dedupe or just one pass?

mping14:12:13

you can use partitionand mapcat

mping14:12:36

(def r (range 1 10))
(->> r (partition 2 2) 
       (mapcat (fn [[a b]] ;;apply haversine here
                  (if ( < a 5) [a b] [b]))))

mping14:12:49

but if [a b c d] are all close , this will generate [a c] or similar, not [a]

Vincent Cantin14:12:52

I would name f something more meaningful. Except this, the function looks right.

Vincent Cantin15:12:24

I would also rename xf to rf