<(^.^)> tsuki

Cool tricks in K

I have been programming in K for quite some time now. Mostly for codegolfing, but I have also used it to create a bunch of random programs.

In this post, I will talk cool tricks and tips and features that I have learned from codegolfing in this thing.

So, here we go.

Implicit function arguments

This is pretty basic, but I think it would still worth it to add this here.

K has a feature called implicit function arguments, which are arguments that are available automagically when you define a function.

These are incredibly handy, as you no longer has to define arguments by yourself.

The name of these are x, y, and z, standing for the first, the second, and the third argument, respectively.

Let's take a basic example:

f:{x*y}

f[5;10]

Here, we define a function f that takes two implcit arguments x and y, and multiply them together.

Then, we call the function f with the arguments 5 and 10. Here x is 5 and y is 10.

Split an array/string into pairs with #

In K, # is commonly used as the Length operator: Measuring the length of its right side.

But it's also used as the Reshape operator: Splitting its right side into a shape according to its left side.

With n 0N or 0N n, you can split a string into possibly even pairs.

Here's an example:

f:{5 0N#x}

f[1 1 1 1 1 1 1 1 1 1]

Here, we are defining function f that will reshape an array of 10 number 1s into 5 pairs, which output this:

(1 1
 1 1
 1 1
 1 1
 1 1)

Note that possibly even is used here, because in some cases where n is not divisible by the length of the array, it can caused uneven distribution, e.g.

f:{7 0N#x}

f[1 1 1 1 1 1 1 1 1 1]

Outputs this:

(,1
 ,1
 1 1
 ,1
 1 1
 ,1
 1 1)

Inline assignment in expression

It's also possible to define a variable in the middle of an expression.

This usually saves you a byte or two from defining it seperately, which requires ;.

Here's another example from one of my codegolf answers on Separate a list into even-indexed and odd-indexed parts:

{t:x@&(0=2!)'!#x;t,x^t} / 23 bytes
{t,x^t:x@&(0=2!)'!#x}   / 21 bytes

Here, instead of defining t seperately (like my old solution), we assign it while it is in the expression itself, saving us 2 bytes.

Turn the function into a train

This one is also pretty basic, but sometimes it's easy to forget this.

If your function only requires one argument and use that argument only once, then you can turn it into a train by using the Right (:) operator.

Think of a train like a straight line of conveyor belts: The input travels through the line, going through every operator that is in it.

Here is another codegolf answer that I did on Are All Items The Same?, which was massively golfed a whole 7 bytes.

{(+/x=*x)~#x} / 10 bytes (Old solution)
{#?x}         / 5 bytes
#?:           / 3 bytes

We will not look into the old solution, but just focus on the two different solutions below instead.

As you can see, we use the Right operator to completely eliminate x, which resulted in our function no longer a function, and then we remove the curly brackets to finish the finaly answer.

Note that if your function uses an only monadic operator (which is rare), or are written so that it will always take the input on its right side (i.e. monadic), then : is not needed.

f:2+
g:#:

Here, the function f will always take the input on its right side and + 2 to it, so no : here, but g uses #, which is both a monadic and dyadic operator, so we need to use : to specify the language that # is monadic in this case.


Note that when writing this post, I only use what I know best about my tricks, so there's a chance I could be wrong or I miss something.

If that's the case, consider email me about the problem.

#k #post #programming #tricks