3 ms·
Oh, also, you can abuse closures to do generator-like things with captures, thanks to the way they're implemented. use std const mkincrement = {inc
by ori_b 8y ago
Oh, also, you can abuse closures to do generator-like things with captures, thanks to the way they're implemented.
use std
const mkincrement = {inc
var x = 0
/* No GC, so the upwards closures are explicit */
-> std.fndup({
x += inc
-> x
})
}
const main = {
var g = mkincrement(5)
std.put("{}\n", g())
std.put("{}\n", g())
std.put("{}\n", g())
std.put("{}\n", g())
std.fnfree(g)
}
Will output:
5
10
15
20
The trick is that the function state needs to be in the capture.