Created
January 21, 2014 17:45
-
-
Save LukaHorvat/8544605 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
using System; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Reflection; | |
using System.Reflection.Emit; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using TriangleNet; | |
using TriangleNet.Geometry; | |
namespace Testing | |
{ | |
class Choice<TOut, TIn> | |
{ | |
public TOut Value; | |
private Func<TIn, bool>[] conditions; | |
public Choice(TOut value, params Func<TIn, bool>[] conditions) | |
{ | |
this.Value = value; | |
this.conditions = conditions; | |
} | |
public bool ConditionMet(TIn target) | |
{ | |
return conditions.Any(f => f(target)); | |
} | |
} | |
class Checker<TOut, TIn> | |
{ | |
public TIn Target; | |
public List<Choice<TOut, TIn>> Choices; | |
public Checker(TIn target) | |
{ | |
Target = target; | |
} | |
public Checker<TOut, TIn> AppendChoice(TOut value, params Func<TIn, bool>[] conditions) | |
{ | |
var choice = new Choice<TOut, TIn>(value, conditions); | |
var checker = new Checker<TOut, TIn>(Target); | |
checker.Choices =Choices == null ? new List<Choice<TOut, TIn>>() : new List<Choice<TOut, TIn>>(Choices); | |
checker.Choices.Add(choice); | |
return checker; | |
} | |
public TOut GetValue() | |
{ | |
return Choices.First(c => c.ConditionMet(Target)).Value; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int a = 10; | |
bool aVeceOd5 = a.Check<bool, int>() | |
.AppendChoice( | |
false, | |
x => x == 1, | |
x => x == 2, | |
x => x == 3, | |
x => x == 4, | |
x => x == 5 | |
).AppendChoice( | |
true, | |
x => x > 5 | |
).GetValue(); | |
Console.WriteLine(aVeceOd5); | |
Console.ReadKey(); | |
} | |
static TOut GetCorrectValue<TOut, TIn>(TIn target, params Choice<TOut, TIn>[] choices) | |
{ | |
return choices.First(choice => choice.ConditionMet(target)).Value; | |
} | |
} | |
static class Extensions | |
{ | |
public static Checker<TOut, TIn> Check<TOut, TIn>(this TIn target) | |
{ | |
return new Checker<TOut, TIn>(target); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment