News for hackers. Stuff that matters.
Re:Myrddin Programming Language by ori_b on Sunday June 03, 2018 @08:24PM (#17223418)
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.

There are no comments on this comment yet.