Skip to content

Instantly share code, notes, and snippets.

@dakrone
Created September 20, 2010 20:27
Show Gist options
  • Select an option

  • Save dakrone/588587 to your computer and use it in GitHub Desktop.

Select an option

Save dakrone/588587 to your computer and use it in GitHub Desktop.
(ns distance
(:use [clojure.contrib.seq :only [indexed]]))
(defn- min-dist
[index inums]
(apply min (map #(Math/abs (- index %)) inums)))
(defn rel-distance
[indexes elems]
(let [ielems (indexed elems)
idxs (if (vector? indexes) indexes (vector indexes))]
(map #(min-dist (first %) idxs) ielems)))
;; (rel-distance 3 [:a :b :c :d :e])
;; -> (3 2 1 0 1)
;; (rel-distance [3 5] [:a :b :c :d :e :f :g :h])
;; -> (3 2 1 0 1 0 1 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment