Contents

Searching...

Built-ins

Batteries included!

Kal does not come with a separate standard library. Rather, everything is intrinsic to the interpreter. This chapter covers general intrinsics.

Kal exposes a global
ARGS
list variable that contains all the command line arguments. Command line arguments are additional pieces of information passed to the interpreter while passing the program.
stdout ARGS "\n".

args.kal

$ kal args.kal sun earth moon
Arguments can be extracted from
ARGS
as you would from a normal list.
stdout ARGS[3] "\n".

args.kal

$ kal args.kal sun earth moon
You can make Kal programs end early using the
exit
statement.
exit 100.
stdout "Won't run!".

exit.kal

$ kal exitCode.kal
$ echo $?
Text can be colorized using the
style
statement. It accepts color, mode or a combination of both.
style "red" "bold".
stdout "Text Red!\n".

color.kal

Valid colors and modes are:
ColorsModes
blackbold
reditalic
greenunderline
yellowblink
bluebg
magentastrike
cyanreset
white

Use
"reset"
to go back to default.
style "reset".

reset.kal

Kal comes with statements for working with lists and dictionaries. Those are documented as follows.

Add a new element to a list using the
push
statement.
var data = [1, 2, 3].
stdout "Before: " data "\n".
push data 4. stdout "After: " data "\n".

push.kal

By default Kal will push an element to the end of the list. Optionally provide the index where you want to push the element to as the third argument.
var data = [1, 2, 3].
stdout "Before: " data "\n".
push data 4 1. stdout "After: " data "\n".

pushIndex.kal

Fetch the first element of a list using the
first
statement.
var data = ["x", "y", "z"].
first data -> f.
stdout "First: " f "\n".

first.kal

Fetch the last element of a list using the
last
statement.
var data = ["x", "y", "z"].
last data -> l.
stdout "Last: " l "\n".

last.kal

Remove the last element of the list using the
pop
statement.
var data = [1, 2, 3].
stdout "Before: " data "\n".
pop data. stdout "After: " data "\n".

pop.kal

Remove the first element of the list using the
popFirst
statement.
var data = [1, 2, 3].
stdout "Before: " data "\n".
popFirst data. stdout "After: " data "\n".

popFirst.kal

Reverse a list using the reverse statement.
var data = [1, 2, 3].
stdout "Before: " data "\n".
reverse data. stdout "After: " data "\n".

reverse.kal

Concatenate lists using the
extend
statement.
var listX = [1, 2, 3],
    listY = [4, 5, 6].
extend listX listY -> listZ. stdout listZ "\n".

extend.kal

Flatten nested lists into a linear list using the
flat
statement.
var data = [
    [1, 2, 3],
    [4, 5, 6]
].
flat data -> line. stdout line "\n".

flat.kal

There could be many levels of list nesting, simple invocation of the
flat
statement only removed the top level Pass in the number of levels you want to resolve in case of deeply nested lists.
var data = [
    [1, [2, 3]],
    [4, [5, 6]]
].
flat data -> line. stdout "First Level: " line "\n".
;; resolve of 2 levels flat data 2 -> secondLine. stdout "Second Level: " secondLine "\n".

flatLevels.kal

For a dictionary, use the
keys
statement to obtain a list of all keys.
var planet = #(
    name -> "Earth",
    is -> "Planet"
).
keys planet -> dictKeys. stdout "Keys = " dictKeys "\n"

keys.kal

Similarly, use the
values
statement to obtain a list of all values.
var planet = #(
    name -> "Earth",
    is -> "Planet"
).
values planet -> dictValues. stdout "Values = " dictValues "\n".

values.kal

To obtain combined key-value pairs, use the
items
statement.
var planet = #(
    name -> "Earth",
    is -> "Planet"
).
items planet -> dictItems. stdout "Items = " dictItems "\n".

items.kal

Check whether a key exists or not in a dictionary using the
exists
statement.
var planet = #(
    name -> "Earth",
    is -> "Planet"
).
exists planet "name" -> x. exists planet "biomes" -> y
stdout "Name? " x "\n" "Biomes? " y "\n".

items.kal

Update an existing dictionary using the
update
statement.
update
takes in another dictionary and patches it over the original one. This adds or updates values in the original dictionary.
var planet = #(
    name -> "Earth",
    is -> "Planet"
).
var planetPatch = #( name -> "Home", biomes -> ["Forest", "Ocean"] ).
update planet planetPatch -> final. stdout final "\n".

update.kal

Finally, use the
len
statement to get the length of a list, dictionary or a string. It returns the number of elements, the number of keys and the number of characters for these respective types.
var data = [1, 2, 3].
len data -> listLen.
var info = #( name -> "Earth", home -> "yes" ). len info -> dictLen.
var day = "24 hours". len day -> strLen. stdout listLen "\n" dictLen "\n" strLen "\n".

len.kal

Throughout the examples we used lists and dictionaries as variables. However, you can also pass them to these statements as literals.