jtye/k playground docs


editor
out

          
js

          
ref
+ type   add   ' each prior bin `js`
- neg    sub   / over right join dec
* sqr    mul   \ scan left split enc
% sqrt   div   inv idiv mod         
& flip   min                  atom  
| rev    max                  atomic
< up     less                  curry
> down   more                    rec
= freq   eql                        
~ not    match                      
. value        parse                
! til    dict  token key where      
@ first  at    amend                
? uniq   find  rand                 
^ sort   cut                        
# count  take             if[c;;;;;]
_ floor  drop            while[c;;;]
, list   cat           for[a;b;c;;;]
$ string print       try[]catch[e;;]



examples

monadic dyadic triadic
from the ref card you can see functions have up to 3 different variants,
1. is monadic form, 2. is dyadic form and 3. is triadic form, which just means it takes 1, 2 or 3 parameters.
monadic: @!10 -> find first in the list
dyadic: 2@!10 -> find value 2 in list
triadic: @[!10,5,2] -> update list at index 5, replace with value 2
these are equivalent
x[i]+y
@[x;i;+;y]
      

matrix
use dyadic ^ to cut list at given indices: (5*!5)^!25
to translate to js it would be cut([0,5,10...], [1,2,3...])
resulting in:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24

the shorthand for matrix generation is 5^!25, this would do the same as above.
to see where the items are cut you need to use $
      

updating items in a vector
you can access items in a vector with square brackets and a vector of indices:
x:0*!9
i:4 5
x[i]:1-x[i]

idiv and mod
idiv[3;!9]    0 0 0 1 1 1 2 2 2
mod[3;!9]     1 2 3 1 2 3 1 2 3

elementary cellular automata rule 30
v: 0 1 1 1 0     / initial generation
l:1_v,@v         / left of v, so take the first item and add it to the end of the vector, then remove the first item
r:|1_(|v),@(|v)  / i could not figure out how to do the right version other ways than this: reverse the result of left, then do another left operation and reverse it back.
n:mod[2;l+v|r]   / v|r combines both vectors into max of each respective item, then add the left to that. After that we mod 2 it so that we round back to 0, and that is rule 30.