Remix.run Logo
NetMageSCW 4 hours ago

My favorite APL one liner reduces spans of consecutive spaces in a string to one space by doing boolean algebra on the vector of which characters are spaces. APL provided me with a whole different perspective on vector style operation (it was my second language to be familiar with after Basic due to finding an APL introductory book at a discount shop). I find that way of thinking has been very helpful in developing with LINQ in C# and my personal library has many methods inspired by APL operators.

icen 4 hours ago | parent [-]

Do you remember what it was? This is how I would spell that in BQN, and you could write something very similar in APL (you don't have shift, so you'd have to write 1 drop 0 cat swap instead)

    ((«¬∘∧⊢)' '=⊢)⊸/
This works by building a boolean mask of spaces, and converting it to a mask of 'is a space, preceded by a space', negates that, and replicates out by that inverted mask (i.e. is not a space preceded by a space):

Here's stepping through it with some input.

       (' '=⊢)  "this  is a  sentence with       many   spaces"
    ⟨ 0 0 0 0 1 1 0 0 1 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 0 ⟩
       («' '=⊢)  "this  is a  sentence with       many   spaces"
    ⟨ 0 0 0 1 1 0 0 1 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 ⟩
       ((«∧⊢)' '=⊢)  "this  is a  sentence with       many   spaces"
    ⟨ 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 ⟩
       ((«¬∘∧⊢)' '=⊢)  "this  is a  sentence with       many   spaces"
    ⟨ 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 ⟩
       ((«¬∘∧⊢)' '=⊢)⊸/  "this  is a  sentence with       many   spaces"
    "this is a sentence with many spaces"
dzaima 2 hours ago | parent | next [-]

Some alternative spellings:

    (¬∘∧⟜«' '=⊢)⊸/
    (¬·«⊸∧' '=⊢)⊸/
    {¬«⊸∧' '=x}⊸/ # should have double-struck x here (U+1D569), but hn removes it
icen 2 hours ago | parent [-]

There's also the much less vector oriented spelling:

    (1 ∾˜ "  "⊸(¬∘⍷))⊸/
which removes the first space of each occurrence of double spacing
tosh 2 hours ago | parent | prev [-]

in k:

  s:"this  is a  sentence with       many   spaces"
  
  s@&~0&':s=" "