Najova

DocsBuilt-in functions

Najoutines

Do several things at once. A Najoutine is a function running alongside the rest; channels pass values between them.

$spawn

$spawn[function;values...]

Starts a Najoutine: the function runs alongside the rest, from when the code running now waits or ends. Gives its handle (for $await and $cancel).

spawn.naja
$function[cook;dish;minutes]
  $wait[$get[minutes] / 100]
  $return[$get[dish] is ready]
$endfunction
$let[a;$spawn[cook;Soup;3]]
$let[b;$spawn[cook;Bread;1]]
$print[Both started]
$print[$await[$get[a]]]
$print[$await[$get[b]]]
najova run spawn.naja
Both started
Soup is ready
Bread is ready

$await

$await[najoutine]

Waits until the Najoutine ends and gives what its function returned; a mistake in it comes back here.

await.naja
$function[slowAdd;x;y]
  $wait[0.02]
  $return[$get[x] + $get[y]]
$endfunction
$print[The sum is $await[$spawn[slowAdd;2;3]]]
najova run await.naja
The sum is 5

$cancel

$cancel[najoutine]

Stops a Najoutine and every Najoutine it started.

cancel.naja
$function[ticker]
  $loop[i;1;100]
    $print[tick $get[i]]
    $wait[0.1]
  $endloop
$endfunction
$let[t;$spawn[ticker]]
$wait[0.35]
$cancel[$get[t]]
$print[Stopped]
najova run cancel.naja
tick 1
tick 2
tick 3
tick 4
Stopped

$channel

$channel or $channel[size]

A channel for passing values between Najoutines. It holds up to size values before $put waits (none: $put waits for a $take).

channel.naja
$let[box;$channel[2]]
$put[$get[box];first]
$put[$get[box];second]
$print[$take[$get[box]] then $take[$get[box]]]
najova run channel.naja
first then second

$put

$put[channel;value]

Puts a value into a channel, waiting while the channel is full.

put.naja
$let[orders;$channel]
$function[kitchen;ch]
  $print[Cooking $take[$get[ch]]]
$endfunction
$spawn[kitchen;$get[orders]]
$put[$get[orders];pizza]
$print[Order sent]
najova run put.naja
Cooking pizza
Order sent

$take

$take[channel]

Takes the next value from a channel, waiting until there is one (nil once the channel is closed and empty).

take.naja
$let[ch;$channel[3]]
$function[numbers;ch]
  $loop[i;1;3]
    $put[$get[ch];$get[i]]
  $endloop
  $close[$get[ch]]
$endfunction
$spawn[numbers;$get[ch]]
$let[n;$take[$get[ch]]]
$while[$get[n] != nil]
  $print[Got $get[n]]
  $set[n;$take[$get[ch]]]
$endwhile
najova run take.naja
Got 1
Got 2
Got 3

$select

$select[channels...]

Waits until one of the channels has a value and gives a list of the channel and the value (the first one ready, in the order given).

select.naja
$let[fast;$channel[1]]
$let[slow;$channel[1]]
$function[send;ch;word;delay]
  $wait[$get[delay]]
  $put[$get[ch];$get[word]]
$endfunction
$spawn[send;$get[slow];slow;0.05]
$spawn[send;$get[fast];fast;0.01]
$let[first;$select[$get[fast];$get[slow]]]
$print[First in: $item[$get[first];2]]
najova run select.naja
First in: fast

$close

$close[channel]

Closes a channel: $put into it fails; $take gives what is left, then nil.

close.naja
$let[ch;$channel[2]]
$put[$get[ch];last one]
$close[$get[ch]]
$print[$take[$get[ch]]]
$print[After that: $take[$get[ch]]]
najova run close.naja
last one
After that: nil

$najoutinesBlock

$najoutines

Runs what follows; Najoutines started inside belong to the block, which waits at $endnajoutines until all have ended. A mistake in one stops the others and comes back in the block.

najoutines.naja
$function[fetchPart;n]
  $wait[$get[n] / 100]
  $print[Part $get[n] done]
$endfunction
$najoutines
  $spawn[fetchPart;3]
  $spawn[fetchPart;1]
  $spawn[fetchPart;2]
$endnajoutines
$print[All parts done]
najova run najoutines.naja
Part 1 done
Part 2 done
Part 3 done
All parts done