Mercurial > hg > ishara
annotate general/funutils/lazy.m @ 61:eff6bddf82e3 tip
Finally implemented perceptual brightness thing.
author | samer |
---|---|
date | Sun, 11 Oct 2015 10:20:42 +0100 |
parents | e44f49929e56 |
children |
rev | line source |
---|---|
samer@4 | 1 % lazy - Lazy evaluator, or memoiser |
samer@4 | 2 % |
samer@4 | 3 % lazy :: (unit -> A) -> (unit -> A). |
samer@4 | 4 % lazy :: (unit -> action A) -> (unit -> action A). |
samer@4 | 5 % |
samer@4 | 6 % If x=lazy(f), then f is not called until x is |
samer@4 | 7 % called. Then x() returns x1=f(). Thereafter, |
samer@4 | 8 % x() returns x1 and f is never called again. |
samer@4 | 9 % |
samer@4 | 10 % Eg, let x=lazy(@()rand). |
samer@4 | 11 % Then x() returns the same random number each |
samer@4 | 12 % time it is called. |
samer@4 | 13 function f=lazy(thunk) |
samer@4 | 14 value=thunk; |
samer@4 | 15 evalled=false; |
samer@4 | 16 f=@get; |
samer@4 | 17 |
samer@4 | 18 function v=get, |
samer@4 | 19 if evalled, |
samer@4 | 20 v=value; |
samer@4 | 21 else |
samer@4 | 22 value=thunk(); |
samer@4 | 23 evalled=true; |
samer@4 | 24 v=value; |
samer@4 | 25 end |
samer@4 | 26 end |
samer@4 | 27 end |