⍝ Variations on primitive scan:

    ⎕io←1

    mscan ⍳10                       ⍝ quick minus-scan
1 ¯1 2 ¯2 3 ¯3 4 ¯4 5 ¯5

    ÷\⍳10                           ⍝ slow primitive divide-scan.
1 0.5 1.5 0.375 1.875 0.3125 2.1875 0.2734375 2.4609375 0.24609375

    dscan ⍳10                       ⍝ quick divide-scan.
1 0.5 1.5 0.375 1.875 0.3125 2.1875 0.2734375 2.4609375 0.24609375

    {⍺+⍵}\⍳10                       ⍝ slow primitive scan.
1 3 6 10 15 21 28 36 45 55

    {⍺+⍵}ascan⍳10                   ⍝ quick associative scan.
1 3 6 10 15 21 28 36 45 55

    {⍺-⍵}\⍳10                       ⍝ slow primitive scan.
1 ¯1 2 ¯2 3 ¯3 4 ¯4 5 ¯5

    {⍺-⍵}ascan⍳10                   ⍝ quick left to right accumulation.
1 ¯1 ¯4 ¯8 ¯13 ¯19 ¯26 ¯34 ¯43 ¯53

    {⍵×⍺+⍵} ascan 0 0 1 0 1 1 1 1 0 1 1 1 0 0       ⍝ count adjacent 1s.
0 0 1 0 1 2 3 4 0 1 2 3 0 0

    ,ascan 2 3⍴⍳6                   ⍝ higher rank scan.
┌─┬───┬─────┐
│1│1 2│1 2 3│
├─┼───┼─────┤
│4│4 5│4 5 6│
└─┴───┴─────┘

    '-'ascan'abcd'                  ⍝ array operand.
┌─┬───┬─────────┬───────────────┐
│a│a-b│┌───┬─┬─┐│┌─────────┬─┬─┐│
│ │   ││a-b│-│c│││┌───┬─┬─┐│-│d││
│ │   │└───┴─┴─┘│││a-b│-│c││ │ ││
│ │   │         ││└───┴─┴─┘│ │ ││
│ │   │         │└─────────┴─┴─┘│
└─┴───┴─────────┴───────────────┘

    ⌽⍨ ascan 'hello' 1 1 0 ¯1 ¯1    ⍝ rotate commute.
┌─────┬─────┬─────┬─────┬─────┬─────┐
│hello│elloh│llohe│llohe│elloh│hello│
└─────┴─────┴─────┴─────┴─────┴─────┘

    top← ↑('one' 'two' 'three')('un' 'deux' 'trois')
    bot← ↑('yan' 'tan' 'tethera')('bir' 'iki' 'üç')

    {⍺,'-',⍵}ascan ↑top bot         ⍝ scan over rank-3 array.
┌───┬───────┬───────────────┐
│one│one-two│one-two-three  │
├───┼───────┼───────────────┤
│un │un-deux│un-deux-trois  │
└───┴───────┴───────────────┘
┌───┬───────┬───────────────┐
│yan│yan-tan│yan-tan-tethera│
├───┼───────┼───────────────┤
│bir│bir-iki│bir-iki-üç     │
└───┴───────┴───────────────┘

    ,ascan∘⍳¨0 1 2                  ⍝ check early-out on few items.
┌┬─┬───────┐
││1│┌─┬───┐│
││ ││1│1 2││
││ │└─┴───┘│
└┴─┴───────┘

⍝∇ ascan dscan mscan

Back to: code

Back to: Workspaces