Skip to content

Instantly share code, notes, and snippets.

@jneira
Forked from marick/gist:1270755
Created October 7, 2011 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jneira/1271233 to your computer and use it in GitHub Desktop.
Save jneira/1271233 to your computer and use it in GitHub Desktop.
;; I cant resist paste my version writing bootom-up through interactive driven devlopment ;-)
;; the origin of the puzzle is http://swizec.com/blog/functional-isnt-always-better/swizec/2591
(defn tails [xs]
(take-while seq (iterate rest xs)))
(defn pairs [xs]
(mapcat (fn [[x & y]] (map list (repeat x) y))
(tails xs)))
;; some vesions in comments in the blog post
(defn pairs [xs]
(distinct
(for [x xs
y xs :when (not (= x y))]
(sort [x y]))))
;; another like mine but better
(defn pairs [xs]
(mapcat (partial map vector) (map repeat xs) (rest (tails xs))))
;; another one using clojure.contrib
(defn pairs [xs]
(combinations xs 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment