Created
June 22, 2010 14:18
-
-
Save agross/448516 to your computer and use it in GitHub Desktop.
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
namespace Sample | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Threading; | |
using CsharpMonads; | |
public class ExpensiveOperation | |
{ | |
private readonly Func<Maybe<int>> _operation1; | |
private readonly Func<Maybe<int>> _operation2; | |
public ExpensiveOperation() | |
{ | |
_operation1 = () => | |
{ | |
Thread.Sleep(2000); | |
return Maybe.From(42); | |
}; | |
_operation2 = () => | |
{ | |
Thread.Sleep(5000); | |
return Maybe<int>.None; | |
}; | |
} | |
public Maybe<int> Compute() | |
{ | |
var stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
var result = _operation1().Concat(_operation2()); | |
stopwatch.Stop(); | |
Console.WriteLine("Time taken: {0}", stopwatch.Elapsed); | |
return result; | |
} | |
} | |
internal static partial class EnumerableExtensions | |
{ | |
public static Maybe<T> Concat<T>(this Maybe<T> source1, Maybe<T> source2) | |
{ | |
return source1.IsNone | |
? source2 | |
: source1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment