Skip to content

Instantly share code, notes, and snippets.

@olauzon
Last active December 24, 2015 03:09
Show Gist options
  • Save olauzon/6735059 to your computer and use it in GitHub Desktop.
Save olauzon/6735059 to your computer and use it in GitHub Desktop.
Ordered paths
(defn ordered-paths
[coll]
(let [len (count coll)
end (dec len)]
(cond
(zero? len) coll
(zero? end) coll
:else
(loop [start 0
curr 1
paths []]
(if (and (= start end) (> curr end)) paths
(do
(recur
(if (= curr end) (inc start) start)
(if (= curr end) (+ start 2) (inc curr))
(conj paths [(nth coll start) (nth coll curr)]))))))))
(ordered-paths [])
;; []
(ordered-paths [4])
;; [4]
(ordered-paths [4 1 2 8 9])
;; [[4 1] [4 2] [4 8] [4 9] [1 2] [1 8] [1 9] [2 8] [2 9] [8 9]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment