Skip to content

Commit

Permalink
[Feature magnars#196] Add -powerset and -permutations (magnars#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
holomorph authored and Fuco1 committed Nov 16, 2016
1 parent 07c61f5 commit dd30a1f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
16 changes: 16 additions & 0 deletions dash.el
Expand Up @@ -1959,6 +1959,22 @@ The test for equality is done with `equal',
or with `-compare-fn' if that's non-nil."
(--filter (not (-contains? list2 it)) list))

(defun -powerset (list)
"Return the power set of LIST."
(if (null list) '(())
(let ((last (-powerset (cdr list))))
(append (mapcar (lambda (x) (cons (car list) x)) last)
last))))

(defun -permutations (list)
"Return the permutations of LIST."
(if (null list) '(())
(apply #'append
(mapcar (lambda (x)
(mapcar (lambda (perm) (cons x perm))
(-permutations (remove x list))))
list))))

(defun -contains? (list element)
"Return non-nil if LIST contains ELEMENT.
Expand Down
9 changes: 9 additions & 0 deletions dev/examples.el
Expand Up @@ -565,6 +565,15 @@ new list."
(-intersection '(1 2 3) '(4 5 6)) => '()
(-intersection '(1 2 3 4) '(3 4 5 6)) => '(3 4))

(defexamples -powerset
(-powerset '()) => '(nil)
(-powerset '(x y z)) => '((x y z) (x y) (x z) (x) (y z) (y) (z) nil))

(defexamples -permutations
(-permutations '()) => '(nil)
(-permutations '(1 2)) => '((1 2) (2 1))
(-permutations '(a b c)) => '((a b c) (a c b) (b a c) (b c a) (c a b) (c b a)))

(defexamples -distinct
(-distinct '()) => '()
(-distinct '(1 2 2 4)) => '(1 2 4)))
Expand Down

0 comments on commit dd30a1f

Please sign in to comment.