Last active
August 29, 2015 14:04
-
-
Save SpencerLynn/634d1dbafdfd0f1acd53 to your computer and use it in GitHub Desktop.
Switch Statement Alternative
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.Linq; | |
public class Swictionary<TKey, TValue> | |
{ | |
readonly Dictionary<Predicate<TKey>, TValue> Cases; | |
TValue Default; | |
public Swictionary(TValue @default) | |
{ | |
Default = @default; | |
this.Cases = new Dictionary<Predicate<TKey>, TValue>(); | |
} | |
public Swictionary(Dictionary<TKey, TValue> cases, TValue @default) | |
: this(@default) | |
{ | |
cases.ToList().ForEach(kvp => AddCase(kvp.Key, kvp.Value)); | |
} | |
public Swictionary(Dictionary<Predicate<TKey>, TValue> cases, TValue @default) | |
: this(@default) | |
{ | |
cases.ToList().ForEach(kvp => AddCase(kvp.Key, kvp.Value)); | |
} | |
public TValue GetValue(TKey @case) | |
{ | |
var caseValue = Cases.FirstOrDefault(kvp => kvp.Key(@case)); | |
if (!caseValue.Equals(default(KeyValuePair<Predicate<TKey>, TValue>))) | |
return caseValue.Value; | |
return this.Default; | |
} | |
public void AddCase(TKey @case, TValue value) | |
{ | |
AddCase(t => t.Equals(@case), value); | |
} | |
public void AddCase(Predicate<TKey> @case, TValue value) | |
{ | |
Cases[@case] = value; | |
} | |
public void UpdateDefault(TValue @default) | |
{ | |
this.Default = @default; | |
} | |
} |
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.Linq; | |
public class SwitchTestDriver | |
{ | |
private Swictionary<int, Action> _simpleSwict; | |
private Swictionary<int, Action> _complexSwict; | |
public void Main() | |
{ | |
Console.WriteLine("**** Simple Cases"); | |
TestSimpleCases(); | |
Console.WriteLine("\n\n**** Complex Cases"); | |
TestComplexCases(); | |
} | |
private void TestSimpleCases() | |
{ | |
_simpleSwict = new Swictionary<int, Action>(GetSimpleCases(), () => Console.WriteLine("Does not equal 3, 30, or 300")); | |
PrintSimpleGetValue(3); | |
PrintSimpleGetValue(30); | |
PrintSimpleGetValue(300); | |
PrintSimpleGetValue(301); | |
PrintSimpleGetValue(500); | |
Console.WriteLine("Inserting 500 as case"); | |
_simpleSwict.AddCase(500, () => Console.WriteLine("Equals 500")); | |
_simpleSwict.UpdateDefault(() => Console.WriteLine("Does not equal 3, 30, 300, or 500")); | |
PrintSimpleGetValue(500); | |
} | |
private void TestComplexCases() | |
{ | |
_complexSwict = new Swictionary<int, Action>(GetComplexCases(), () => Console.WriteLine("Number not in any case range")); | |
PrintComplexGetValue(3); | |
PrintComplexGetValue(30); | |
PrintComplexGetValue(300); | |
PrintComplexGetValue(301); | |
PrintComplexGetValue(500); | |
Console.WriteLine("Inserting >=500 as case"); | |
_complexSwict.AddCase(x => x >= 500, () => Console.WriteLine("Greater than or equal to 500")); | |
PrintComplexGetValue(500); | |
} | |
private void PrintSimpleGetValue(int number) | |
{ | |
Console.Write(number + ": "); | |
_simpleSwict.GetValue(number)(); | |
} | |
private void PrintComplexGetValue(int number) | |
{ | |
Console.Write(number + ": "); | |
_complexSwict.GetValue(number)(); | |
} | |
private Dictionary<int, Action> GetSimpleCases() | |
{ | |
return new Dictionary<int, Action> | |
{ | |
{ 3, () => Console.WriteLine("Equals 3") }, | |
{ 30, () => Console.WriteLine("Equals 30") }, | |
{ 300, () => Console.WriteLine("Equals 300") } | |
}; | |
} | |
private Dictionary<Predicate<int>, Action> GetComplexCases() | |
{ | |
return new Dictionary<Predicate<int>, Action> | |
{ | |
{ x => x <= 3, () => Console.WriteLine("Smaller than or equal to 3") }, | |
{ x => x <= 30, () => Console.WriteLine("Smaller than or equal to 30") }, | |
{ x => x <= 300, () => Console.WriteLine("Smaller than or equal to 300") } | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment