Skip to content

Instantly share code, notes, and snippets.

@logaan
Last active August 29, 2015 14:20
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 logaan/9eea9c65d6cd655e1242 to your computer and use it in GitHub Desktop.
Save logaan/9eea9c65d6cd655e1242 to your computer and use it in GitHub Desktop.
(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