res ← keys (fn ##.key) list         ⍝ key operator

From Roger Hui, this operator models the "key" adverb of J (u/.), described:

    "In (x u/. y), items of x specify keys for corresponding items of y
     and u is applied to each collection of y having identical keys."

The shape of the result is (⍴∪⍺) the order being that of the first occurrence of
    each item of (∪⍺)."

Technical note:

From Dyalog V12.1, the code may be simplified:                              <V>

    ⍺⍺¨(↓{(∪⍵)∘.=⍵}⍳⍨⊂[1↓⍳⍴⍴⍺]⍺)⌿¨⊂⍵
                     ├┬──────┘
    ⍺⍺¨(↓{(∪⍵)∘.=⍵}⍳⍨↓⍪⍺)⌿¨⊂⍵

Examples:

    abc ← 'abracadabra'

    abc +/ key 1
5 2 2 1 1

    +key∘⍳∘⍴⍨ abc
 1 4 6 8 11  2 9  3 10  5  7

    {(⊃⍵),⍴⍵}key⍨ abc
 a 5  b 2  r 2  c 1  d 1

    disp {⍵}key⍨ 5 4⍴'JohnMaryJohn'
┌→───┬────┐
│John│Mary│
│John│Mary│
│John↓    ↓
└───→┴───→┘

See also: alists

Back to: contents

Back to: Workspaces

Trouble seeing APL font?