at{⍺←⊢                             ⍝ Prefix-friendly @.
    0::⎕SIGNAL ⎕EN                  ⍝ signal any error to caller
    chk←{0⊣⎕SIGNAL ⍵↓⍺}             ⍝ ⍺-signal unless ⍵
    lfn rfn←3=⎕NC 2 2⍴'⍺⍺⍵⍵'        ⍝ operands are functions?
    2 chk lfn∨3=⎕NC'⍺':             ⍝ ⍺ and ⍺⍺ can't both be arrays
    ~lfn:⍺⍺⊣∇∇ ⍵⍵⊢⍵                 ⍝ modifier is array: ⍺@ → ⍺⊣@
    S←⍵⍵⊣⍵                          ⍝ selector array, mask or index
    rfn<1<|≡S:AA[S]←⍺ ⍺⍺(A←⍵)[S]   ⍝ nested: reach/choose indexing
    pfx←{⍺≡(⍴⍺)↑⍵}                  ⍝ is a prefix?
    4 chk rfn∨1≥⍴⍴S:                ⍝ index must be vector or scalar
    5 chk rfnpfx/⍴¨S ⍵:            ⍝ mask shape must be prefix of ⍵ shape
    X←⍸⍣rfn⊢,S                      ⍝ numeric index selection vector
    P←,[⍳1+⍴⍴S]↑,⊂⍵                 ⍝ projection conformable with selection
    sel(X)P                      ⍝ selection
    sx←{⍬⍴⍣(1=×/⍴⍵)⊢⍵}              ⍝ single → scalar for single extension
    newsx ⍺ ⍺⍺ sel                 ⍝ new values for selection
    extnew∘.⊣((⍴⍴new)↓⍴sel)⍴0      ⍝ extension of new values to conform
    (⍴⍵)P⊣((X)P)←ext             ⍝ amended array.
}
code_colours

test script

Back to: notes

Back to: Workspaces