[nem-en] New memoization macro.
d
d at hell.art.pl
Wed Apr 19 17:59:36 CEST 2006
Hi
I just committed my new memoization code (http://nemerle.org/svn/
nemerle/trunk/macros/Memoize.n), if you have any comments/
suggestions, let me know.
Now a few words about the new Scope option - there are two
possibilities here - Scope = Instance (default) and Scope = Class.
Example:
class A {
[Nemerle.Memoize]
public fib (n : int) : int {
System.Console.WriteLine ($"CALL TO foo $n");
match (n) {
| 0 | 1 => 1
| _ => fib (n - 1) + fib (n - 2)
}
}
}
def a = A ();
System.Console.WriteLine ($"$(a.fib (10))");
System.Console.WriteLine ($"$(a.fib (9))");
System.Console.WriteLine ($"$(a.fib (8))");
def b = A ();
System.Console.WriteLine ($"$(b.fib (10))");
System.Console.WriteLine ($"$(b.fib (9))");
System.Console.WriteLine ($"$(b.fib (8))");
//===>
CALL TO foo 10
CALL TO foo 9
CALL TO foo 8
CALL TO foo 7
CALL TO foo 6
CALL TO foo 5
CALL TO foo 4
CALL TO foo 3
CALL TO foo 2
CALL TO foo 1
CALL TO foo 0
89
55
34
CALL TO foo 10
CALL TO foo 9
CALL TO foo 8
CALL TO foo 7
CALL TO foo 6
CALL TO foo 5
CALL TO foo 4
CALL TO foo 3
CALL TO foo 2
CALL TO foo 1
CALL TO foo 0
89
55
34
///
class A {
[Nemerle.Memoize (Scope = Class)]
public fib (n : int) : int {
System.Console.WriteLine ($"CALL TO foo $n");
match (n) {
| 0 | 1 => 1
| _ => fib (n - 1) + fib (n - 2)
}
}
}
def a = A ();
System.Console.WriteLine ($"$(a.fib (10))");
System.Console.WriteLine ($"$(a.fib (9))");
System.Console.WriteLine ($"$(a.fib (8))");
def b = A ();
System.Console.WriteLine ($"$(b.fib (10))");
System.Console.WriteLine ($"$(b.fib (9))");
System.Console.WriteLine ($"$(b.fib (8))");
//===>
CALL TO foo 10
CALL TO foo 9
CALL TO foo 8
CALL TO foo 7
CALL TO foo 6
CALL TO foo 5
CALL TO foo 4
CALL TO foo 3
CALL TO foo 2
CALL TO foo 1
CALL TO foo 0
89
55
34
89
55
34
///
So in case of Scope = Instance the results are memoized in the
scope of a class instance, in case of Scope = Class, instances share
their memoized results (AFAIK this is pretty close to aggressive
sharing, am I correct ?).
thanks, later,
d
--
https://hell.art.pl == terror.org.pl == terror.icm.edu.pl/~d/gpg
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
Url : /mailman/pipermail/devel-en/attachments/20060419/72ae5af8/PGP.bin
More information about the devel-en
mailing list