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 |
<lang rexx>/*REXX program demonstrates a method of consolidating some sample sets. */ |
||
@.=; @.1 = '{A,B} {C,D}' |
|||
sets. = /*assign all SETS. to null.*/ |
|||
@.2 = "{A,B} {B,D}" |
|||
@.3 = '{A,B} {C,D} {D,B}' |
|||
@.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 |
do j=1 while @.j\=='' /*traipse through each of sample sets. */ |
||
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. */ |
||
⚫ | |||
/*──────────────────────────────────SETCOMBO subroutine─────────────────*/ |
|||
isIn: return wordpos(arg(1), arg(2))\==0 /*is (word) arg1 in set arg2 ? */ |
|||
/*──────────────────────────────────SETCOMBINE subroutine─────────────────────*/ |
|||
⚫ | |||
SETcombine: procedure; parse arg old,new; #=words(old) /*nullify NEW.*/ |
|||
⚫ | |||
do k=1 for |
do k=1 for # /* [↓] change all commas to a blank. */ |
||
!.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 |
do set=1 for #-1 |
||
do item=1 for words( |
do item=1 for words(!.set); x=word(!.set,item) |
||
do other=set+1 to |
do other=set+1 to # |
||
if isIn(x, |
if isIn(x,!.other) then do; changed=1 /*has changed*/ |
||
!.set=!.set !.other; !.other= |
|||
iterate set |
iterate set |
||
end |
end |
||
end /*other*/ |
end /*other*/ |
||
end /*item*/ |
end /*item */ |
||
end /*set*/ |
end /*set */ |
||
end /*until |
end /*until*/ |
||
/* ╔╦══════════════════════════════════════════════════elide any duplicates.*/ |
|||
do set=1 for |
do set=1 for #; $= /*nullify $ */ |
||
do items=1 for words( |
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 |
||
$=$ x /*build new. */ |
|||
do until \isIn(x, |
do until \isIn(x, !.set) |
||
_=wordpos(x, |
_=wordpos(x, !.set) |
||
!.set=subword(!.set,1,_-1) ',' subwOrd(!.set,_+1) /*purify set.*/ |
|||
end /*until ¬isIn*/ |
end /*until ¬isIn ··· */ |
||
end /*items*/ |
end /*items*/ |
||
!.set=translate(strip($), ',', " ") |
|||
end /*set*/ |
end /*set*/ |
||
do |
do i=1 for #; if !.i=='' then iterate |
||
new=space(new '{'!.i"}") |
|||
end /* |
end /*i*/ |
||
say ' the new |
say ' the new set=' new; say |
||
return |
return</lang> |
||
⚫ | |||
⚫ | |||
isIn: return wordpos(arg(1), arg(2))\==0 /*is (word) arg1 in set arg2? */</lang> |
|||
⚫ | |||
<pre> |
<pre> |
||
the old |
the old set= {A,B} {C,D} |
||
the new |
the new set= {A,B} {C,D} |
||
the old |
the old set= {A,B} {B,D} |
||
the new |
the new set= {A,B,D} |
||
the old |
the old set= {A,B} {C,D} {D,B} |
||
the new |
the new set= {A,B,D,C} |
||
the old |
the old set= {H,I,K} {A,B} {C,D} {D,B} {F,G,H} |
||
the new |
the new set= {H,I,K,F,G} {A,B,D,C} |
||
the old |
the old set= {snow,ice,slush,frost,fog} {icebergs,icecubes} {rain,fog,sleet} |
||
the new |
the new set= {snow,ice,slush,frost,fog,rain,sleet} {icebergs,icecubes} |
||
</pre> |
</pre> |
||