Skip to content

Commit

Permalink
Switched the 2+ value pattern matchers over to using the new ValueTup…
Browse files Browse the repository at this point in the history
…le types.

Added support for (T1, T2) pattern matching.

Issue #16.
  • Loading branch information
DavidArno committed Jan 6, 2017
1 parent c5034b7 commit a40f1d5
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 63 deletions.
3 changes: 1 addition & 2 deletions SuccincT/PatternMatchers/Any.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public struct Any
public static bool operator !=(Any any1, Any any2) => false;

[SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
// ReSharper disable once ConvertToAutoProperty
public static Any _ => AnAny;
public static Any _ { get; } = AnAny;
}
}
5 changes: 2 additions & 3 deletions SuccincT/PatternMatchers/EitherTuple{T1,T2,T3}.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using SuccincT.Functional;

namespace SuccincT.PatternMatchers
Expand Down Expand Up @@ -66,7 +65,7 @@ public EitherTuple(Any value1, Any value2, Any value3)
_value3 = new Either<T3, Any>(value3);
}

public bool MatchesTuple(Tuple<T1, T2, T3> tuple) =>
public bool MatchesTuple((T1, T2, T3) tuple) =>
(!_value1.IsLeft || EqualityComparer<T1>.Default.Equals(_value1.Left, tuple.Item1)) &&
(!_value2.IsLeft || EqualityComparer<T2>.Default.Equals(_value2.Left, tuple.Item2)) &&
(!_value3.IsLeft || EqualityComparer<T3>.Default.Equals(_value3.Left, tuple.Item3));
Expand Down
2 changes: 1 addition & 1 deletion SuccincT/PatternMatchers/EitherTuple{T1,T2}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public EitherTuple(Any value1, Any value2)
_value2 = new Either<T2, Any>(value2);
}

public bool MatchesTuple(Tuple<T1, T2> tuple) =>
public bool MatchesTuple((T1, T2) tuple) =>
(!_value1.IsLeft || EqualityComparer<T1>.Default.Equals(_value1.Left, tuple.Item1)) &&
(!_value2.IsLeft || EqualityComparer<T2>.Default.Equals(_value2.Left, tuple.Item2));
}
Expand Down
40 changes: 20 additions & 20 deletions SuccincT/PatternMatchers/Matcher{T1,T2,T3,T4,TR}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ internal sealed class Matcher<T1, T2, T3, T4, TResult> :
IFuncWhereHandler<IFuncMatcher<T1, T2, T3, T4, TResult>, T1, T2, T3, T4, TResult>,
IFuncMatcherAfterElse<TResult>
{
private readonly MatchFunctionSelector<Tuple<T1, T2, T3, T4>, Tuple<T1, T2, T3, T4>, TResult> _functionSelector;
private readonly Tuple<T1, T2, T3, T4> _item;
private IList<Tuple<T1, T2, T3, T4>> _withValues;
private Func<Tuple<T1, T2, T3, T4>, bool> _whereExpression;
private Func<Tuple<T1, T2, T3, T4>, TResult> _elseFunction;
private readonly MatchFunctionSelector<(T1, T2, T3, T4), (T1, T2, T3, T4), TResult> _functionSelector;
private readonly (T1, T2, T3, T4) _item;
private IList<(T1, T2, T3, T4)> _withValues;
private Func<(T1, T2, T3, T4), bool> _whereExpression;
private Func<(T1, T2, T3, T4), TResult> _elseFunction;

internal Matcher(Tuple<T1, T2, T3, T4> item)
internal Matcher((T1, T2, T3, T4) item)
{
_item = item;
_functionSelector = new MatchFunctionSelector<Tuple<T1, T2, T3, T4>, Tuple<T1, T2, T3, T4>, TResult>(x =>
_functionSelector = new MatchFunctionSelector<(T1, T2, T3, T4), (T1, T2, T3, T4), TResult>(x =>
{
throw new NoMatchException(
$"No match action exists for value of ({_item.Item1}, {_item.Item2}, {_item.Item3}, {_item.Item4}");
Expand All @@ -37,21 +37,21 @@ internal Matcher(Tuple<T1, T2, T3, T4> item)
IActionWithHandler<IActionMatcher<T1, T2, T3, T4>, T1, T2, T3, T4> IMatcher<T1, T2, T3, T4>.With(
T1 value1, T2 value2, T3 value3, T4 value4)
{
_withValues = new List<Tuple<T1, T2, T3, T4>> {Tuple.Create(value1, value2, value3, value4)};
_withValues = new List<(T1, T2, T3, T4)> {(value1, value2, value3, value4)};
return this;
}

IActionWithHandler<IActionMatcher<T1, T2, T3, T4>, T1, T2, T3, T4> IActionMatcher<T1, T2, T3, T4>.With(
T1 value1, T2 value2, T3 value3, T4 value4)
{
_withValues = new List<Tuple<T1, T2, T3, T4>> {Tuple.Create(value1, value2, value3, value4)};
_withValues = new List<(T1, T2, T3, T4)> {(value1, value2, value3, value4)};
return this;
}

IFuncWithHandler<IFuncMatcher<T1, T2, T3, T4, TResult>, T1, T2, T3, T4, TResult>
IFuncMatcher<T1, T2, T3, T4, TResult>.With(T1 value1, T2 value2, T3 value3, T4 value4)
{
_withValues = new List<Tuple<T1, T2, T3, T4>> {Tuple.Create(value1, value2, value3, value4) };
_withValues = new List<(T1, T2, T3, T4)> {(value1, value2, value3, value4)};
return this;
}

Expand Down Expand Up @@ -104,14 +104,14 @@ internal Matcher(Tuple<T1, T2, T3, T4> item)
IFuncWithHandler<IFuncMatcher<T1, T2, T3, T4, TResult>, T1, T2, T3, T4, TResult>.Or(
T1 value1, T2 value2, T3 value3, T4 value4)
{
_withValues.Add(Tuple.Create(value1, value2, value3, value4));
_withValues.Add((value1, value2, value3, value4));
return this;
}

IActionMatcher<T1, T2, T3, T4> IActionWithHandler<IActionMatcher<T1, T2, T3, T4>, T1, T2, T3, T4>.Do(
Action<T1, T2, T3, T4> action)
{
RecordFunction((x, y) => y.Any(v => EqualityComparer<Tuple<T1, T2, T3, T4>>.Default.Equals(x, v)),
RecordFunction((x, y) => y.Any(v => EqualityComparer<(T1, T2, T3, T4)>.Default.Equals(x, v)),
_withValues,
ActionToFunc(action));
return this;
Expand All @@ -127,7 +127,7 @@ internal Matcher(Tuple<T1, T2, T3, T4> item)
IFuncMatcher<T1, T2, T3, T4, TResult> IFuncWithHandler<IFuncMatcher<T1, T2, T3, T4, TResult>, T1, T2, T3, T4, TResult>.Do(
Func<T1, T2, T3, T4, TResult> function)
{
RecordFunction((x, y) => y.Any(v => EqualityComparer<Tuple<T1, T2, T3, T4>>.Default.Equals(x, v)),
RecordFunction((x, y) => y.Any(v => EqualityComparer<(T1, T2, T3, T4)>.Default.Equals(x, v)),
_withValues,
x => function(x.Item1, x.Item2, x.Item3, x.Item4));
return this;
Expand All @@ -136,7 +136,7 @@ internal Matcher(Tuple<T1, T2, T3, T4> item)
IFuncMatcher<T1, T2, T3, T4, TResult> IFuncWithHandler<IFuncMatcher<T1, T2, T3, T4, TResult>, T1, T2, T3, T4, TResult>.Do(
TResult value)
{
RecordFunction((x, y) => y.Any(v => EqualityComparer<Tuple<T1, T2, T3, T4>>.Default.Equals(x, v)),
RecordFunction((x, y) => y.Any(v => EqualityComparer<(T1, T2, T3, T4)>.Default.Equals(x, v)),
_withValues,
x => value);
return this;
Expand Down Expand Up @@ -173,15 +173,15 @@ TResult IFuncMatcherAfterElse<TResult>.Result()
return possibleResult.HasValue ? possibleResult.Value : _elseFunction(_item);
}

private void RecordFunction(Func<Tuple<T1, T2, T3, T4>, IList<Tuple<T1, T2, T3, T4>>, bool> test,
IList<Tuple<T1, T2, T3, T4>> values,
Func<Tuple<T1, T2, T3, T4>, TResult> function) =>
private void RecordFunction(Func<(T1, T2, T3, T4), IList<(T1, T2, T3, T4)>, bool> test,
IList<(T1, T2, T3, T4)> values,
Func<(T1, T2, T3, T4), TResult> function) =>
_functionSelector.AddTestAndAction(test, values, null, function);

private void RecordFunction(Func<Tuple<T1, T2, T3, T4>, bool> test, Func<Tuple<T1, T2, T3, T4>, TResult> function) =>
private void RecordFunction(Func<(T1, T2, T3, T4), bool> test, Func<(T1, T2, T3, T4), TResult> function) =>
_functionSelector.AddTestAndAction(null, null, test, function);

private static Func<Tuple<T1, T2, T3, T4>, TResult> ActionToFunc(Action<T1, T2, T3, T4> action) =>
private static Func<(T1, T2, T3, T4), TResult> ActionToFunc(Action<T1, T2, T3, T4> action) =>
x =>
{
action(x.Item1, x.Item2, x.Item3, x.Item4);
Expand All @@ -192,7 +192,7 @@ TResult IFuncMatcherAfterElse<TResult>.Result()
IActionWithHandler<IActionMatcher<T1, T2, T3, T4>, T1, T2, T3, T4>.Or(
T1 value1, T2 value2, T3 value3, T4 value4)
{
_withValues.Add(Tuple.Create(value1, value2, value3, value4));
_withValues.Add((value1, value2, value3, value4));
return this;
}
}
Expand Down
20 changes: 10 additions & 10 deletions SuccincT/PatternMatchers/Matcher{T1,T2,T3,TR}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ internal sealed class Matcher<T1, T2, T3, TResult> :
IFuncWhereHandler<IFuncMatcher<T1, T2, T3, TResult>, T1, T2, T3, TResult>,
IFuncMatcherAfterElse<TResult>
{
private readonly MatchFunctionSelector<Tuple<T1, T2, T3>, EitherTuple<T1, T2, T3>, TResult> _functionSelector;
private readonly Tuple<T1, T2, T3> _item;
private readonly MatchFunctionSelector<(T1, T2, T3), EitherTuple<T1, T2, T3>, TResult> _functionSelector;
private readonly (T1, T2, T3) _item;
private IList<EitherTuple<T1, T2, T3>> _withValues;
private Func<Tuple<T1, T2, T3>, bool> _whereExpression;
private Func<Tuple<T1, T2, T3>, TResult> _elseFunction;
private Func<(T1, T2, T3), bool> _whereExpression;
private Func<(T1, T2, T3), TResult> _elseFunction;

internal Matcher(Tuple<T1, T2, T3> item)
internal Matcher((T1, T2, T3) item)
{
_item = item;
_functionSelector = new MatchFunctionSelector<Tuple<T1, T2, T3>, EitherTuple<T1, T2, T3>, TResult>(x =>
_functionSelector = new MatchFunctionSelector<(T1, T2, T3), EitherTuple<T1, T2, T3>, TResult>(x =>
{
throw new NoMatchException(
$"No match action exists for value of ({_item.Item1}, {_item.Item2}, {_item.Item3}");
Expand Down Expand Up @@ -288,15 +288,15 @@ TResult IFuncMatcherAfterElse<TResult>.Result()
return possibleResult.HasValue ? possibleResult.Value : _elseFunction(_item);
}

private void RecordFunction(Func<Tuple<T1, T2, T3>, IList<EitherTuple<T1, T2, T3>>, bool> test,
private void RecordFunction(Func<(T1, T2, T3), IList<EitherTuple<T1, T2, T3>>, bool> test,
IList<EitherTuple<T1, T2, T3>> values,
Func<Tuple<T1, T2, T3>, TResult> function) =>
Func<(T1, T2, T3), TResult> function) =>
_functionSelector.AddTestAndAction(test, values, null, function);

private void RecordFunction(Func<Tuple<T1, T2, T3>, bool> test, Func<Tuple<T1, T2, T3>, TResult> function) =>
private void RecordFunction(Func<(T1, T2, T3), bool> test, Func<(T1, T2, T3), TResult> function) =>
_functionSelector.AddTestAndAction(null, null, test, function);

private static Func<Tuple<T1, T2, T3>, TResult> ActionToFunc(Action<T1, T2, T3> action) =>
private static Func<(T1, T2, T3), TResult> ActionToFunc(Action<T1, T2, T3> action) =>
x =>
{
action(x.Item1, x.Item2, x.Item3);
Expand Down
20 changes: 10 additions & 10 deletions SuccincT/PatternMatchers/Matcher{T1,T2,TR}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ internal sealed class Matcher<T1, T2, TResult> :
IFuncWhereHandler<IFuncMatcher<T1, T2, TResult>, T1, T2, TResult>,
IFuncMatcherAfterElse<TResult>
{
private readonly MatchFunctionSelector<Tuple<T1, T2>, EitherTuple<T1, T2>, TResult> _functionSelector;
private readonly Tuple<T1, T2> _item;
private readonly MatchFunctionSelector<(T1, T2), EitherTuple<T1, T2>, TResult> _functionSelector;
private readonly (T1, T2) _item;
private IList<EitherTuple<T1, T2>> _withValues;
private Func<Tuple<T1, T2>, bool> _whereExpression;
private Func<Tuple<T1, T2>, TResult> _elseFunction;
private Func<(T1, T2), bool> _whereExpression;
private Func<(T1, T2), TResult> _elseFunction;

internal Matcher(Tuple<T1, T2> item)
internal Matcher((T1, T2) item)
{
_item = item;
_functionSelector = new MatchFunctionSelector<Tuple<T1, T2>, EitherTuple<T1, T2>, TResult>(x =>
_functionSelector = new MatchFunctionSelector<(T1, T2), EitherTuple<T1, T2>, TResult>(x =>
{
throw new NoMatchException($"No match action exists for value of ({_item.Item1}, {_item.Item2}");
});
Expand Down Expand Up @@ -275,15 +275,15 @@ TResult IFuncMatcherAfterElse<TResult>.Result()
return possibleResult.HasValue ? possibleResult.Value : _elseFunction(_item);
}

private void RecordFunction(Func<Tuple<T1, T2>, IList<EitherTuple<T1, T2>>, bool> test,
private void RecordFunction(Func<(T1, T2), IList<EitherTuple<T1, T2>>, bool> test,
IList<EitherTuple<T1, T2>> values,
Func<Tuple<T1, T2>, TResult> function) =>
Func<(T1, T2), TResult> function) =>
_functionSelector.AddTestAndAction(test, values, null, function);

private void RecordFunction(Func<Tuple<T1, T2>, bool> test, Func<Tuple<T1, T2>, TResult> function) =>
private void RecordFunction(Func<(T1, T2), bool> test, Func<(T1, T2), TResult> function) =>
_functionSelector.AddTestAndAction(null, null, test, function);

private static Func<Tuple<T1, T2>, TResult> ActionToFunc(Action<T1, T2> action) =>
private static Func<(T1, T2), TResult> ActionToFunc(Action<T1, T2> action) =>
x =>
{
action(x.Item1, x.Item2);
Expand Down
21 changes: 12 additions & 9 deletions SuccincT/PatternMatchers/SpecificTypeMatcherExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,34 @@ public static class SpecificTypeMatcherExtensions
{
public static IMatcher<T> Match<T>(this Tuple<T> item) => new Matcher<T, Unit>(item.Item1);

public static IMatcher<T1, T2> Match<T1, T2>(this Tuple<T1, T2> item) =>
new Matcher<T1, T2, Unit>(item);
public static IMatcher<T1, T2> Match<T1, T2>(this (T1, T2) tuple) =>
new Matcher<T1, T2, Unit>(tuple);

public static IMatcher<T1, T2> Match<T1, T2>(this Tuple<T1, T2> tuple) =>
new Matcher<T1, T2, Unit>((tuple.Item1, tuple.Item2));

public static IMatcher<T1, T2> Match<T1, T2>(this ITupleMatchable<T1, T2> item)
{
var tuple = item.PropertiesToMatch;
return new Matcher<T1, T2, Unit>(tuple);
return new Matcher<T1, T2, Unit>((tuple.Item1, tuple.Item2));
}

public static IMatcher<T1, T2, T3> Match<T1, T2, T3>(this ITupleMatchable<T1, T2, T3> item)
{
var tuple = item.PropertiesToMatch;
return new Matcher<T1, T2, T3, Unit>(tuple);
return new Matcher<T1, T2, T3, Unit>((tuple.Item1, tuple.Item2, tuple.Item3));
}

public static IMatcher<T1, T2, T3, T4> Match<T1, T2, T3, T4>(this ITupleMatchable<T1, T2, T3, T4> item)
{
var tuple = item.PropertiesToMatch;
return new Matcher<T1, T2, T3, T4, Unit>(tuple);
return new Matcher<T1, T2, T3, T4, Unit>((tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4));
}

public static IMatcher<T1, T2, T3> Match<T1, T2, T3>(this Tuple<T1, T2, T3> item) =>
new Matcher<T1, T2, T3, Unit>(item);
public static IMatcher<T1, T2, T3> Match<T1, T2, T3>(this Tuple<T1, T2, T3> tuple) =>
new Matcher<T1, T2, T3, Unit>((tuple.Item1, tuple.Item2, tuple.Item3));

public static IMatcher<T1, T2, T3, T4> Match<T1, T2, T3, T4>(this Tuple<T1, T2, T3, T4> item) =>
new Matcher<T1, T2, T3, T4, Unit>(item);
public static IMatcher<T1, T2, T3, T4> Match<T1, T2, T3, T4>(this Tuple<T1, T2, T3, T4> tuple) =>
new Matcher<T1, T2, T3, T4, Unit>((tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4));
}
}
17 changes: 13 additions & 4 deletions SuccincTTests/SuccincT/Tuples/TupleExecTestsT1T2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ public void Tuple_CanBeMatchedWithExec()
}

[Test]
public void TupleWithAnyInt_CanBeMatchedWithExec()
public void ValueTuple_CanBeMatchedWithExec()
{
var tuple = Tuple.Create(1, "a");
var tuple = (1, "a");
var result = false;
tuple.Match().With(1, "a").Do((x, y) => result = true).Exec();
Assert.IsTrue(result);
}

[Test]
public void ValueTupleWithAnyInt_CanBeMatchedWithExec()
{
var tuple = (1, "a");
var result = false;
tuple.Match().With(_, "a").Do((x, y) => result = true).Exec();
Assert.IsTrue(result);
Expand All @@ -36,9 +45,9 @@ public void TupleWithAnyString_CanBeMatchedWithExec()
}

[Test]
public void TupleWithAnyAndAny_CanBeMatchedWithExec()
public void ValueTupleWithAnyAndAny_CanBeMatchedWithExec()
{
var tuple = Tuple.Create(1, "a");
var tuple = (1, "a");
var result = false;
tuple.Match().With(_, _).Do((x, y) => result = true).Exec();
Assert.IsTrue(result);
Expand Down
8 changes: 4 additions & 4 deletions SuccincTTests/SuccincT/Tuples/TupleTestsT1T2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public void Tuple_CanBeMatched()
}

[Test]
public void Tuple_CanBeMatchedViaOr()
public void ValueTuple_CanBeMatchedViaOr()
{
var tuple = Tuple.Create(1, "a");
var tuple = (1, "a");
var result = tuple.Match().To<bool>().With(2, "a").Or(1, "a").Do((x, y) => true).Result();
Assert.IsTrue(result);
}
Expand All @@ -33,9 +33,9 @@ public void Tuple_CanBeMatchedUsingIntWildcard()
}

[Test]
public void Tuple_CanBeMatchedUsingStringWildcard()
public void ValueTuple_CanBeMatchedUsingStringWildcard()
{
var tuple = Tuple.Create(1, "a");
var tuple = (1, "a");
var result = tuple.Match().To<bool>().With(1, _).Do((x, y) => true).Result();
Assert.IsTrue(result);
}
Expand Down

0 comments on commit a40f1d5

Please sign in to comment.