延迟赋值主要有两点:
1.一个参数可能或可能没被赋值.
2.一个参数在一个函数中每次使用时可能被赋值.
int Add(int x, int y) { return (2 + 1) + (1); }
使用Func<T>,我们轻松实现,看代码:
/// <summary> /// LazyExpression /// </summary> /// <typeparam name="T">T</typeparam> public class LazyExpression<T> { Func<T> thunk; public LazyExpression(Func<T> Thunk) { thunk = Thunk; } public T Evaluate() { return thunk(); } } /// <summary> /// LazyBoolExpression /// </summary> public static class LazyBoolExpression { public static bool And(LazyExpression<bool> LHS, LazyExpression<bool> RHS) { return LHS.Evaluate() && RHS.Evaluate(); } public static bool Or(LazyExpression<bool> LHS, LazyExpression<bool> RHS) { return LHS.Evaluate() == true ? true : RHS.Evaluate(); } } /// <summary> /// LazyMemoizedExpression /// </summary> /// <typeparam name="T"></typeparam> public class LazyMemoizedExpression<T> { bool thunked; T value; Func<T> thunk; public LazyMemoizedExpression(Func<T> Thunk) { thunked = false; thunk = Thunk; } public T Evaluate() { if (!thunked) { value = thunk(); thunked = true; } return value; } }
LazyExpression<T>类实现了基本的延迟赋值,LazyMemoizedExpression<T>类实现了一次赋值,多次使用.
LazyBoolExpression实现逻辑表达式.
看UnitTest,一切就明白了.
/// <summary> /// Lazies the expression test. /// </summary> [TestCase] public void LazyExpressionTest() { var lme1 = new LazyExpression<int>(() => 2 + 1); var lme2 = new LazyExpression<int>(() => 1); Assert.AreEqual(4, Add(lme1, lme2)); } /// <summary> /// Adds the specified x. /// </summary> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <returns>result</returns> private int Add(LazyExpression<int> x, LazyExpression<int> y) { return x.Evaluate() + y.Evaluate(); } /// <summary> /// Lazies the expression with logic. /// </summary> [TestCase] public void LazyExpressionWithLogic() { var exp1 = new LazyExpression<bool>(() => true); var exp2 = new LazyExpression<bool>(() => true || false); if (LazyBoolExpression.And(exp1, exp2)) { Console.WriteLine("lazy and"); } if (LazyBoolExpression.Or(exp1, exp2)) { Console.WriteLine("lazy or"); } } /// <summary> /// Lazies the memoized expression test. /// </summary> /// <remarks>Author Petter Liu http://wintersun.cnblogs.com </remarks> [TestCase] public void LazyMemoizedExpressionTest() { var lme1 = new LazyMemoizedExpression<int>(() => GetIntResult()); Assert.AreEqual(943, lme1.Evaluate()); Assert.AreEqual(943, lme1.Evaluate()); //output: //1 passed, 0 failed, 0 skipped, took 2.80 seconds (NUnit 2.5). } /// <summary> /// Compares to lazy expression test. /// </summary>
/// <remarks>Author Petter Liu http://wintersun.cnblogs.com </remarks> [TestCase] public void CompareToLazyExpressionTest() { var lme1 = new LazyExpression<int>(() => GetIntResult()); Assert.AreEqual(943, lme1.Evaluate()); Assert.AreEqual(943, lme1.Evaluate()); //output: //1 passed, 0 failed, 0 skipped, took 4.80 seconds (NUnit 2.5). } /// <summary> /// Gets the int result. /// </summary> /// <returns></returns> private int GetIntResult() { //current thread sleep two second. System.Threading.Thread.Sleep(2000); return 943; }
Author:Petter Liu http://wintersun.cnblogs.com