Last active
May 17, 2018 22:10
-
-
Save ecounysis/b752c82fed4bd7be2e8f5e2aae2669a9 to your computer and use it in GitHub Desktop.
easy function memoization in C#
This file contains hidden or 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
// Copyright (c) 2016 Eric Christensen | |
/* | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this | |
software and associated documentation files (the "Software"), to deal in the Software | |
without restriction, including without limitation the rights to use, copy, modify, merge, | |
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons | |
to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies | |
or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | |
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE | |
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
DEALINGS IN THE SOFTWARE. | |
*/ | |
using System; | |
using System.Threading; | |
namespace FunctionalHelper | |
{ | |
public class MemoizationTesting | |
{ | |
Func<int, int> add10; | |
static int executionSeconds; | |
static int memorySeconds; | |
public static void Main(string [] args) | |
{ | |
executionSeconds = 1; | |
memorySeconds = 5; | |
logWithTime("Memoization tests"); | |
logWithTime(string.Format("Memoized functions will take {0} seconds to compute", executionSeconds)); | |
logWithTime(string.Format("Memoized functions have memory of {0} seconds", memorySeconds)); | |
var tester = new MemoizationTesting(); | |
tester.add10 = Memoizer.Create<int, int>(x => | |
{ | |
logWithTime("Nothing in memory: computing add10(" + x.ToString() + ")"); | |
Thread.Sleep(executionSeconds*1000); | |
return x + 10; | |
}, memorySeconds); // can remove int parameter for unlimited memory | |
logWithTime("Executing add10(10)"); | |
logWithTime("Result is " + tester.add10(10).ToString()); | |
logWithTime("Executing add10(11)"); | |
logWithTime("Result is " + tester.add10(11).ToString()); | |
logWithTime("Executing add10(10)"); | |
logWithTime("Result is " + tester.add10(10).ToString()); | |
logWithTime("Executing add10(11)"); | |
logWithTime("Result is " + tester.add10(11).ToString()); | |
logWithTime("Executing add10(10)"); | |
logWithTime("Result is " + tester.add10(10).ToString()); | |
logWithTime("Executing add10(11)"); | |
logWithTime("Result is " + tester.add10(11).ToString()); | |
logWithTime("Executing add10(10)"); | |
logWithTime("Result is " + tester.add10(10).ToString()); | |
logWithTime("Executing add10(11)"); | |
logWithTime("Result is " + tester.add10(11).ToString()); | |
logWithTime("Taking a nap for 10 seconds!"); | |
Thread.Sleep(10*1000); | |
logWithTime("Awake!"); | |
logWithTime("Executing add10(10)"); | |
logWithTime("Result is " + tester.add10(10).ToString()); | |
logWithTime("Executing add10(11)"); | |
logWithTime("Result is " + tester.add10(11).ToString()); | |
logWithTime("Executing add10(10)"); | |
logWithTime("Result is " + tester.add10(10).ToString()); | |
logWithTime("Executing add10(11)"); | |
logWithTime("Result is " + tester.add10(11).ToString()); | |
logWithTime("Executing add10(10)"); | |
logWithTime("Result is " + tester.add10(10).ToString()); | |
logWithTime("Executing add10(11)"); | |
logWithTime("Result is " + tester.add10(11).ToString()); | |
logWithTime("Executing add10(10)"); | |
logWithTime("Result is " + tester.add10(10).ToString()); | |
logWithTime("Executing add10(11)"); | |
logWithTime("Result is " + tester.add10(11).ToString()); | |
} | |
static void logWithTime(string val) | |
{ | |
Console.WriteLine("[{0}] {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), val); | |
} | |
} | |
} |
This file contains hidden or 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
// Copyright (c) 2016 Eric Christensen | |
/* | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this | |
software and associated documentation files (the "Software"), to deal in the Software | |
without restriction, including without limitation the rights to use, copy, modify, merge, | |
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons | |
to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies | |
or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | |
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE | |
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
DEALINGS IN THE SOFTWARE. | |
*/ | |
using System; | |
using System.Collections.Generic; | |
namespace FunctionalHelper | |
{ | |
public static class Memoizer | |
{ | |
// create an inner class to store the results | |
class Result<T1, T2> | |
{ | |
public T1 input; | |
public T2 output; | |
DateTime expireDate; | |
public bool Expired() | |
{ | |
return (expireDate.CompareTo(DateTime.Now) < 0); | |
} | |
public Result(T1 input, T2 output, int validSeconds) | |
{ | |
this.input = input; | |
this.output = output; | |
expireDate = DateTime.Now.AddSeconds(validSeconds); | |
} | |
} | |
// create an inner class to store the results | |
class Result<T> | |
{ | |
public T output; | |
DateTime expireDate; | |
public bool Expired() | |
{ | |
return (expireDate.CompareTo(DateTime.Now) < 0); | |
} | |
public Result() | |
{ | |
expireDate = DateTime.Now.AddSeconds(-99); | |
} | |
public Result(T output, int validSeconds) | |
{ | |
this.output = output; | |
expireDate = DateTime.Now.AddSeconds(validSeconds); | |
} | |
} | |
// input and output, finite memory | |
public static Func<T1, T2> Create<T1, T2>(Func<T1, T2> fn, int memorySeconds) | |
{ | |
Dictionary<T1, Result<T1, T2>> memory = new Dictionary<T1, Result<T1, T2>>(); | |
return x => | |
{ | |
Result<T1, T2> foundInMemory; | |
if (memory.TryGetValue(x, out foundInMemory)) | |
{ | |
if (!foundInMemory.Expired()) | |
{ | |
return foundInMemory.output; | |
} | |
} | |
T2 val = fn(x); | |
memory[x] = new Result<T1, T2>(x, val, memorySeconds); | |
return val; | |
}; | |
} | |
// input and output, infinite memory | |
public static Func<T1, T2> Create<T1, T2>(Func<T1, T2> fn) | |
{ | |
Dictionary<T1, T2> memory = new Dictionary<T1, T2>(); | |
return x => | |
{ | |
T2 val; | |
if (memory.TryGetValue(x, out val)) | |
{ | |
return val; | |
} | |
val = fn(x); | |
memory[x] = val; | |
return val; | |
}; | |
} | |
// output only, finite memory | |
public static Func<T1> Create<T1>(Func<T1> fn, int memorySeconds) | |
{ | |
Result<T1> memory = new Result<T1>(); | |
return () => | |
{ | |
T1 val; | |
if (!memory.Expired()) | |
{ | |
return memory.output; | |
} | |
val = fn(); | |
memory = new Result<T1>(val, memorySeconds); | |
return val; | |
}; | |
} | |
// output only, infinite memory | |
public static Func<T1> Create<T1>(Func<T1> fn) | |
{ | |
Result<T1> memory = new Result<T1>(); | |
return () => | |
{ | |
T1 val; | |
if (!memory.Expired()) | |
{ | |
return memory.output; | |
} | |
val = fn(); | |
memory = new Result<T1>(val, 999999999); // 31 years | |
return val; | |
}; | |
} | |
} | |
} |
This file contains hidden or 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
[2016-06-16 22:17:26.706] Memoization tests | |
[2016-06-16 22:17:26.715] Memoized functions will take 1 seconds to compute | |
[2016-06-16 22:17:26.715] Memoized functions have memory of 5 seconds | |
[2016-06-16 22:17:26.716] Executing add10(10) | |
[2016-06-16 22:17:26.717] Nothing in memory: computing add10(10) | |
[2016-06-16 22:17:27.718] Result is 20 | |
[2016-06-16 22:17:27.718] Executing add10(11) | |
[2016-06-16 22:17:27.719] Nothing in memory: computing add10(11) | |
[2016-06-16 22:17:28.719] Result is 21 | |
[2016-06-16 22:17:28.719] Executing add10(10) | |
[2016-06-16 22:17:28.720] Result is 20 | |
[2016-06-16 22:17:28.720] Executing add10(11) | |
[2016-06-16 22:17:28.720] Result is 21 | |
[2016-06-16 22:17:28.721] Executing add10(10) | |
[2016-06-16 22:17:28.721] Result is 20 | |
[2016-06-16 22:17:28.721] Executing add10(11) | |
[2016-06-16 22:17:28.721] Result is 21 | |
[2016-06-16 22:17:28.722] Executing add10(10) | |
[2016-06-16 22:17:28.722] Result is 20 | |
[2016-06-16 22:17:28.722] Executing add10(11) | |
[2016-06-16 22:17:28.722] Result is 21 | |
[2016-06-16 22:17:28.723] Taking a nap for 10 seconds! | |
[2016-06-16 22:17:38.735] Awake! | |
[2016-06-16 22:17:38.735] Executing add10(10) | |
[2016-06-16 22:17:38.735] Nothing in memory: computing add10(10) | |
[2016-06-16 22:17:39.735] Result is 20 | |
[2016-06-16 22:17:39.735] Executing add10(11) | |
[2016-06-16 22:17:39.735] Nothing in memory: computing add10(11) | |
[2016-06-16 22:17:40.735] Result is 21 | |
[2016-06-16 22:17:40.735] Executing add10(10) | |
[2016-06-16 22:17:40.736] Result is 20 | |
[2016-06-16 22:17:40.736] Executing add10(11) | |
[2016-06-16 22:17:40.737] Result is 21 | |
[2016-06-16 22:17:40.737] Executing add10(10) | |
[2016-06-16 22:17:40.737] Result is 20 | |
[2016-06-16 22:17:40.738] Executing add10(11) | |
[2016-06-16 22:17:40.738] Result is 21 | |
[2016-06-16 22:17:40.738] Executing add10(10) | |
[2016-06-16 22:17:40.738] Result is 20 | |
[2016-06-16 22:17:40.738] Executing add10(11) | |
[2016-06-16 22:17:40.738] Result is 21 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment