Najova

DocsBuilt-in functions

Lists and tables

A list keeps values in order, counted from 1. A table finds values by their keys.

$list

$list[a;b;c...]

A list of values.

list.naja
$let[fruit;$list[apple;pear;plum]]
$print[$count[$get[fruit]] fruit, first $item[$get[fruit];1]]
najova run list.naja
3 fruit, first apple

$table

$table[key;value;key;value...]

A table: values found by their keys.

table.naja
$let[prices;$table[apple;2;pear;3]]
$print[A pear costs $item[$get[prices];pear]]
najova run table.naja
A pear costs 3

$item

$item[list;number] or $item[table;key]

An item of a list (the first is 1) or the value of a key of a table.

item.naja
$let[days;$list[Mon;Tue;Wed]]
$print[$item[$get[days];2]]
$print[$item[$get[days];9]]
najova run item.naja
Tue
nil
An item that is not there gives nil.

$count

$count[list]

How many items a list or table has, or how many letters a text has.

count.naja
$print[$count[$list[1;2;3]]]
$print[$count[hello]]
najova run count.naja
3
5

$contains

$contains[list;value]

true when the list holds the value, the table has the key, or the text holds the other text.

contains.naja
$let[bag;$list[key;map]]
$print[$contains[$get[bag];key]]
$print[$contains[hello world;world]]
najova run contains.naja
true
true

$keys

$keys[table]

A list of the keys of a table.

keys.naja
$let[stock;$table[apples;4;pears;0]]
$print[$keys[$get[stock]]]
najova run keys.naja
[apples; pears]

$add

$add[list;value]

Puts a value at the end of a list.

add.naja
$let[queue;$list[]]
$add[$get[queue];Ana]
$add[$get[queue];Ben]
$print[$get[queue]]
najova run add.naja
[Ana; Ben]

$insert

$insert[list;number;value]

Puts a value into a list at a place (the first is 1); a table gets the key.

insert.naja
$let[queue;$list[Ana;Ben]]
$insert[$get[queue];1;Cy]
$print[$get[queue]]
najova run insert.naja
[Cy; Ana; Ben]

$remove

$remove[list] or $remove[list;number]

Takes the last item (or the one at a place, or a table's key) out and gives it.

remove.naja
$let[stack;$list[a;b;c]]
$print[Took $remove[$get[stack]]]
$print[Left $get[stack]]
najova run remove.naja
Took c
Left [a; b]

$sort

$sort[list] or $sort[list;key]

A sorted copy of a list (numbers small to big, then text A to Z); with a key, a list of tables sorted by that key.

sort.naja
$print[$sort[$list[5;1;3]]]
$let[team;$list[$table[name;Zoe;age;30];$table[name;Al;age;25]]]
$each[p;$sort[$get[team];age]]
  $print[$get[p.name]]
$endeach
najova run sort.naja
[1; 3; 5]
Al
Zoe

$reverse

$reverse[list]

A copy of a list (or a text) the other way round.

reverse.naja
$print[$reverse[$list[1;2;3]]]
$print[$reverse[stressed]]
najova run reverse.naja
[3; 2; 1]
desserts

$slice

$slice[list;from] or $slice[list;from;to]

Part of a list or a text, from one place up to another (the first is 1, -1 is the last).

slice.naja
$let[n;$list[1;2;3;4;5]]
$print[$slice[$get[n];2;4]]
$print[$slice[Najova;1;3]]
najova run slice.naja
[2; 3; 4]
Naj