Self and Swap

Self/Swap π”½Λœ 𝕩 𝔽 𝕩 𝕨 π”½Λœ 𝕩 𝔽 𝕨 𝕩

Have the arguments to a function, but not in the right places? Self/Swap (˜) will fix it for you. There are only two APL-style 1-modifiersβ€”that is, operands used as functions and applied to argumentsβ€”that make sense, and ˜ is both of them. It always calls its operand with two arguments: if there are two arguments to begin with, they are exchanged (Swap), and if there's only one, it's used for both arguments (Self).

Name Call Definition
Self FΛœπ•© 𝕩F𝕩
Swap 𝕨FΛœπ•© 𝕩F𝕨

Since 𝕩 always becomes the left argument, these two definitions can be unified as {π•©π”½π•¨βŠ£π•©}, noting that Left returns 𝕨 if it's given and 𝕩 if not.

Swap is arguably less transformative. Some common examples are -˜ and ÷˜, since these two functions run the wrong way for BQN's evaluation order. This is very often useful in tacit programming, and less needed for explicit code. While it sometimes allows for shorter code by making a pair of parentheses unnecessary (say, (aΓ—b)-c is c-˜aΓ—b), I personally don't think this is always a good idea. My opinion is that it should be used when it makes the semantics a better fit for BQN, but putting the primary argument on the right and a secondary or control argument on the left.

↗️
    'a' β‹ˆΛœ 'b'
"ba"

    " +" ⊏˜ 0β€Ώ1β€Ώ1β€Ώ0β€Ώ0≍1β€Ώ0β€Ώ1β€Ώ0β€Ώ1
β”Œβ”€       
β•΅" ++    
  + + +" 
        β”˜

Moving on, Self re-uses one argument twice. In this way it's a little like Over, which re-uses one function twice. A common combination is with Table, ⌜˜, so that the operand function is called on each combination of elements in 𝕩 to form a square result. For example, =⌜˜ applied to ↕n gives the identity matrix of size n.

↗️
    Γ—Λœ 4
16

    =⌜˜ ↕3
β”Œβ”€       
β•΅ 1 0 0  
  0 1 0  
  0 0 1  
        β”˜

Note that Self isn't needed with Before (⊸) and After (⟜), which essentially have a copy built in: for example F⊸G 𝕩 is the same as F⊸G˜ 𝕩 by definition.