From d291cb7faf2c1856e2cea486b0d935bbb16db2c9 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Wed, 13 Apr 2022 16:05:00 -0400 Subject: Documentation for Self/Swap --- docs/doc/index.html | 1 + docs/doc/primitive.html | 2 +- docs/doc/swap.html | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 docs/doc/swap.html (limited to 'docs/doc') diff --git a/docs/doc/index.html b/docs/doc/index.html index 821de18a..38f7d2cd 100644 --- a/docs/doc/index.html +++ b/docs/doc/index.html @@ -69,6 +69,7 @@
  • Scan (`)
  • Search functions (⊐⊒∊)
  • Select ()
  • +
  • Self and Swap (˜)
  • Self-search functions (⊐⊒∊⍷)
  • Shift functions (»«)
  • Solo, Couple, and Merge (≍>)
  • diff --git a/docs/doc/primitive.html b/docs/doc/primitive.html index dda31281..c4b48a20 100644 --- a/docs/doc/primitive.html +++ b/docs/doc/primitive.html @@ -431,7 +431,7 @@ ˜ -Self/Swap +Self/Swap {𝕩𝔽𝕨𝕩} Duplicate one argument or exchange two diff --git a/docs/doc/swap.html b/docs/doc/swap.html new file mode 100644 index 00000000..190b0d0a --- /dev/null +++ b/docs/doc/swap.html @@ -0,0 +1,79 @@ + + + + BQN: Self and Swap + + +

    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).

    + + + + + + + + + + + + + + + + + + + + +
    NameCallDefinition
    Self F˜𝕩𝕩F𝕩
    Swap𝕨F˜𝕩𝕩F𝕨
    +

    Since 𝕩 is always the left argument, these two definitions can be unified as {𝕩𝔽𝕨𝕩}, noting that Left becomes a plain identity function when the left argument 𝕨 isn't given.

    +

    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 useful 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"
    +
    +    " +" ˜ 0110010101
    +┌─       
    +╵" ++    
    +  + + +" 
    +        ┘
    +
    +

    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 from the argument 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 FG 𝕩 is the same as FG˜ 𝕩 by definition.

    -- cgit v1.2.3