Search code examples
apldyalog

How provide default values when using ⌸ in Dyalog APL


When using key to produce a frequency table, e.g

      {⍺,≢⍵}⌸'mississippi'
┌→──┐
↓m 1│
│i 4│
│s 4│
│p 2│
└+──┘

I frequently find myself wanting to 'seed' the result set to provide a count of 0 for any items I know to be missing. If we're doing a letter frequency distribution analysis we might want to provide a 0 as the base value for all of ⎕A. Can anyone recommend a good way to achieve this in Dyalog APL?


Solution

  • If you're performing a frequency analysis "over" ⎕A, then maybe a result with the same shape as ⎕A is actually easier to consume than the output of ⌸:

    ⎕A (+/∘.=) 'MISSISSIPPI'
    

    (I think Dyalog knows how to optimize this so that it doesn't actually create a 26-by-n binary array and then sum the rows.)

    Another option is to first append the alphabet to the input, and then decrease all counts by 1.

    {⍺,(≢⍵)-1}⌸ 'MISSISSIPPI',⎕A