-
-
Save jneira/1271233 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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