179 lines
3.1 KiB
Plaintext
179 lines
3.1 KiB
Plaintext
![]() |
---
|
||
|
description: k files
|
||
|
globs: *.k
|
||
|
---
|
||
|
Use ngn/k for k files.
|
||
|
|
||
|
ngn/k Cheatsheet
|
||
|
|
||
|
Basics
|
||
|
|
||
|
Running k Code
|
||
|
|
||
|
1+2 / Addition
|
||
|
3-1 / Subtraction
|
||
|
2*3 / Multiplication
|
||
|
10%3 / Division (floating point if needed)
|
||
|
|
||
|
Right-to-Left Evaluation
|
||
|
|
||
|
1+5*2-2 / Evaluates as: 1 + (5 * (2 - 2)) = 1
|
||
|
1+(5*2)-2 / Parentheses force standard order
|
||
|
|
||
|
Assignments
|
||
|
|
||
|
a: 10 / Assign 10 to a
|
||
|
b: a+5 / Assign 15 to b
|
||
|
|
||
|
Comments
|
||
|
|
||
|
/ This is a comment
|
||
|
|
||
|
Data Types
|
||
|
|
||
|
Atoms (Scalars)
|
||
|
|
||
|
42 / Integer
|
||
|
3.14 / Float
|
||
|
"c" / Character
|
||
|
`symbol / Symbol
|
||
|
|
||
|
Vectors (1D Arrays)
|
||
|
|
||
|
1 2 3 4 / Integer vector
|
||
|
1.2 3.4 5.6 / Float vector
|
||
|
"abcd" / Character vector
|
||
|
`foo`bar`baz / Symbol vector
|
||
|
|
||
|
Lists (Nested Arrays)
|
||
|
|
||
|
(1 2 3; 4 5) / List of vectors
|
||
|
("abc"; 1 2 3) / Mixed-type list
|
||
|
|
||
|
Vectorized Operations
|
||
|
|
||
|
Unlike traditional languages, operations apply element-wise:
|
||
|
|
||
|
1 2 3 + 4 / 5 6 7
|
||
|
2 * 1 2 3 / 2 4 6
|
||
|
|
||
|
Operations also work on nested lists:
|
||
|
|
||
|
(1 2; 3 4) + 10 / (11 12; 13 14)
|
||
|
|
||
|
Indexing
|
||
|
|
||
|
x: 10 20 30 40
|
||
|
x[2] / Returns 30
|
||
|
x[1 3] / Returns 20 40
|
||
|
|
||
|
For lists:
|
||
|
|
||
|
l: (10 20 30; "abc"; `sym)
|
||
|
l[1] / Returns "abc"
|
||
|
l[0][2] / Returns 30
|
||
|
|
||
|
Built-in Verbs
|
||
|
|
||
|
Arithmetic
|
||
|
|
||
|
+ Flip (transpose matrix), Add
|
||
|
- Negate (monadic), Subtract (dyadic)
|
||
|
* First (monadic), Multiply (dyadic)
|
||
|
% Square root (monadic), Divide (dyadic)
|
||
|
! Factorial (monadic), Modulo (dyadic)
|
||
|
|
||
|
Comparisons
|
||
|
|
||
|
< Less than, Ascend (monadic)
|
||
|
> Greater than, Descend (monadic)
|
||
|
= Equal, Group (monadic)
|
||
|
~ Match (deep equality), Not (monadic)
|
||
|
|
||
|
Logical Operations
|
||
|
|
||
|
& Min (dyadic), Where (monadic)
|
||
|
| Max (dyadic), Reverse (monadic)
|
||
|
|
||
|
Data Manipulation
|
||
|
|
||
|
, Concat, Enlist (monadic)
|
||
|
^ Without (dyadic), Null (monadic)
|
||
|
# Reshape (dyadic), Length (monadic)
|
||
|
_ Drop (dyadic), Floor (monadic)
|
||
|
$ Cast (dyadic), String (monadic)
|
||
|
? Find (dyadic), Unique (monadic), Random selection
|
||
|
@ Apply (indexing and function calls)
|
||
|
. Drill (deep indexing)
|
||
|
|
||
|
Adverbs (Higher-order Operations)
|
||
|
|
||
|
Each (')
|
||
|
|
||
|
2*'1 2 3 / Returns 2 4 6
|
||
|
|
||
|
Reduce (/)
|
||
|
|
||
|
+/1 2 3 / Returns sum: 6
|
||
|
|
||
|
Scan (\)
|
||
|
|
||
|
+\1 2 3 / Returns running sum: 1 3 6
|
||
|
|
||
|
Each-Prior (':)
|
||
|
|
||
|
+': 1 2 3 4 / Returns 1 3 5 7 (pairwise sum)
|
||
|
|
||
|
Each-Right (/:)
|
||
|
|
||
|
1 2*/:3 4 / Returns (3 6; 4 8)
|
||
|
|
||
|
Each-Left (\:)
|
||
|
|
||
|
1 2*\:3 4 / Returns (3 4; 6 8)
|
||
|
|
||
|
Window (i':)
|
||
|
|
||
|
3':!10 / Sliding window
|
||
|
|
||
|
Stencil (i f':)
|
||
|
|
||
|
3+/':!10 / Stencil sum
|
||
|
|
||
|
Bin Search (X')
|
||
|
|
||
|
1 3 5 7 9'8 9 0 / Bin classification
|
||
|
|
||
|
Array-Oriented Typing vs Traditional Languages
|
||
|
|
||
|
Key Differences
|
||
|
|
||
|
Operations are implicitly vectorized
|
||
|
|
||
|
No need for loops to apply operations to arrays
|
||
|
|
||
|
Example: 2 * 1 2 3 produces 2 4 6 directly
|
||
|
|
||
|
Data is homogeneously typed
|
||
|
|
||
|
Unlike Python lists, k enforces consistent typing in arrays
|
||
|
|
||
|
Example: 1 2 "a" would cause an error
|
||
|
|
||
|
Functions behave like array transformations
|
||
|
|
||
|
Monadic (single argument) and dyadic (two arguments) versions exist
|
||
|
|
||
|
Example: * 1 2 3 returns 1 (first element), 1*2 3 returns 2 3
|
||
|
|
||
|
Indexing and function application use the same syntax
|
||
|
|
||
|
f[2] calls f with argument 2
|
||
|
|
||
|
arr[2] retrieves index 2 from arr
|
||
|
|
||
|
Example: Simple Statistics
|
||
|
|
||
|
x: 1 2 3 4 5
|
||
|
mean: (+/x) % #x / Sum divided by count
|
||
|
mean / this will print to screen aka return from main
|