⍝ Run Length Encoding (RLE packing):

    packR'Mississippi'
┌──┬───────────────┬────────┐
│11│1 1 2 1 2 1 2 1│Misisipi│
└──┴───────────────┴────────┘

    packR 11 11 11 22 22 11 11
┌─┬─────┬────────┐
│7│3 2 2│11 22 11│
└─┴─────┴────────┘

    mat← {⍵×[⎕io]⍵∘.≥⍵}⍳9    ⍝ lower-diagonal matrix.
    mat
1 0 0 0 0 0 0 0 0
2 2 0 0 0 0 0 0 0
3 3 3 0 0 0 0 0 0
4 4 4 4 0 0 0 0 0
5 5 5 5 5 0 0 0 0
6 6 6 6 6 6 0 0 0
7 7 7 7 7 7 7 0 0
8 8 8 8 8 8 8 8 0
9 9 9 9 9 9 9 9 9

    packR mat
┌───┬─────────────────────────────────┬─────────────────────────────────┐
│9 9│1 8 2 7 3 6 4 5 5 4 6 3 7 2 8 1 9│1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9│
└───┴─────────────────────────────────┴─────────────────────────────────┘

    chk←{
        ⍵≡0 ⍺⍺ ⍺⍺ ⍵:'ok'
        ' Error: compression round-trip fails'
    }

    packR chk ⎕fmt notes.Marilyn        ⍝ simple text matrix
ok

Back to: code

Back to: Workspaces