Last active
August 29, 2015 14:20
-
-
Save logaan/9eea9c65d6cd655e1242 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
(ns permutations | |
(:require [midje.sweet :refer :all])) | |
(defn permute | |
([in] | |
(set (permute [] in))) | |
([concrete possible-next] | |
(mapcat | |
(fn [n] | |
(let [new-next (disj possible-next n) | |
new-concrete (conj concrete n)] | |
(if (empty? new-next) | |
[new-concrete] | |
(permute new-concrete new-next)))) | |
possible-next))) | |
(fact (permute #{1}) | |
=> #{[1]}) | |
(fact (permute #{1 2}) | |
=> #{[1 2] | |
[2 1]}) | |
(fact (permute #{1 2 3}) | |
=> #{[1 2 3] | |
[1 3 2] | |
[2 1 3] | |
[2 3 1] | |
[3 2 1] | |
[3 1 2]}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment