⍝ extended at:

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ right operand conformability

      1 at{⍬⍴1 0}3 4 5⍴0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
         
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
         
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

      1 at{3⍴1 0}3 4 5⍴0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
         
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
         
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

      1 at{3 4⍴1 0}3 4 5⍴0
1 1 1 1 1
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0
         
1 1 1 1 1
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0
         
1 1 1 1 1
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0

      1 at{3 4 5⍴1 0}3 4 5⍴0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
         
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
         
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ left operand conformability

      (⍬⍴⍳40)at{1 0 1}3 4 5⍴0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
         
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
         
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

      (2⍴⍳40)at{1 0 1}3 4 5⍴0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
         
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
         
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
      (2 4⍴⍳40)at{1 0 1}3 4 5⍴0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
         
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
         
5 5 5 5 5
6 6 6 6 6
7 7 7 7 7
8 8 8 8 8

      (2 4 5⍴⍳40)at{1 0 1}3 4 5⍴0
 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20
              
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
              
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ left and right operand conformability

    (⍳4) +⍤0 1 at{3 4⍴0 0 1}3 4 5⍴0     ⍝ left and right operand agreement
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0
         
0 0 0 0 0
2 2 2 2 2
0 0 0 0 0
0 0 0 0 0
         
3 3 3 3 3
0 0 0 0 0
0 0 0 0 0
4 4 4 4 4

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ Special cases

    (,'*')at{1 0}2 2⍴⎕A         ⍝ _single_ extension
**
CD

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ Regular @ testing:

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ selection by index:

    '*'at 2 4 ⊢5↑⎕A             ⍝ * at 2nd and 4th items
A*C*E

    M←5 5⍴⍳25

    0 at 2 4 ⊢M                 ⍝ 0 at 2nd and 4th rows
 1  2  3  4  5
 0  0  0  0  0
11 12 13 14 15
 0  0  0  0  0
21 22 23 24 25

    (2 5⍴⎕a) at 2 4 ⊢M          ⍝ A..J at 2nd and 4th rows
 1  2  3  4  5
 A  B  C  D  E
11 12 13 14 15
 F  G  H  I  J
21 22 23 24 25

    0 at 2 4⍤1 ⊢M               ⍝ 0 at 2nd and 4th cols
 1 0  3 0  5
 6 0  8 0 10
11 0 13 0 15
16 0 18 0 20
21 0 23 0 25

   (⍉2 5⍴⎕a)⊣at 2 4⍤1 ⊢M        ⍝ A..J at 2nd and 4th cols
 1 A  3 F  5
 6 B  8 G 10
11 C 13 H 15
16 D 18 I 20
21 E 23 J 25

    ⌽ at 2 4 ⊢M                 ⍝ reverse of 2nd and 4th rows (boustrophedon)
 1  2  3  4  5
10  9  8  7  6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25

    ⊖ at 2 4 ⊢M                 ⍝ exchange of 2nd and 4th rows →gauss_jordan←
 1  2  3  4  5
16 17 18 19 20
11 12 13 14 15
 6  7  8  9 10
21 22 23 24 25

    ⍙←{⍵⍵⍣¯1 ⍺⍺ ⍵⍵ ⍵}           ⍝ model for under/dual: ⍢

    ⌽ at 2 4⍙⍉ ⊢M               ⍝ reverse of alternate cols (Anc. Mongolian)
 1 22  3 24  5
 6 17  8 19 10
11 12 13 14 15
16  7 18  9 20
21  2 23  4 25

    0 at 2 4⍤1 at 2 4 ⊢M        ⍝ cf: M[2 4;2 4]←0
 1  2  3  4  5
 6  0  8  0 10
11 12 13 14 15
16  0 18  0 20
21 22 23 24 25

    0 at (2 4∘.,2 4) ⊢5 5⍴⍳25   ⍝ ditto using choose indexing
 1  2  3  4  5
 6  0  8  0 10
11 12 13 14 15
16  0 18  0 20
21 22 23 24 25

    (2 2⍴⎕a)⊣at 2 4⍤1 at 2 4 ⊢M     ⍝ cf: M[2 4;2 4]←2 2⍴⎕A
 1  2  3  4  5
 6  A  8  B 10
11 12 13 14 15
16  C 18  D 20
21 22 23 24 25

    '⍟'at(1 5)(2 2)⊢'hello' 'world'         ⍝ reach
┌─────┬─────┐
│hell⍟│w⍟rld│
└─────┴─────┘

    10×at 2 4 ⊢⍳5               ⍝ 10× at ...
1 20 3 40 5

    1+at 2 2 ⊢5/0               ⍝ nb: dups ignored
0 1 0 0 0

    3 at(⊂⍬) ⊢4                 ⍝ test of scalar substitution
3

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ boolean selection:

    '*'at(∊∘'AEIOU') ⎕A         ⍝ vowels
*BCD*FGH*JKLMN*PQRST*VWXYZ

    100×at(2∘|) ⊢M              ⍝ alternately × 100
 100    2  300    4  500
   6  700    8  900   10
1100   12 1300   14 1500
  16 1700   18 1900   20
2100   22 2300   24 2500

    {∊¨(0=3 5∘|¨⍵)/¨⊂'fizz' 'buzz'}at{0∨.=3 5∘.|⍵} 2 3 8⍴⍳48    ⍝ FizzBuzz
┌────┬────┬────┬────┬────────┬────────┬────────┬────┐
│1   │2   │fizz│4   │buzz    │fizz    │7       │8   │
├────┼────┼────┼────┼────────┼────────┼────────┼────┤
│fizz│buzz│11  │fizz│13      │14      │fizzbuzz│16  │
├────┼────┼────┼────┼────────┼────────┼────────┼────┤
│17  │fizz│19  │buzz│fizz    │22      │23      │fizz│
└────┴────┴────┴────┴────────┴────────┴────────┴────┘
┌────┬────┬────┬────┬────────┬────────┬────────┬────┐
│buzz│26  │fizz│28  │29      │fizzbuzz│31      │32  │
├────┼────┼────┼────┼────────┼────────┼────────┼────┤
│fizz│34  │buzz│fizz│37      │38      │fizz    │buzz│
├────┼────┼────┼────┼────────┼────────┼────────┼────┤
│41  │fizz│43  │44  │fizzbuzz│46      │47      │fizz│
└────┴────┴────┴────┴────────┴────────┴────────┴────┘

    3 4 at⊢¨ 2⍴⊂1 1 0               ⍝ left operand 3 4: bound to @
┌─────┬─────┐
│3 4 0│3 4 0│
└─────┴─────┘
    3 4 ⊣at⊢¨ 2⍴⊂1 1 0              ⍝ left argument 3 4: distribution by ¨
┌─────┬─────┐
│3 3 0│4 4 0│
└─────┴─────┘

    0 at {1} ⍳3                     ⍝ single item result distributed each-wise
0 0 0
    0 at {0} ⍳3
1 2 3

    {(≢⍵)⍴⎕A}@(1∘pco) 6 7⍴ ⍳42      ⍝ vector ABC... for primes [GC]
 1  A  B  4  C  6  D
 8  9 10  E 12  F 14
15 16  G 18  H 20 21
22  I 24 25 26 27 28
 J 30  K 32 33 34 35
36  L 38 39 40  M 42

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝   errors:

    '*'at(2 2⍴⍳4)⊢5 5⍴⎕A            ⍝ simple index selector rank too high
4::RANK ERROR

    '*'at(⍉∊∘'AEIOU') 4 5⍴⎕A        ⍝ mask not conformable with right arg
5::LENGTH ERROR

    'abc'at{1 0 1} 3 4⍴⎕A           ⍝ new values not conformable with selection
5::LENGTH ERROR

    0(1 at 2)⍳3                     ⍝ ⍺ and ⍺⍺ can't both be arrays 
2::SYNTAX ERROR

    1 at{2}⍳3                       ⍝ function ⍵⍵ must return bool result
11::DOMAIN ERROR

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝

⍝ Roger Hui's model of "Mesh" and "Mask", implemented using [at]:

⍝ Mesh (APL, 1962) -------------------------------------------------------------

⍝   c ← a (u Mesh) b    ←→    ((~u)⌿c)≡a and (u⌿c)≡b

    ⎕io←0

    a ← 33 44 55
    b ← ⍳10
    u ← 1 1 0 1 1 1 1 0 1 1 1 0 1

    ↑ u (b at(u⊣⊣) (~u)⍀a)
1 1  0 1 1 1 1  0 1 1 1  0 1
0 1 33 2 3 4 5 44 6 7 8 55 9

    c←b at(u⊣⊣) (~u)⍀a
    a ≡ (~u)⌿c
1
    b ≡ u⌿c
1
    c ≡ b at(u/⍳≢u) ⊢(~u)⍀a
1

⍝ The last version with the integer right operand is required for meshing major
⍝ cells.

    a←2⍴⍤0 ⊢33 44 55
    b←2⍴⍤0 ⍳10
    c ← b at(u/⍳≢u) ⊢(~u)⍀a
    a ≡ (~u)⌿c
1
    b ≡ u⌿c
1

⍝ Mask (APL, 1962) -------------------------------------------------------------

⍝   c ← a (u Mask) b    ←→    ((~u)⌿c)≡(~u)⌿a and (u⌿c)≡u⌿b

⍝ For numeric vectors a and b,

    a ← ⍳13
    b ← 20+⍳13
    u ← 13⍴0 0 1
    c ← (a×~u) + b×u

    ↑ u (b (u⌿⊣)at(u⊣⊣) a)
0 0  1 0 0  1 0 0  1 0  0  1  0
0 1 22 3 4 25 6 7 28 9 10 31 12

    c ← b (u⌿⊣)at(u⊣⊣) a
    ((~u)⌿c) ≡ (~u)⌿a
1
    (u⌿c)≡u⌿b
1
    c ≡ b (u⌿⊣)at(u/⍳≢u) ⊢a
1

⍝ Again, the last version with the integer right operand is required for masking
⍝ major cells.

    a ← 2⍴⍤0 ⊢⍳13
    b ← 2⍴⍤0 ⊢20+⍳13
    c ← b (u⌿⊣)at(u/⍳≢u) ⊢a
    ((~u)⌿c) ≡ (~u)⌿a
1
    (u⌿c)≡u⌿b
1

⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝ Prefix agreement 
                                                ⍝ Aaron Hsu
    '*'at{1 0 1} 3 4⍴⍳12                        ⍝ vector mask vs matrix rarg
* * * *
4 5 6 7
* * * *

     '*'⊣at((0=10|⊃)⍤2)5 5 5⍴⍳75                ⍝ Aaron's example
 *  *  *  *  *
 *  *  *  *  *
 *  *  *  *  *
 *  *  *  *  *
 *  *  *  *  *
              
25 26 27 28 29
30 31 32 33 34
35 36 37 38 39
40 41 42 43 44
45 46 47 48 49
              
 *  *  *  *  *
 *  *  *  *  *
 *  *  *  *  *
 *  *  *  *  *
 *  *  *  *  *
              
 *  *  *  *  *
 *  *  *  *  *
 *  *  *  *  *
 *  *  *  *  *
 *  *  *  *  *
              
25 26 27 28 29
30 31 32 33 34
35 36 37 38 39
40 41 42 43 44
45 46 47 48 49

    10×at{3 4⍴1 0 0} 3 4 5⍴⍳5                   ⍝ higher rank example
0 10 20 30 40
0  1  2  3  4
0  1  2  3  4
0 10 20 30 40
             
0  1  2  3  4
0  1  2  3  4
0 10 20 30 40
0  1  2  3  4
             
0  1  2  3  4
0 10 20 30 40
0  1  2  3  4
0  1  2  3  4

    'efgh'at{0 1 0} 3 4⍴ ⎕A                     ⍝ unit axes not ignored
5::LENGTH ERROR

    (↑,↓'efgh')at{0 1 0} 3 4⍴ ⎕A                ⍝ conv to 1-row matrix
ABCD
efgh
IJKL

⍝∇ at pco

Back to: code

Back to: Workspaces