Set consolidation: Difference between revisions
Content added Content deleted
(→{{header|TXR}}: Translation of Racket added.) |
(→{{header|TXR}}: Argh; pasted the wrong code. Fixed now.) |
||
Line 1,450: | Line 1,450: | ||
<lang txr>@(do |
<lang txr>@(do |
||
(defun mkset ( |
(defun mkset (items) [group-by identity items]) |
||
(defun |
(defun empty-p (set) (zerop (hash-count set))) |
||
(defun |
(defun consoli (ss) |
||
( |
(defun comb (cs s) |
||
( |
(cond ((empty-p s) cs) |
||
⚫ | |||
((empty-p (hash-isec s (first cs))) |
|||
(defun consoli (sets) |
|||
( |
(cons (first cs) (comb (rest cs) s))) |
||
( |
(t (consoli (cons (hash-uni s (first cs)) (rest cs)))))) |
||
[reduce-left comb ss nil]) |
|||
(mkset p e) |
|||
⚫ | |||
(hash-values |
|||
[group-by (op fnd p) (hash-keys |
|||
[group-by identity (flatten sets)])]))) |
|||
;; tests |
;; tests |
||
(each ((test '(((a b) (c d)) |
(each ((test '(((a b) (c d)) |
||
((a b) (b d)) |
((a b) (b d)) |
||
((a b) (c d) (d b)) |
((a b) (c d) (d b)) |
||
((h i k) (a b) (c d) (d b) (f g h))))) |
((h i k) (a b) (c d) (d b) (f g h))))) |
||
(format t "~s -> ~s\n" test |
(format t "~s -> ~s\n" test |
||
[mapcar hash-keys (consoli [mapcar mkset test])])))</lang> |
|||
Output: |
Output: |