Examples

Practical MKS snippets.

Copyable examples for loops, arrays, strings, entities, extend, modules, watch/defer, tests, and the standard library.

repeat swap arrays strings entity extend watch / defer tests stdlib
/examples/showcase.mks
$ mks examples/showcase.mks
loading std/math... ok
binding watcher... ok
running tests... pass
0

copyable snippets

8

feature groups

0

unsupported alias patterns

How to read this page

Start with a small snippet, then combine patterns.

Each card is intentionally small. Learn the shape, copy it, then combine loops, functions, modules, and stdlib helpers into larger programs.

1. Pick a feature 2. Copy the snippet 3. Adapt one line
Basics

Hello world

Writeln("hello, mks");
Arrays

Index read + write

var xs =: [1, 2, 3];

Writeln(xs[0]);
xs[1] =: 20;

Writeln(xs[1]);
Basics

Variables + swap

var a =: 1;
var b =: 2;

a <--> b;

Writeln(a);
Writeln(b);
Functions

Recursion

fnc fact(n) ->
  if (n <= 1) ->
    return 1;
  <-

  return n * fact(n - 1);
<-

Writeln(fact(5));
Control

if / else

var n =: 8;

if (n ?= 8) ->
  Writeln("ok");
<- else ->
  Writeln("no");
<-
Control

repeat

repeat 3 ->
  Writeln("ping");
<-

repeat i in 3 ->
  Writeln(i);
<-
Control

for loop

for (var i =: 0; i < 3; i =: i + 1) ->
  Writeln(i);
<-
Functions

Function + call

fnc add(a, b) ->
  return a + b;
<-

Writeln(add(2, 3));
Runtime

watch / on change

var x =: 1;
watch x;

on change x ->
  Writeln("x changed to", x);
<-

x =: 7;
GC

Heap pressure sketch

var root =: [1, 2, 3];
var temp =: [4, 5, 6];

temp =: null;

Writeln(root[0]);
Runtime

defer

defer ->
  Writeln("closing scope");
<-

Writeln("work...");
Writeln("done");
Entity

Simple entity

entity Point(x, y) ->
  init ->
    self.x =: x;
    self.y =: y;
  <-

  method len2() ->
    return self.x * self.x + self.y * self.y;
  <-
<-

var p =: Point(2, 3);
Writeln(p.len2());
Extend

extend array

extend array ->
  method sum() ->
    var s =: 0;
    var i =: 0;

    while (i < self.len()) ->
      s =: s + self[i];
      i =: i + 1;
    <-

    return s;
  <-
<-

Writeln([1,2,3].sum());
Modules

using std/math

using "std/math";

Writeln(abs(-5));
Writeln(square(4));
Writeln(min(10, 3));
Writeln(max(10, 3));
Modules

Local module import

// math.mks
fnc add(a, b) ->
  return a + b;
<-

// main.mks
using "math.mks";
Writeln(add(2, 2));
Tests

test / expect

test "math add" ->
  expect(1 + 1 ?= 2);
<-
Control

break / continue

for (var i =: 0; i < 10; i =: i + 1) ->
  if (i ?= 2) ->
    continue;
  <-

  if (i ?= 5) ->
    break;
  <-

  Writeln(i);
<-
Strings

String output

var name =: "MKS";
var event =: "x changed";

Writeln("hello", name);
Writeln(event);
Resolver

Import lookup order

using "path/to/file";
using "std/math";

// lookup:
// current file dir
// current working dir
// installed std path
Numbers

Arithmetic pack

Writeln(1 + 2);
Writeln(10 - 3);
Writeln(4 * 5);
Writeln(20 / 4);
Writeln(10 % 3);
Compare

Comparison operators

Writeln(1 ?= 1);
Writeln(1 !? 2);
Writeln(5 > 3);
Writeln(3 >= 3);
Writeln(2 <= 5);
Arrays

Empty array + inject

var arr =: [];
Writeln(arr.len());

arr.inject(10);
Writeln(arr[0]);
Writeln(arr.len());
Arrays

Nested arrays

var arr =: [[1, 2], [3, 4]];

Writeln(arr[0][1]);
Writeln(arr[1][0]);
Strings

String methods

var s =: "Hello";

Writeln(s[1]);
Writeln(s.upper());
Writeln(s.lower());
Writeln(s.len());
Control

while counter

var i =: 0;

while (i < 3) ->
  Writeln(i);
  i =: i + 1;
<-
Control

Loop guard pattern

var x =: 0;

while (x < 6) ->
  x =: x + 1;

  if (x ?= 2) ->
    continue;
  <-

  if (x ?= 4) ->
    break;
  <-

  Writeln(x);
<-
Tests

Expected pass line

test "math add" ->
  expect(1 + 1 ?= 2);
<-

// prints:
// [PASS] math add
Defer

Two defers

defer ->
  Writeln("d1");
<-

defer ->
  Writeln("d2");
<-

Writeln("body");
Entity

Mutable entity field

entity Buba(name, power) ->
  init ->
    self.name =: name;
    self.power =: power;
  <-

  method boost(amount) ->
    self.power =: self.power + amount;
  <-
<-

var b =: Buba("buba", 12);
b.boost(5);
Writeln(b.power);
Extend

extend array last

extend array ->
  method last() ->
    if (self.len() ?= 0) ->
      return null;
    <-
    return self[self.len() - 1];
  <-
<-

Writeln([1, 2, 3].last());
std/bool

Boolean helpers

using "std/bool";

Writeln(true);
Writeln(false);
Writeln(not(true));
Writeln(and(true, false));
Writeln(or(false, true));
std/array

Array helpers

using "std/array";

var xs =: [1, 2, 3];
push(xs, 4);

Writeln(first(xs));
Writeln(last(xs));
Writeln(sum(xs));
Writeln(avg(xs));
std/string

String wrappers

using "std/string";

Writeln(len("Hello"));
Writeln(upper("Hello"));
Writeln(lower("Hello"));
Writeln(contains("Hello", "ell"));
std/time

Time helpers

using "std/time";

Writeln(now());
Writeln(timestamp());
sleep(100);
std/math

More math

using "std/math";

Writeln(sqrt(9));
Writeln(pow(2, 3));
Writeln(floor(1.9));
Writeln(ceil(1.1));
Modules

Duplicate import skip

using "math.mks";
Writeln(PI);
Writeln(get_circle_area(2));

using "math.mks"; // duplicate skipped
Writeln(VERSION);
std/array

Reverse copy

using "std/array";

var xs =: [1, 2, 3];
var reversed =: reverse(xs);

Writeln(reversed);
std/array

Slice array

using "std/array";

var xs =: [10, 20, 30, 40];
var part =: slice(xs, 1, 2);

Writeln(part);
std/string

Replace text

using "std/string";

var text =: "hello mks";

Writeln(replace(text, "mks", "MKS"));
std/string

Split and join

using "std/string";

var parts =: split("a,b,c", ",");

Writeln(join(parts, "-"));
std/string

Prefix and suffix

using "std/string";

var name =: "MKS-lang";

Writeln(starts_with(name, "MKS"));
Writeln(ends_with(name, "lang"));
Control

Nested if in loop

repeat i in 5 ->
  if (i > 2) ->
    Writeln("big", i);
  <- else ->
    Writeln("small", i);
  <-
<-
Control

Manual countdown

var n =: 3;

while (n > 0) ->
  Writeln(n);
  n =: n - 1;
<-

Writeln("go");
Arrays

Swap array values

var xs =: [1, 2, 3];

xs[0] <--> xs[2];

Writeln(xs);
Watch

Watch score changes

var score =: 0;
watch score;

on change score ->
  Writeln("score", score);
<-

score =: score + 10;
score =: score + 5;
Extend

extend string shout

extend string ->
  method shout() ->
    return self.upper();
  <-
<-

Writeln("mks".shout());
CLI

Run + inspect

mks examples/showcase.mks
mks --repl
mks --version
mks --help
Tests

Golden tests

test "math add" ->
  expect(1 + 1 ?= 2);
<-

// repository root
// ./tests.sh