September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
18
Task/Run-length-encoding/Zkl/run-length-encoding-1.zkl
Normal file
18
Task/Run-length-encoding/Zkl/run-length-encoding-1.zkl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
const MAX_LEN=250, MIN_LEN=3;
|
||||
fcn compress(text){ // !empty byte/text stream -->Data (byte stream)
|
||||
sink:=Data(); cnt:=Ref(0);
|
||||
write:='wrap(c,n){ // helper function
|
||||
while(n>MAX_LEN){
|
||||
sink.write(1); sink.write(MAX_LEN); sink.write(c);
|
||||
n-=MAX_LEN;
|
||||
}
|
||||
if(n>MIN_LEN){ sink.write(1); sink.write(n); sink.write(c); }
|
||||
else { do(n) { sink.write(c); } }
|
||||
};
|
||||
text.reduce('wrap(a,b){
|
||||
if(a==b) cnt.inc();
|
||||
else{ write(a,cnt.value); cnt.set(1); }
|
||||
b
|
||||
},text[0]) : write(_,cnt.value);
|
||||
sink;
|
||||
}
|
||||
6
Task/Run-length-encoding/Zkl/run-length-encoding-2.zkl
Normal file
6
Task/Run-length-encoding/Zkl/run-length-encoding-2.zkl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
fcn inflate(data){ //-->String
|
||||
data.howza(3).pump(String,
|
||||
fcn(c){ // if c==1, read n,c2 and expand, else write c
|
||||
if(c=="\x01") return(Void.Read,2) else return(Void.Write,c) },
|
||||
fcn(_,n,c){ c*n.toAsc() })
|
||||
}
|
||||
5
Task/Run-length-encoding/Zkl/run-length-encoding-3.zkl
Normal file
5
Task/Run-length-encoding/Zkl/run-length-encoding-3.zkl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
text:="WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
|
||||
d:=compress(text);
|
||||
d.bytes().println();
|
||||
println(text.len()," bytes --> ",d.len()," bytes");
|
||||
println(text==inflate(d));
|
||||
Loading…
Add table
Add a link
Reference in a new issue