branch: externals/dash commit dd30a1fbbae0201cfd0b861e3150ba6ea5d500fd Author: holomorph <mvote...@udel.edu> Commit: Matus Goljer <dota.k...@gmail.com>
[Feature #196] Add -powerset and -permutations (#203) --- dash.el | 16 ++++++++++++++++ dev/examples.el | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/dash.el b/dash.el index 27dd2c1..a094e41 100644 --- a/dash.el +++ b/dash.el @@ -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. diff --git a/dev/examples.el b/dev/examples.el index a35ae33..092e57e 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -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)))