A Simple Memoization Function Class.
Memoization is simply remembering the previous worked out answers for the value inputted for the function.
Now let define a few functions.
Fibonacci
Factorial
Both are rather close to how you would write the mathematically.
Memoization is simply remembering the previous worked out answers for the value inputted for the function.
Public Class Memo(Of T0 , T1 ) Private TheFunction As Func(Of Memo(of T0,T1),T0,T1) Private MemoizationValues As New Dictionary(Of T0,T1) Public Sub New ( TheFunction As Func(of Memo(Of T0 , T1), T0, T1 ) ) Me.TheFunction = TheFunction End Sub Public Default Property Fun(x As T0) As T1 Set(Value As T1) If Not MemoizationValues.ContainsKey(x) Then MemoizationValues(x) = Value End Set Get If MemoizationValues.ContainsKey(x) Then Return MemoizationValues(x) Dim Value = TheFunction(Me,x) MemoizationValues(x)=Value Return Value End Get End Property End Class
Now let define a few functions.
Fibonacci
Dim fib As New Memo(Of BigInteger, BigInteger)(Function(f,x) f(x-2) + f(x-1)) fib(0) = 0 fib(1) = 1 Console.WriteLine("Fib(10) = ", Fib(10))
Factorial
Dib Fact As New Memo(Of BigInteger, BigInteger(Function(f,x) x * f(x-1)) f(0) = 1 Console.WriteLine("Fact(10) = ", Fact(10) )
Both are rather close to how you would write the mathematically.
1 Comments On This Entry
Page 1 of 1
Page 1 of 1
Search My Blog
Recent Entries
Recent Comments
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)
← April 2018 →
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- .net
- .net4
- bf
- brainfuck
- Codeplex
- Coding
- custom Control
- custom controls
- DIC CodeAID VS Gallery
- Dice
- Die
- DLL
- Englishify
- Extension
- Extension Method
- ExtMethods
- F#
- Functional
- Functional Programming
- Graph
- Graphs
- Language Intergrated Query.
- Library
- LINQ
- LINQ Codes
- LISP interpreter
- Macro
- My Games
- Nemerle.
- net
- podcast
- Project
- Project Cider
- RadixSort Generics (Of T)
- restricted textbox
- Rolling
- rss
- rss feed
- Scribblings
- shadowtext
- Tips
- Transparent Textbox
- vb
- vb.net
- VB.net +LINQ Extension Method
- vb.net 1-Liners
- vb.net visual basic vs2010 .net4
- vs2010
- Weird
- XM
- xml
- XML Literals