Last active
August 29, 2015 14:02
-
-
Save cambiata/cfc9a48f4e24653fc95a to your computer and use it in GitHub Desktop.
Lazy Evaluation idea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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