new ← {larg} (sel ##.sam mod) old           ⍝ Select and modify.

[sel]ected  items  of the [old] array are [mod]ified. [larg] is an optional left
argument  to  [sel],  which is a primitive function suitable for selective spec-
ification.  [mod] is a monadic function that is applied to the selected items of
the  array.  Application  of  the operator, bound with its function operands, is
equivalent to the selective specification:

    new←old
    (larg sel new)←mod larg sel old
    new

Technical note:

Suggested  by  Paul  Mansour,  [sam] can be used to avoid, or at least hide, the
"destructive assignment"  that is anathema to the functional programming purist.
Having said this, the selective specification, which is used to implement [sam],
is a prime example of such destruction. There are "pure" ways to code this oper-
ator but none are as efficient.

Examples:

    vex                                 ⍝ depth-3 array.
┌───────────────┬──────────────────┬────────────────┐
│┌───┬───┬─────┐│┌─────┬────┬─────┐│┌───┬────┬─────┐│
││one│two│three│││alpha│beta│gamma│││red│blue│green││
│└───┴───┴─────┘│└─────┴────┴─────┘│└───┴────┴─────┘│
└───────────────┴──────────────────┴────────────────┘

    2 ↑sam⌽ vex                         ⍝ first two items reversed.
┌──────────────────┬───────────────┬────────────────┐
│┌─────┬────┬─────┐│┌───┬───┬─────┐│┌───┬────┬─────┐│
││alpha│beta│gamma│││one│two│three│││red│blue│green││
│└─────┴────┴─────┘│└───┴───┴─────┘│└───┴────┴─────┘│
└──────────────────┴───────────────┴────────────────┘

    2 ↑sam⌽¨ vex                        ⍝ each of first 2 items reversed.
┌───────────────┬──────────────────┬────────────────┐
│┌───┬───┬─────┐│┌────┬─────┬─────┐│┌────┬───┬─────┐│
││two│one│three│││beta│alpha│gamma│││blue│red│green││
│└───┴───┴─────┘│└────┴─────┴─────┘│└────┴───┴─────┘│
└───────────────┴──────────────────┴────────────────┘

    2 ↑sam⌽¨¨ vex                       ⍝ each of each of   ..      ..
┌───────────────┬──────────────────┬────────────────┐
│┌───┬───┬─────┐│┌─────┬────┬─────┐│┌───┬────┬─────┐│
││noe│wto│htree│││lapha│ebta│agmma│││erd│lbue│rgeen││
│└───┴───┴─────┘│└─────┴────┴─────┘│└───┴────┴─────┘│
└───────────────┴──────────────────┴────────────────┘

    ⊃sam⍪ vex                           ⍝ first item flipped.
┌───────┬──────────────────┬────────────────┐
│┌─────┐│┌─────┬────┬─────┐│┌───┬────┬─────┐│
││one  │││alpha│beta│gamma│││red│blue│green││
│├─────┤│└─────┴────┴─────┘│└───┴────┴─────┘│
││two  ││                  │                │
│├─────┤│                  │                │
││three││                  │                │
│└─────┘│                  │                │
└───────┴──────────────────┴────────────────┘

    2 ⊃sam⍪ vex                         ⍝ middle item flipped.
┌───────────────┬───────┬────────────────┐
│┌───┬───┬─────┐│┌─────┐│┌───┬────┬─────┐│
││one│two│three│││alpha│││red│blue│green││
│└───┴───┴─────┘│├─────┤│└───┴────┴─────┘│
│               ││beta ││                │
│               │├─────┤│                │
│               ││gamma││                │
│               │└─────┘│                │
└───────────────┴───────┴────────────────┘

    1 2 3 ⊃sam⍪¨ vex                    ⍝ one vector of each item  flipped.
┌─────────────┬───────────────┬────────────┐
│┌─┬───┬─────┐│┌─────┬─┬─────┐│┌───┬────┬─┐│
││o│two│three│││alpha│b│gamma│││red│blue│g││
││n│   │     │││     │e│     │││   │    │r││
││e│   │     │││     │t│     │││   │    │e││
│└─┴───┴─────┘││     │a│     │││   │    │e││
│             │└─────┴─┴─────┘││   │    │n││
│             │               │└───┴────┴─┘│
└─────────────┴───────────────┴────────────┘

    1 2 3 ⊃sam{'<',⍵,'>'}¨ vex          ⍝ one vector of each item bracketed.
┌─────────────────┬────────────────────┬──────────────────┐
│┌─────┬───┬─────┐│┌─────┬──────┬─────┐│┌───┬────┬───────┐│
││<one>│two│three│││alpha│<beta>│gamma│││red│blue│<green>││
│└─────┴───┴─────┘│└─────┴──────┴─────┘│└───┴────┴───────┘│
└─────────────────┴────────────────────┴──────────────────┘

    1 0 1 /sam{⊂'---'} vex              ⍝ outside items replaced.
┌───┬──────────────────┬───┐
│---│┌─────┬────┬─────┐│---│
│   ││alpha│beta│gamma││   │
│   │└─────┴────┴─────┘│   │
└───┴──────────────────┴───┘

    (⊂1 0 1) /sam{⊂'---'}¨ vex          ⍝ outside vectors of each item replaced.
┌─────────────┬──────────────┬──────────────┐
│┌───┬───┬───┐│┌───┬────┬───┐│┌───┬────┬───┐│
││---│two│---│││---│beta│---│││---│blue│---││
│└───┴───┴───┘│└───┴────┴───┘│└───┴────┴───┘│
└─────────────┴──────────────┴──────────────┘

    1 0 1 /sam{⊂'---'}¨ vex             ⍝ each vector of outside items replaced.
┌─────────────┬──────────────────┬─────────────┐
│┌───┬───┬───┐│┌─────┬────┬─────┐│┌───┬───┬───┐│
││---│---│---│││alpha│beta│gamma│││---│---│---││
│└───┴───┴───┘│└─────┴────┴─────┘│└───┴───┴───┘│
└─────────────┴──────────────────┴─────────────┘

    ↑vex                                ⍝ matrix of vectors.
┌─────┬────┬─────┐
│one  │two │three│
├─────┼────┼─────┤
│alpha│beta│gamma│
├─────┼────┼─────┤
│red  │blue│green│
└─────┴────┴─────┘

    1 1 ⍉sam⌽ ↑vex                      ⍝ diagonal reversed.
┌─────┬────┬─────┐
│green│two │three│
├─────┼────┼─────┤
│alpha│beta│gamma│
├─────┼────┼─────┤
│red  │blue│one  │
└─────┴────┴─────┘

    1 1 ⍉sam(⌽¨) ↑vex                   ⍝ each vector on diagonal reversed.
┌─────┬────┬─────┐
│eno  │two │three│
├─────┼────┼─────┤
│alpha│ateb│gamma│
├─────┼────┼─────┤
│red  │blue│neerg│
└─────┴────┴─────┘

    1 1 ⍉sam{ucase¨⍵} ↑vex              ⍝ diagonal vectors upper-cased.
┌─────┬────┬─────┐
│ONE  │two │three│
├─────┼────┼─────┤
│alpha│BETA│gamma│
├─────┼────┼─────┤
│red  │blue│GREEN│
└─────┴────┴─────┘

    ↑¨vex                               ⍝ vector of matrices.
┌─────┬─────┬─────┐
│one  │alpha│red  │
│two  │beta │blue │
│three│gamma│green│
└─────┴─────┴─────┘

    (⊂1 1) ⍉sam{ucase ⍵}¨ ↑¨vex         ⍝ diagonals upper-cased.
┌─────┬─────┬─────┐
│One  │Alpha│Red  │
│tWo  │bEta │bLue │
│thRee│gaMma│grEen│
└─────┴─────┴─────┘

    (⊂1 1) ⍉sam{'·'}¨ ↑¨vex             ⍝ diagonals zapped.
┌─────┬─────┬─────┐
│·ne  │·lpha│·ed  │
│t·o  │b·ta │b·ue │
│th·ee│ga·ma│gr·en│
└─────┴─────┴─────┘

See also: select each at

Back to: contents

Back to: Workspaces