Set consolidation: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|J}}: restore proper handling of consolidate 'ab';'bd')
m (syntax highlighting fixup automation)
Line 28:
We start with specifying a generic package Set_Cons that provides the neccessary tools, such as contructing and manipulating sets, truning them, etc.:
 
<langsyntaxhighlight Adalang="ada">generic
type Element is (<>);
with function Image(E: Element) return String;
Line 54:
type Set is array(Element) of Boolean;
 
end Set_Cons;</langsyntaxhighlight>
 
Here is the implementation of Set_Cons:
 
<langsyntaxhighlight Adalang="ada">package body Set_Cons is
 
function "+"(E: Element) return Set is
Line 134:
end Image;
 
end Set_Cons;</langsyntaxhighlight>
 
Given that package, the task is easy:
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Set_Cons;
 
procedure Set_Consolidation is
Line 177:
Ada.Text_IO.Put_Line
(Image(Consolidate((H+I+K) & (A+B) & (C+D) & (D+B) & (F+G+H))));
end Set_Consolidation;</langsyntaxhighlight>
 
{{out}}
Line 186:
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">display(list l)
{
for (integer i, record r in l) {
Line 240:
 
0;
}</langsyntaxhighlight>
{{out}}
<pre>{A, B}, {C, D}
Line 248:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">SetConsolidation(sets){
arr2 := [] , arr3 := [] , arr4 := [] , arr5 := [], result:=[]
; sort each set individually
Line 293:
result[i, A_Index] := k
return result
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">test1 := [["A","B"], ["C","D"]]
test2 := [["A","B"], ["B","D"]]
test3 := [["A","B"], ["C","D"], ["D","B"]]
Line 312:
}
MsgBox % RTrim(result, "`n[")
return</langsyntaxhighlight>
{{out}}
<pre>[["A","B"] , ["C","D"]]
Line 320:
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">( ( consolidate
= a m z mm za zm zz
. ( removeNumFactors
Line 347:
& test$(A+B C+D D+B)
& test$(H+I+K A+B C+D D+B F+G+H)
);</langsyntaxhighlight>
{{out}}
<pre>A+B C+D ==> A+B C+D
Line 362:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
#define s(x) (1U << ((x) - 'A'))
Line 398:
puts("\nAfter:"); show_sets(x, consolidate(x, len));
return 0;
}</langsyntaxhighlight>
 
The above is O(N<sup>2</sup>) in terms of number of input sets. If input is large (many sets or huge number of elements), here's an O(N) method, where N is the sum of the sizes of all input sets:
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 502:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Collections.Generic;
Line 611:
foreach (var set in currentSets) yield return set.Select(value => value);
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 644:
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(defn consolidate-linked-sets [sets]
(apply clojure.set/union sets))
 
Line 661:
(recur (remove-used (rest seeds) linked)
(conj (remove-used sets linked)
(consolidate-linked-sets linked)))))))</langsyntaxhighlight>
 
{{out}}
Line 675:
=={{header|Common Lisp}}==
{{trans|Racket}}
<langsyntaxhighlight lang="lisp">(defun consolidate (ss)
(labels ((comb (cs s)
(cond ((null s) cs)
Line 682:
(cons (first cs) (comb (rest cs) s)))
((consolidate (cons (union s (first cs)) (rest cs)))))))
(reduce #'comb ss :initial-value nil)))</langsyntaxhighlight>
 
{{Out}}
Line 696:
=={{header|D}}==
{{trans|Go}}
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.array;
 
dchar[][] consolidate(dchar[][] sets) @safe {
Line 724:
[['H','I','K'], ['A','B'], ['C','D'],
['D','B'], ['F','G','H']].consolidate.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>["AB", "CD"]
Line 732:
 
'''Recursive version''', as described on talk page.
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.array;
 
dchar[][] consolidate(dchar[][] sets) @safe {
Line 763:
[['H','I','K'], ['A','B'], ['C','D'],
['D','B'], ['F','G','H']].consolidate.writeln;
}</langsyntaxhighlight>
<pre>["AB", "CD"]
["ABD"]
Line 770:
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
;; utility : make a set of sets from a list
(define (make-set* s)
Line 795:
→ { { a b c d } { f g h i k } }
</syntaxhighlight>
</lang>
 
=={{header|Egison}}==
 
<langsyntaxhighlight lang="egison">
(define $consolidate
(lambda [$xss]
Line 810:
 
(test (consolidate {{'H' 'I' 'K'} {'A' 'B'} {'C' 'D'} {'D' 'B'} {'F' 'G' 'H'}}))
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="egison">
{"DBAC" "HIKFG"}
</syntaxhighlight>
</lang>
 
=={{header|Ela}}==
This solution emulate sets using linked lists:
<langsyntaxhighlight lang="ela">open list
 
merge [] ys = ys
Line 828:
where conso xs [] = xs
conso (x::xs)@r (y::ys) | intersect x y <> [] = conso ((merge x y)::xs) ys
| else = conso (r ++ [y]) ys</langsyntaxhighlight>
Usage:
<langsyntaxhighlight lang="ela">open monad io
 
:::IO
Line 838:
putLn x
y <- return $ consolidate [['A','B'], ['B','D']]
putLn y</langsyntaxhighlight>
 
Output:<pre>[['K','I','F','G','H'],['A','C','D','B']]
Line 844:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule RC do
def set_consolidate(sets, result\\[])
def set_consolidate([], result), do: result
Line 866:
IO.write "#{inspect sets} =>\n\t"
IO.inspect RC.set_consolidate(sets)
end)</langsyntaxhighlight>
 
{{out}}
Line 881:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let (|SeqNode|SeqEmpty|) s =
if Seq.isEmpty s then SeqEmpty
else SeqNode ((Seq.head s), Seq.skip 1 s)
Line 906:
[["H";"I";"K"]; ["A";"B"]; ["C";"D"]; ["D";"B"]; ["F";"G";"H"]]
]
0</langsyntaxhighlight>
{{out}}
<pre>seq [set ["C"; "D"]; set ["A"; "B"]]
Line 914:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: arrays kernel sequences sets ;
 
: comb ( x x -- x )
Line 923:
] if ;
 
: consolidate ( x -- x ) { } [ comb ] reduce ;</langsyntaxhighlight>
{{out}}
<pre>
Line 939:
=={{header|Go}}==
{{trans|Python}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 994:
}
return true
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,002:
=={{header|Haskell}}==
 
<langsyntaxhighlight Haskelllang="haskell">import Data.List (intersperse, intercalate)
import qualified Data.Set as S
 
Line 1,027:
 
showSet :: S.Set Char -> String
showSet = flip intercalate ["{", "}"] . intersperse ',' . S.elems</langsyntaxhighlight>
 
{{Out}}
Line 1,037:
=={{header|J}}==
 
<langsyntaxhighlight Jlang="j">consolidate=:4 :0/
b=. y 1&e.@e.&> x
(1,-.b)#(~.;x,b#y);y
)</langsyntaxhighlight>
 
In other words, fold each set into a growing list of consolidated sets. When there's any overlap between the newly considered set (<code>x</code>) and any of the list of previously considered sets (<code>y</code>), merge the unique values from all of those into a single set (any remaining sets remain as-is). Here, <code>b</code> selects the overlapping sets from y (and <code>-.b</code> selects the rest of those sets).
Line 1,046:
Examples:
 
<langsyntaxhighlight Jlang="j"> consolidate 'ab';'cd'
┌──┬──┐
│ab│cd│
Line 1,061:
┌─────┬────┐
│hijfg│abcd│
└─────┴────┘</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|D}}
{{works with|Java|7}}
<langsyntaxhighlight lang="java">import java.util.*;
 
public class SetConsolidation {
Line 1,128:
return r;
}
}</langsyntaxhighlight>
<pre>[A, B] [D, C]
[D, A, B]
Line 1,135:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,255:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>{c,d}, and {a,b}
Line 1,266:
 
Currently, jq does not have a "Set" library, so to save space here, we will use simple but inefficient implementations of set-oriented functions as they are fast for sets of moderate size. Nevertheless, we will represent sets as sorted arrays.
<langsyntaxhighlight lang="jq">def to_set: unique;
 
def union(A; B): (A + B) | unique;
Line 1,272:
# boolean
def intersect(A;B):
reduce A[] as $x (false; if . then . else (B|index($x)) end) | not | not;</langsyntaxhighlight>
'''Consolidation''':
 
For clarity, the helper functions are presented as top-level functions, but they could be defined as inner functions of the main function, consolidate/0.
 
<langsyntaxhighlight lang="jq"># Input: [i, j, sets] with i < j
# Return [i,j] for a pair that can be combined, else null
def combinable:
Line 1,307:
end
end;
</syntaxhighlight>
</lang>
'''Examples''':
<langsyntaxhighlight lang="jq">def tests:
[["A", "B"], ["C","D"]],
[["A","B"], ["B","D"]],
Line 1,319:
tests | to_set | consolidate;
 
test</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight lang="sh">$ jq -c -n -f Set_consolidation.rc
[["A","B"],["C","D"]]
[["A","B","D"]]
[["A","B","C","D"]]
[["A","B","C","D"],["F","G","H","I","K"]]</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 1,331:
 
Here I assume that the data are contained in a list of sets. Perhaps a recursive solution would be more elegant, but in this case playing games with a stack works well enough.
<syntaxhighlight lang="julia">
<lang Julia>
function consolidate{T}(a::Array{Set{T},1})
1 < length(a) || return a
Line 1,350:
return c
end
</syntaxhighlight>
</lang>
 
'''Main'''
<syntaxhighlight lang="julia">
<lang Julia>
p = Set(["A", "B"])
q = Set(["C", "D"])
Line 1,371:
println("consolidate([p, q, r, s, t]) =\n ",
consolidate([p, q, r, s, t]))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,391:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun<T : Comparable<T>> consolidateSets(sets: Array<Set<T>>): Set<Set<T>> {
Line 1,425:
)
for (sets in unconsolidatedSets) println(consolidateSets(sets))
}</langsyntaxhighlight>
 
{{out}}
Line 1,436:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- SUPPORT:
function T(t) return setmetatable(t, {__index=table}) end
function S(t) local s=T{} for k,v in ipairs(t) do s[v]=v end return s end
Line 1,473:
example:consolidate():each(function(set) set:keys():dump() end)
print("")
end</langsyntaxhighlight>
{{out}}
<pre>Given input sets:
Line 1,506:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">reduce[x_] :=
Block[{pairs, unique},
pairs =
Line 1,514:
unique = Complement[Range@Length@x, Flatten@pairs];
Join[Union[Flatten[x[[#]]]] & /@ pairs, x[[unique]]]]
consolidate[x__] := FixedPoint[reduce, {x}]</langsyntaxhighlight>
<pre>consolidate[{a, b}, {c, d}]
-> {{a, b}, {c, d}}
Line 1,526:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">proc consolidate(sets: varargs[set[char]]): seq[set[char]] =
if len(sets) < 2:
return @sets
Line 1,540:
echo consolidate({'A', 'B'}, {'B', 'D'})
echo consolidate({'A', 'B'}, {'C', 'D'}, {'D', 'B'})
echo consolidate({'H', 'I', 'K'}, {'A', 'B'}, {'C', 'D'}, {'D', 'B'}, {'F', 'G', 'H'})</langsyntaxhighlight>
 
{{out}}
Line 1,552:
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let join a b =
List.fold_left (fun acc v ->
if List.mem v acc then acc else v::acc
Line 1,591:
print_sets (consolidate [["H";"I";"K"]; ["A";"B"]; ["C";"D"]; ["D";"B"];
["F";"G";"H"]]);
;;</langsyntaxhighlight>
 
{{out}}
Line 1,600:
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang="oorexx">/* REXX ***************************************************************
* 04.08.2013 Walter Pachl using ooRexx features
* (maybe not in the best way -improvements welcome!)
Line 1,715:
End
End
Return strip(ol)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,747:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">cons(V)={
my(v,u,s);
for(i=1,#V,
Line 1,758:
V=select(v->#v,V);
if(s,cons(V),V)
};</langsyntaxhighlight>
 
=={{header|Perl}}==
We implement the key data structure, a set of sets, as an array containing references to arrays of scalars.
<langsyntaxhighlight lang="perl">use strict;
use English;
use Smart::Comments;
Line 1,808:
}
return @result;
}</langsyntaxhighlight>
{{out}}
<pre>### Example 1: [
Line 1,856:
=={{header|Phix}}==
Using strings to represent sets of characters
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">has_intersection</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span>
Line 1,892:
<span style="color: #0000FF;">?</span><span style="color: #000000;">consolidate</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"AB"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"CD"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"DB"</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">consolidate</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"HIK"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"AB"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"CD"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"DB"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"FGH"</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,903:
=={{header|PicoLisp}}==
{{trans|Python}}
<langsyntaxhighlight PicoLisplang="picolisp">(de consolidate (S)
(when S
(let R (cons (car S))
Line 1,910:
(set R (uniq (conc X (car R))))
(conc R (cons X)) ) )
R ) ) )</langsyntaxhighlight>
Test:
<langsyntaxhighlight PicoLisplang="picolisp">: (consolidate '((A B) (C D)))
-> ((A B) (C D))
: (consolidate '((A B) (B D)))
Line 1,919:
-> ((D B C A))
: (consolidate '((H I K) (A B) (C D) (D B) (F G H)))
-> ((F G H I K) (D B C A))</langsyntaxhighlight>
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">Set: procedure options (main); /* 13 November 2013 */
declare set(20) character (200) varying;
declare e character (1);
Line 1,979:
end print;
 
end Set;</langsyntaxhighlight>
<pre>
The original sets: {A,B}
Line 2,000:
=={{header|Python}}==
===Python: Iterative===
<langsyntaxhighlight lang="python">def consolidate(sets):
setlist = [s for s in sets if s]
for i, s1 in enumerate(setlist):
Line 2,010:
s1.clear()
s1 = s2
return [s for s in setlist if s]</langsyntaxhighlight>
 
===Python: Recursive===
<langsyntaxhighlight lang="python">def conso(s):
if len(s) < 2: return s
Line 2,020:
if r[0].intersection(x): r[0].update(x)
else: r.append(x)
return r</langsyntaxhighlight>
 
===Python: Testing===
The <code>_test</code> function contains solutions to all the examples as well as a check to show the order-independence of the sets given to the consolidate function.
<langsyntaxhighlight lang="python">def _test(consolidate=consolidate):
def freze(list_of_sets):
Line 2,057:
if __name__ == '__main__':
_test(consolidate)
_test(conso)</langsyntaxhighlight>
 
{{out}}
Line 2,070:
{{Trans|JavaScript}}
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Set consolidation'''
 
from functools import (reduce)
Line 2,142:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Consolidation of sets of characters:
Line 2,151:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(define (consolidate ss)
Line 2,166:
(consolidate (list (set 'a 'b) (set 'c 'd) (set 'd 'b)))
(consolidate (list (set 'h 'i 'k) (set 'a 'b) (set 'c 'd) (set 'd 'b) (set 'f 'g 'h)))
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="racket">
(list (set 'b 'a) (set 'd 'c))
(list (set 'a 'b 'c))
(list (set 'a 'b 'd 'c))
(list (set 'g 'h 'k 'i 'f) (set 'a 'b 'd 'c))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>multi consolidate() { () }
multi consolidate(Set \this is copy, *@those) {
gather {
Line 2,193:
[set(A,B), set(B,D)],
[set(A,B), set(C,D), set(D,B)],
[set(H,I,K), set(A,B), set(C,D), set(D,B), set(F,G,H)];</langsyntaxhighlight>
{{out}}
<pre>set(A, B) set(C, D)
Line 2,205:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates a method of set consolidating using some sample sets. */
@.=; @.1 = '{A,B} {C,D}'
@.2 = "{A,B} {B,D}"
Line 2,256:
 
say ' the new set=' new; say
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the (internal) default supplied sample sets:}}
<pre>
Line 2,276:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Set consolidation
 
Line 2,315:
consolidate = s + " = " + substr(list2str(sets),nl,",")
return consolidate
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,325:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'set'
 
tests = [[[:A,:B], [:C,:D]],
Line 2,337:
end
p sets
end</langsyntaxhighlight>
{{out}}
<pre>
Line 2,348:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object SetConsolidation extends App {
def consolidate[Type](sets: Set[Set[Type]]): Set[Set[Type]] = {
var result = sets // each iteration combines two sets and reiterates, else returns
Line 2,374:
})
 
}</langsyntaxhighlight>
{{out}}
<pre>{A,B} {C,D} -> {A,B} {C,D}
Line 2,383:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func consolidate() { [] }
func consolidate(this, *those) {
gather {
Line 2,407:
].each { |ss|
say (format(ss), "\n\t==> ", format(consolidate(ss...)));
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,424:
This is not a particularly efficient solution, but it gets the job done.
 
<syntaxhighlight lang="sql">
<lang SQL>
/*
This code is an implementation of "Set consolidation" in SQL ORACLE 19c
Line 2,519:
;
/
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,542:
{{tcllib|struct::set}}
This uses just the recursive version, as this is sufficient to handle substantial merges.
<langsyntaxhighlight lang="tcl">package require struct::set
 
proc consolidate {sets} {
Line 2,559:
}
return [lset r 0 $r0]
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">puts 1:[consolidate {{A B} {C D}}]
puts 2:[consolidate {{A B} {B D}}]
puts 3:[consolidate {{A B} {C D} {D B}}]
puts 4:[consolidate {{H I K} {A B} {C D} {D B} {F G H}}]</langsyntaxhighlight>
{{out}}
<pre>1:{A B} {C D}
Line 2,575:
Original solution:
 
<langsyntaxhighlight lang="txrlisp">(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])))
Line 2,599:
((a b) (c d) (d b))
((h i k) (a b) (c d) (d b) (f g h)))))
(format t "~s -> ~s\n" test (consoli test)))</langsyntaxhighlight>
 
{{out}}
Line 2,609:
{{trans|Racket}}
 
<langsyntaxhighlight lang="txrlisp">(defun mkset (items) [group-by identity items])
 
(defun empty-p (set) (zerop (hash-count set)))
Line 2,628:
((h i k) (a b) (c d) (d b) (f g h)))))
(format t "~s -> ~s\n" test
[mapcar hash-keys (consoli [mapcar mkset test])]))</langsyntaxhighlight>
 
{{out}}
Line 2,639:
{{trans|Phix}}
This solutions uses collections as sets. The first three coroutines are based on the Phix solution. Two coroutines are written to create the example sets as collections, and another coroutine to show the consolidated set.
<langsyntaxhighlight lang="vb">Private Function has_intersection(set1 As Collection, set2 As Collection) As Boolean
For Each element In set1
On Error Resume Next
Line 2,703:
show consolidate(ms(Array(mc("AB"), mc("CD"), mc("DB"))))
show consolidate(ms(Array(mc("HIK"), mc("AB"), mc("CD"), mc("DB"), mc("FGH"))))
End Sub</langsyntaxhighlight>{{out}}
<pre>{{A, B}, {C, D}}
{{A, B, D}}
Line 2,710:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Function consolidate(s)
sets = Split(s,",")
Line 2,744:
WScript.StdOut.WriteLine consolidate(t)
Next
</syntaxhighlight>
</lang>
 
{{Out}}
Line 2,760:
 
As Sets are Map-based, iteration (and hence printing) order are undefined.
<langsyntaxhighlight lang="ecmascript">import "/set" for Set
 
var consolidateSets = Fn.new { |sets|
Line 2,797:
System.print("Unconsolidated: %(sets)")
System.print("Cosolidated : %(consolidateSets.call(sets))\n")
}</langsyntaxhighlight>
 
{{out}}
Line 2,816:
=={{header|zkl}}==
{{trans|Tcl}}
<langsyntaxhighlight lang="zkl">fcn consolidate(sets){ // set are munged if they are read/write
if(sets.len()<2) return(sets);
r,r0 := List(List()),sets[0];
Line 2,826:
r[0]=r0;
r
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn prettize(sets){
sets.apply("concat"," ").pump(String,"(%s),".fmt)[0,-1]
}
Line 2,842:
prettize(sets).print(" --> ");
consolidate(sets) : prettize(_).println();
}</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits