-- demo\rosetta\LZW_compression.exw with javascript_semantics function compress(string uncompressed) integer dict = new_dict(), dictSize = 255, c sequence result = {} string word = "" for i=0 to 255 do setd(""&i,i,dict) end for for c in uncompressed do if getd_index(word&c,dict) then word &= c else result &= getd(word,dict) dictSize += 1 setd(word&c,dictSize,dict) word = ""&c end if end for if word!="" then result &= getd(word,dict) end if destroy_dict(dict) return result end function function decompress(sequence compressed) integer dict = new_dict(), dictSize = 255, k, ki string dent = "", result = "", word = "" for i=0 to 255 do setd(i,""&i,dict) end for for k in compressed do if getd_index(k,dict) then dent = getd(k,dict) elsif k=dictSize then dent = word&word[1] else crash("error") end if result &= dent setd(dictSize,word&dent[1],dict) dictSize += 1 word = dent end for destroy_dict(dict) return result end function constant example = "TOBEORNOTTOBEORTOBEORNOT" sequence com = compress(example) pp(com,{pp_IntCh,true,pp_Maxlen,90}) ?decompress(com)