Run-length encoding: Difference between revisions

jq
m (Added the Sidef language)
(jq)
Line 1,800:
}</lang>
 
=={{header|Kjq}}==
Note: "run_length_decode" as defined below requires a version of jq with regex support.
 
'''Utility function:'''
<lang jq>def runs:
reduce .[] as $item
( [];
if . == [] then [ [ $item, 1] ]
else .[-1] as $last
| if $last[0] == $item then (.[0:length-1] + [ [$item, $last[1] + 1] ] )
else . + [[$item, 1]]
end
end ) ;</lang>
'''Run-length encoding and decoding''':
<lang jq>def run_length_encode:
explode | runs | reduce .[] as $x (""; . + "\($x[1])\([$x[0]]|implode)");
 
def run_length_decode:
reduce (scan( "[0-9]+[A-Z]" )) as $pair
( "";
($pair[0:-1] | tonumber) as $n
| $pair[-1:] as $letter
| . + ($n * $letter)) ;</lang>
'''Example''':
<lang jq>"ABBCCC" | run_length_encode | run_length_decode</lang>
{{out}}
<lang sh>$ jq -n -f Run_length_encoding.jq
"ABBBCCC"</lang>
 
=={{header|K}}==
 
<lang k>rle: {,/($-':i,#x),'x@i:&1,~=':x}</lang>
Line 1,815 ⟶ 1,843:
rld "12W1B12W3B24W1B14W"
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"</lang>
 
 
 
=={{header|Lasso}}==
2,497

edits