L1VM - pure functions
L1VM - pure functions
Brackets now has support for “pure” functions. A pure function has no side effects and returns the same output when the input stays the same. This is true for a function which returns the square of a number: 2 -> 4, 3 -> 9 and so on. A pure function name ends with an uppercase “P”. A pure function can only call other pure functions. And can not access a global variable. Here is an example:
// hello-pure.l1com - Brackets
//
// This shows how "pure" functions with no global state or side effects can be defined.
// You can only call other "pure" functions from a "pure" function!
// Pure functions end with an uppercase "P" in the name.
//
#include <intr.l1h>
#include <misc-macros.l1h>
(main func)
(set int64 1 zero 0)
(set int64 1 one 1)
(set double 1 num 4.0)
(set double 1 ret 0.0)
(set int64 1 f 0)
(num :squareP !)
(ret stpopd)
print_d (ret)
print_n
(num :do_squareP !)
(ret stpopd)
print_d (ret)
print_n
exit (zero)
(funcend)
(variable-local-only-on)
(squareP func)
#var ~ squareP
(set double 1 num~ 0.0)
(set double 1 ret~ 0.0)
(num~ stpopd)
{ret~ = num~ * num~}
(ret~ stpushd)
(funcend)
(do_squareP func)
#var ~ do_squareP
(set double 1 num~ 0.0)
(num~ stpopd)
(num~ :squareP !)
(num~ stpopd)
(num~ stpushd)
(funcend)
Have fun!