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
(defn combinations [n coll] | |
(if (= 1 n) | |
(map list coll) | |
(lazy-seq | |
(when-let [[head & tail] (seq coll)] | |
(concat (for [x (combinations (dec n) tail)] | |
(cons head x)) | |
(combinations n tail)))))) | |
user> (require '[clojure.contrib.combinatorics :as combine]) | |
nil | |
user> (time (last (user/combinations 4 (range 100)))) | |
"Elapsed time: 4379.959957 msecs" | |
(96 97 98 99) | |
user> (time (last (combine/combinations (range 100) 4))) | |
"Elapsed time: 10913.170605 msecs" | |
(96 97 98 99) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment