Janet 1.19.0-a0ddfcb Documentation
(Other Versions:
1.18.1
1.17.1
1.16.1
1.15.0
1.13.1
1.12.2
1.11.1
1.10.1
1.9.1
1.8.1
1.7.0
1.6.0
1.5.1
1.5.0
1.4.0
1.3.1
)
Flow
Janet has only two built in primitives to change flow while inside a function.
The first is the if
special form, which behaves as expected in most
functional languages. It takes two or three parameters: a condition, an
expression to evaluate to if the condition is Boolean true (ie: not nil
or false
), and an optional condition to evaluate to when the condition is
nil
or false
. If the optional parameter is omitted, the if
form evaluates to nil
.
(if (> 4 3)
"4 is greater than 3"
"4 is not greater then three") # Evaluates to the first statement
(if true
(print "Hey")) # Will print
(if false
(print "Oy!")) # Will not print
The second primitive control flow construct is the while
loop. The
while
form behaves much the same as in many other programming languages,
including C, Java, and Python. The while
loop takes two or more
parameters: the first is a condition (like in the if
statement), that is
checked before every iteration of the loop. If it is nil
or false
,
the while
loop ends and evaluates to nil
. Otherwise, the rest of
the parameters will be evaluated sequentially and then the program will return
to the beginning of the loop.
# Loop from 100 down to 1 and print each time
(var i 100)
(while (pos? i)
(print "the number is " i)
(-- i))
# Print ... until a random number in range [0, 1) is >= 0.9
# (math/random evaluates to a value between 0 and 1)
(while (> 0.9 (math/random))
(print "..."))
Besides these special forms, Janet has many macros for both conditional testing
and looping that are much better for the majority of cases. For conditional
testing, the cond
, case
, and when
macros can be used to
great effect. cond
can be used for making an if-else chain, where using
just raw if
forms would result in many parentheses. case
is
similar to switch
in C without fall-through, or case
in some
Lisps, though simpler. when
is like if
, but returns nil
if
the condition evaluates to nil
or false
, similar to the macro of
the same name in Common Lisp and Clojure. For looping, loop
, seq
,
and generate
implement Janet's form of list comprehension, as in Python
or Clojure.