Skip to content

Instantly share code, notes, and snippets.

@cambiata
Last active August 29, 2015 14:02
Show Gist options
  • Save cambiata/cfc9a48f4e24653fc95a to your computer and use it in GitHub Desktop.
Save cambiata/cfc9a48f4e24653fc95a to your computer and use it in GitHub Desktop.
Lazy Evaluation idea
// The following will be macromagically transformed...
class LazyExample implements Lazy
{
var values:Array<Int>;
public function new()
{
this.values = [3, 5, 1, 7];
}
@lazy public function getSum():Int
{
var result = 0;
for (value in values) result += value;
return result;
}
}
// ...into this:
class LazyExample implements Lazy
{
var values:Array<Int>;
public function new()
{
this.values = [3, 5, 1, 7];
}
private var _sum:Null<Int>;
public function getSum():Int
{
if (this._sum != null) return this._sum;
var result = 0;
for (value in values) result += value;
this._sum = result;
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment