Set consolidation: Difference between revisions

Content added Content deleted
(Added EchoLisp)
m (→‎{{header|REXX}}: added/changed whitespace and comments, made other cosmetic changes.)
Line 1,464: Line 1,464:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program demonstrates how to consolidate a sample bunch of sets.*/
<lang rexx>/*REXX program demonstrates a method of consolidating some sample sets. */
@.=; @.1 = '{A,B} {C,D}'
sets. = /*assign all SETS. to null.*/
sets.1 = '{A,B} {C,D}'
@.2 = "{A,B} {B,D}"
sets.2 = "{A,B} {B,D}"
@.3 = '{A,B} {C,D} {D,B}'
sets.3 = '{A,B} {C,D} {D,B}'
@.4 = '{H,I,K} {A,B} {C,D} {D,B} {F,G,H}'
sets.4 = '{H,I,K} {A,B} {C,D} {D,B} {F,G,H}'
@.5 = '{snow,ice,slush,frost,fog} {icebergs,icecubes} {rain,fog,sleet}'
sets.5 = '{snow,ice,slush,frost,fog} {iceburgs,icecubes} {rain,fog,sleet}'


do j=1 while sets.j\=='' /*traipse through the sample sets*/
do j=1 while @.j\=='' /*traipse through each of sample sets. */
call SETcombo sets.j /*have the other guy do the work.*/
call SETcombine @.j /*have the function do the heavy work. */
end /*j*/
end /*j*/
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────ISIN SUBRoutine───────────────────────────*/
/*──────────────────────────────────SETCOMBO subroutine─────────────────*/
SETcombo: procedure; parse arg bunch; n=words(bunch); newBunch=
isIn: return wordpos(arg(1), arg(2))\==0 /*is (word) arg1 in set arg2 ? */
/*──────────────────────────────────SETCOMBINE subroutine─────────────────────*/
say ' the old sets=' space(bunch)
SETcombine: procedure; parse arg old,new; #=words(old) /*nullify NEW.*/
say ' the old set=' space(old)


do k=1 for n /* [↓] change commas to a blank.*/
do k=1 for # /* [↓] change all commas to a blank. */
@.k=translate(word(bunch,k),,'},{') /*create a list of words (=a set)*/
!.k=translate(word(old,k), , '},{') /*create a list of words (aka, a set).*/
end /*k*/ /*··· and also remove the braces.*/
end /*k*/ /* [↑] ··· and also remove the braces.*/


do until \changed; changed=0 /*consolidate some sets (maybe).*/
do until \changed; changed=0 /*consolidate some sets (well, maybe).*/
do set=1 for n-1
do set=1 for #-1
do item=1 for words(@.set); x=word(@.set,item)
do item=1 for words(!.set); x=word(!.set,item)
do other=set+1 to n
do other=set+1 to #
if isIn(x,@.other) then do; changed=1 /*has changed.*/
if isIn(x,!.other) then do; changed=1 /*has changed*/
@.set=@.set @.other; @.other=
!.set=!.set !.other; !.other=
iterate set
iterate set
end
end
end /*other*/
end /*other*/
end /*item*/
end /*item */
end /*set*/
end /*set */
end /*until ¬changed*/
end /*until*/
/* ╔╦══════════════════════════════════════════════════elide any duplicates.*/

do set=1 for n; new= /*remove duplicates in a set. */
do set=1 for #; $= /*nullify $ */
do items=1 for words(@.set); x=word(@.set, items)
do items=1 for words(!.set); x=word(!.set, items)
if x==',' then iterate; if x=='' then leave
if x==',' then iterate; if x=='' then leave
new=new x /*start building the new set. */
$=$ x /*build new. */
do until \isIn(x, @.set)
do until \isIn(x, !.set)
_=wordpos(x, @.set)
_=wordpos(x, !.set)
@.set=subword(@.set,1,_-1) ',' subword(@.set,_+1) /*purify set.*/
!.set=subword(!.set,1,_-1) ',' subwOrd(!.set,_+1) /*purify set.*/
end /*until ¬isIn*/
end /*until ¬isIn ··· */
end /*items*/
end /*items*/
@.set=translate(strip(new), ',', " ")
!.set=translate(strip($), ',', " ")
end /*set*/
end /*set*/


do new=1 for n; if @.new=='' then iterate
do i=1 for #; if !.i=='' then iterate
newBunch=space(newbunch '{'@.new"}")
new=space(new '{'!.i"}")
end /*new*/
end /*i*/


say ' the new sets=' newBunch; say
say ' the new set=' new; say
return
return</lang>
'''output''' &nbsp; when using the default supplied sample sets:
/*──────────────────────────────────ISIN subroutine─────────────────────*/
isIn: return wordpos(arg(1), arg(2))\==0 /*is (word) arg1 in set arg2? */</lang>
{{out}} when using the default supplied sample sets:
<pre>
<pre>
the old sets= {A,B} {C,D}
the old set= {A,B} {C,D}
the new sets= {A,B} {C,D}
the new set= {A,B} {C,D}


the old sets= {A,B} {B,D}
the old set= {A,B} {B,D}
the new sets= {A,B,D}
the new set= {A,B,D}


the old sets= {A,B} {C,D} {D,B}
the old set= {A,B} {C,D} {D,B}
the new sets= {A,B,D,C}
the new set= {A,B,D,C}


the old sets= {H,I,K} {A,B} {C,D} {D,B} {F,G,H}
the old set= {H,I,K} {A,B} {C,D} {D,B} {F,G,H}
the new sets= {H,I,K,F,G} {A,B,D,C}
the new set= {H,I,K,F,G} {A,B,D,C}


the old sets= {snow,ice,slush,frost,fog} {iceburgs,icecubes} {rain,fog,sleet}
the old set= {snow,ice,slush,frost,fog} {icebergs,icecubes} {rain,fog,sleet}
the new sets= {snow,ice,slush,frost,fog,rain,sleet} {iceburgs,icecubes}
the new set= {snow,ice,slush,frost,fog,rain,sleet} {icebergs,icecubes}
</pre>
</pre>