lama_byterun/stdlib/Lazy.lama

25 lines
466 B
Text
Raw Normal View History

2020-02-20 12:43:52 +03:00
-- Lazy.
-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020
--
-- Deferred computations.
public fun makeLazy (f) {
var value, set = false;
fun () {
2020-02-15 22:58:43 +03:00
if set
then value
2020-02-20 12:43:52 +03:00
else
var c; -- need this temporary since in value := f () value would
2020-02-20 12:43:52 +03:00
-- create an intermediate managed pointer
set := true;
c := f ();
value := c;
c
2020-02-15 22:58:43 +03:00
fi
}
}
public fun force (f) {
f ()
}