Set consolidation: Difference between revisions

Content added Content deleted
m (→‎{{header|TXR}}: Drop @(do ...))
Line 1,628: Line 1,628:
Original solution:
Original solution:


<lang txrlisp>(defun mkset (p x) (set [p x] (or [p x] x)))
<lang txr>@(do
(defun mkset (p x) (set [p x] (or [p x] x)))


(defun fnd (p x) (if (eq [p x] x) x (fnd p [p x])))
(defun fnd (p x) (if (eq [p x] x) x (fnd p [p x])))


(defun uni (p x y)
(defun uni (p x y)
(let ((xr (fnd p x)) (yr (fnd p y)))
(let ((xr (fnd p x)) (yr (fnd p y)))
(set [p xr] yr)))
(set [p xr] yr)))


(defun consoli (sets)
(defun consoli (sets)
(let ((p (hash)))
(let ((p (hash)))
(each ((s sets))
(each ((s sets))
(each ((e s))
(each ((e s))
(mkset p e)
(mkset p e)
(uni p e (car s))))
(uni p e (car s))))
(hash-values
(hash-values
[group-by (op fnd p) (hash-keys
[group-by (op fnd p) (hash-keys
[group-by identity (flatten sets)])])))
[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 (consoli test))))</lang>
(format t "~s -> ~s\n" test (consoli test)))</lang>


{{out}}
{{out}}
<pre>((a b) (c d)) -> ((d c) (b a))
<pre>((a b) (c d)) -> ((b a) (d c))
((a b) (b d)) -> ((d b a))
((a b) (b d)) -> ((b a d))
((a b) (c d) (d b)) -> ((d c b a))
((a b) (c d) (d b)) -> ((b a d c))
((h i k) (a b) (c d) (d b) (f g h)) -> ((d c b a) (g f k i h))</pre>
((h i k) (a b) (c d) (d b) (f g h)) -> ((g f k i h) (b a d c)</pre>


{{trans|Racket}}
{{trans|Racket}}


<lang txrlisp>(defun mkset (items) [group-by identity items])
<lang txr>@(do
(defun mkset (items) [group-by identity items])


(defun empty-p (set) (zerop (hash-count set)))
(defun empty-p (set) (zerop (hash-count set)))


(defun consoli (ss)
(defun consoli (ss)
(defun comb (cs s)
(defun combi (cs s)
(cond ((empty-p s) cs)
(cond ((empty-p s) cs)
((null cs) (list s))
((null cs) (list s))
((empty-p (hash-isec s (first cs)))
((empty-p (hash-isec s (first cs)))
(cons (first cs) (comb (rest cs) s)))
(cons (first cs) (combi (rest cs) s)))
(t (consoli (cons (hash-uni s (first cs)) (rest cs))))))
(t (consoli (cons (hash-uni s (first cs)) (rest cs))))))
[reduce-left comb ss nil])
[reduce-left combi ss nil])


;; 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>
[mapcar hash-keys (consoli [mapcar mkset test])]))</lang>


{{out}}
{{out}}
<pre>((a b) (c d)) -> ((b a) (d c))
<pre>((a b) (c d)) -> ((b a) (d c))
((a b) (b d)) -> ((b a d))
((a b) (b d)) -> ((d b a))
((a b) (c d) (d b)) -> ((b a d c))
((a b) (c d) (d b)) -> ((d c b a))
((h i k) (a b) (c d) (d b) (f g h)) -> ((g f k i h) (b a d c))</pre>
((h i k) (a b) (c d) (d b) (f g h)) -> ((g f k i h) (d c b a))</pre>


=={{header|VBScript}}==
=={{header|VBScript}}==