Search code examples
k

When is the EACH operator extension necessary in K besides mod/rotate?


In the K language, an operator can be suffixed with an apostrophe to apply to each element in an array:

  8 +' 2 4 10
10 12 18
  9 <' 3 10 2
0 1 0
  8 -' 1 7 10
7 1 -2

However, in each of those cases, the apostrophe is not required, because these dyadic verbs naturally apply across the array:

  8 + 2 4 10
10 12 18
  9 < 3 10 2
0 1 0
  8 - 1 7 10
7 1 -2

The only place I have yet seen there to be a difference is with the ! verb, which applies the modulo operation for each element of the array when decorated with ' but acts as a rotate when not decorated:

  3 !' 1 2 3 4 5
0 1 0 3 3
  3 ! 1 2 3 4 5
4 5 1 2 3

Are there any other places in K where the decorated (apostrophe-d) version of a verb is different from the undecorated case? (I'm new to K so very likely am missing such cases!)


Solution

  • Lots of places, especially once you start dealing with your own functions:

      {"go",x} ("";"ing";"ne")
    "g"
    "o"
    ""
    "ing"
    "ne"
    
      {"go",x}' ("";"ing";"ne")
    "go"
    "going"
    "gone"
    

    (you might prefer to write that as ,["go"]')