Skip to content

Commit

Permalink
Issue #8
Browse files Browse the repository at this point in the history
* Moved Either<,> to Unions, where it should have been in the first place. Treating this as a non-breaking change as it's an undocumented partial addition to v2.2.0.

* Changed the way "any" matches are constructed for tuples. This now relies on parameters of With and Or being Either<T,Any> and the Either<,> type supporting implicit conversions from T and Any to Either<T,Any>. This removed the need to add just shy of 100 new methods to the three matcher classes used by tuple matching, plus to the interfaces and 25 or so to the tuple creation class etc.

* As mentioned above, the Either<T1,T2> type now supports implicit conversions from T1 and T2.

Added a bunch more tests for "nay matching" on tuples. This feature now appears to be complete.
  • Loading branch information
DavidArno committed Feb 2, 2017
1 parent a7389e4 commit 44f2fd2
Show file tree
Hide file tree
Showing 31 changed files with 499 additions and 620 deletions.
11 changes: 5 additions & 6 deletions SuccincT/Functional/ConsListBuilderEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

namespace SuccincT.Functional
{
internal sealed class ConsListBuilderEnumerator<TConsNode, T> : IEnumerator<TConsNode> where
TConsNode : ConsNode<T>, new()
internal sealed class ConsListBuilderEnumerator<T> : IEnumerator<ConsNode<T>>
{
private readonly IEnumerator<T> _enumerator;
private TConsNode _node;
private ConsNode<T> _node;

internal ConsListBuilderEnumerator(TConsNode node)
internal ConsListBuilderEnumerator(ConsNode<T> node)
{
_enumerator = node.Enumeration.GetEnumerator();
}
Expand All @@ -20,7 +19,7 @@ public bool MoveNext()
{
if (!_enumerator.MoveNext()) return false;

_node = new TConsNode
_node = new ConsNode<T>
{
Value = _enumerator.Current,
State = HasValue
Expand All @@ -31,7 +30,7 @@ public bool MoveNext()

public void Reset() { throw new NotSupportedException(); }

public TConsNode Current => _node;
public ConsNode<T> Current => _node;

object IEnumerator.Current => Current;

Expand Down
2 changes: 1 addition & 1 deletion SuccincT/Functional/ConsNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SuccincT.Functional
{
internal class ConsNode<T>
internal sealed class ConsNode<T>
{
internal ConsNodeState State { get; set; }
internal IEnumerable<T> Enumeration { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion SuccincT/Functional/ConsNodeEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public bool MoveNext()

if (_node.State == HasEnumeration)
{
_node.Enumerating(new ConsListBuilderEnumerator<ConsNode<T>, T>(_node));
_node.Enumerating(new ConsListBuilderEnumerator<T>(_node));
}

if (_node.State == HasValue) return true;
Expand Down
44 changes: 13 additions & 31 deletions SuccincT/PatternMatchers/EitherTuple.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,23 @@
namespace SuccincT.PatternMatchers
using SuccincT.Unions;

namespace SuccincT.PatternMatchers
{
internal static class EitherTuple
{
public static EitherTuple<T1, T2> Create<T1, T2>(T1 value1, T2 value2) =>
new EitherTuple<T1, T2>(value1, value2);

public static EitherTuple<T1, T2> Create<T1, T2>(Any value1, T2 value2) =>
new EitherTuple<T1, T2>(value1, value2);

public static EitherTuple<T1, T2> Create<T1, T2>(T1 value1, Any value2) =>
public static EitherTuple<T1, T2> Create<T1, T2>(Either<T1, Any> value1,
Either<T2, Any> value2) =>
new EitherTuple<T1, T2>(value1, value2);

public static EitherTuple<T1, T2> Create<T1, T2>(Any value1, Any value2) =>
new EitherTuple<T1, T2>(value1, value2);

public static EitherTuple<T1, T2, T3> Create<T1, T2, T3>(T1 value1, T2 value2, T3 value3) =>
new EitherTuple<T1, T2, T3>(value1, value2, value3);

public static EitherTuple<T1, T2, T3> Create<T1, T2, T3>(Any value1, T2 value2, T3 value3) =>
new EitherTuple<T1, T2, T3>(value1, value2, value3);

public static EitherTuple<T1, T2, T3> Create<T1, T2, T3>(T1 value1, Any value2, T3 value3) =>
public static EitherTuple<T1, T2, T3> Create<T1, T2, T3>(Either<T1, Any> value1,
Either<T2, Any> value2,
Either<T3, Any> value3) =>
new EitherTuple<T1, T2, T3>(value1, value2, value3);

public static EitherTuple<T1, T2, T3> Create<T1, T2, T3>(T1 value1, T2 value2, Any value3) =>
new EitherTuple<T1, T2, T3>(value1, value2, value3);

public static EitherTuple<T1, T2, T3> Create<T1, T2, T3>(Any value1, Any value2, T3 value3) =>
new EitherTuple<T1, T2, T3>(value1, value2, value3);

public static EitherTuple<T1, T2, T3> Create<T1, T2, T3>(Any value1, T2 value2, Any value3) =>
new EitherTuple<T1, T2, T3>(value1, value2, value3);

public static EitherTuple<T1, T2, T3> Create<T1, T2, T3>(T1 value1, Any value2, Any value3) =>
new EitherTuple<T1, T2, T3>(value1, value2, value3);

public static EitherTuple<T1, T2, T3> Create<T1, T2, T3>(Any value1, Any value2, Any value3) =>
new EitherTuple<T1, T2, T3>(value1, value2, value3);
public static EitherTuple<T1, T2, T3, T4> Create<T1, T2, T3, T4>(Either<T1, Any> value1,
Either<T2, Any> value2,
Either<T3, Any> value3,
Either<T4, Any> value4) =>
new EitherTuple<T1, T2, T3, T4>(value1, value2, value3, value4);
}
}
31 changes: 31 additions & 0 deletions SuccincT/PatternMatchers/EitherTuple{T1,T2,T3,T4}.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using SuccincT.Unions;

namespace SuccincT.PatternMatchers
{
internal struct EitherTuple<T1, T2, T3, T4>
{
private readonly Either<T1, Any> _value1;
private readonly Either<T2, Any> _value2;
private readonly Either<T3, Any> _value3;
private readonly Either<T4, Any> _value4;

internal EitherTuple(Either<T1, Any> value1,
Either<T2, Any> value2,
Either<T3, Any> value3,
Either<T4, Any> value4)
{
_value1 = value1;
_value2 = value2;
_value3 = value3;
_value4 = value4;
}

public bool MatchesTuple(Tuple<T1, T2, T3, T4> 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)) &&
(!_value4.IsLeft || EqualityComparer<T4>.Default.Equals(_value4.Left, tuple.Item4));
}
}
67 changes: 10 additions & 57 deletions SuccincT/PatternMatchers/EitherTuple{T1,T2,T3}.cs
Original file line number Diff line number Diff line change
@@ -1,69 +1,22 @@
using System;
using System.Collections.Generic;
using SuccincT.Functional;
using SuccincT.Unions;

namespace SuccincT.PatternMatchers
{
internal struct EitherTuple<T1, T2, T3>
{
private Either<T1, Any> _value1;
private Either<T2, Any> _value2;
private Either<T3, Any> _value3;
private readonly Either<T1, Any> _value1;
private readonly Either<T2, Any> _value2;
private readonly Either<T3, Any> _value3;

public EitherTuple(T1 value1, T2 value2, T3 value3)
public EitherTuple(Either<T1, Any> value1,
Either<T2, Any> value2,
Either<T3, Any> value3)
{
_value1 = new Either<T1, Any>(value1);
_value2 = new Either<T2, Any>(value2);
_value3 = new Either<T3, Any>(value3);
}

public EitherTuple(Any value1, T2 value2, T3 value3)
{
_value1 = new Either<T1, Any>(value1);
_value2 = new Either<T2, Any>(value2);
_value3 = new Either<T3, Any>(value3);
}

public EitherTuple(T1 value1, Any value2, T3 value3)
{
_value1 = new Either<T1, Any>(value1);
_value2 = new Either<T2, Any>(value2);
_value3 = new Either<T3, Any>(value3);
}

public EitherTuple(T1 value1, T2 value2, Any value3)
{
_value1 = new Either<T1, Any>(value1);
_value2 = new Either<T2, Any>(value2);
_value3 = new Either<T3, Any>(value3);
}

public EitherTuple(Any value1, Any value2, T3 value3)
{
_value1 = new Either<T1, Any>(value1);
_value2 = new Either<T2, Any>(value2);
_value3 = new Either<T3, Any>(value3);
}

public EitherTuple(Any value1, T2 value2, Any value3)
{
_value1 = new Either<T1, Any>(value1);
_value2 = new Either<T2, Any>(value2);
_value3 = new Either<T3, Any>(value3);
}

public EitherTuple(T1 value1, Any value2, Any value3)
{
_value1 = new Either<T1, Any>(value1);
_value2 = new Either<T2, Any>(value2);
_value3 = new Either<T3, Any>(value3);
}

public EitherTuple(Any value1, Any value2, Any value3)
{
_value1 = new Either<T1, Any>(value1);
_value2 = new Either<T2, Any>(value2);
_value3 = new Either<T3, Any>(value3);
_value1 = value1;
_value2 = value2;
_value3 = value3;
}

public bool MatchesTuple(Tuple<T1, T2, T3> tuple) =>
Expand Down
30 changes: 6 additions & 24 deletions SuccincT/PatternMatchers/EitherTuple{T1,T2}.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
using System;
using System.Collections.Generic;
using SuccincT.Functional;
using SuccincT.Unions;

namespace SuccincT.PatternMatchers
{
internal struct EitherTuple<T1, T2>
{
private Either<T1, Any> _value1;
private Either<T2, Any> _value2;
private readonly Either<T1, Any> _value1;
private readonly Either<T2, Any> _value2;

public EitherTuple(T1 value1, T2 value2)
public EitherTuple(Either<T1, Any> value1, Either<T2, Any> value2)
{
_value1 = new Either<T1, Any>(value1);
_value2 = new Either<T2, Any>(value2);
}

public EitherTuple(Any value1, T2 value2)
{
_value1 = new Either<T1, Any>(value1);
_value2 = new Either<T2, Any>(value2);
}

public EitherTuple(T1 value1, Any value2)
{
_value1 = new Either<T1, Any>(value1);
_value2 = new Either<T2, Any>(value2);
}

public EitherTuple(Any value1, Any value2)
{
_value1 = new Either<T1, Any>(value1);
_value2 = new Either<T2, Any>(value2);
_value1 = value1;
_value2 = value2;
}

public bool MatchesTuple(Tuple<T1, T2> tuple) =>
Expand Down
6 changes: 5 additions & 1 deletion SuccincT/PatternMatchers/IActionMatcher{T1,T2,T3,T4}.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using SuccincT.Unions;

namespace SuccincT.PatternMatchers
{
public interface IActionMatcher<T1, T2, T3, T4>
{
IActionWithHandler<IActionMatcher<T1, T2, T3, T4>, T1, T2, T3, T4> With(T1 value1, T2 value2, T3 value3, T4 value4);
IActionWithHandler<IActionMatcher<T1, T2, T3, T4>, T1, T2, T3, T4> With(Either<T1, Any> value1,
Either<T2, Any> value2,
Either<T3, Any> value3,
Either<T4, Any> value4);
IActionWhereHandler<IActionMatcher<T1, T2, T3, T4>, T1, T2, T3, T4> Where(Func<T1, T2, T3, T4, bool> expression);
IActionMatcherAfterElse Else(Action<T1, T2, T3, T4> action);
IActionMatcherAfterElse IgnoreElse();
Expand Down
13 changes: 5 additions & 8 deletions SuccincT/PatternMatchers/IActionMatcher{T1,T2,T3}.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using System;
using SuccincT.Unions;

namespace SuccincT.PatternMatchers
{
public interface IActionMatcher<T1, T2, T3>
{
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3> With(T1 value1, T2 value2, T3 value3);
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3> With(Any value1, T2 value2, T3 value3);
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3> With(T1 value1, Any value2, T3 value3);
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3> With(T1 value1, T2 value2, Any value3);
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3> With(Any value1, Any value2, T3 value3);
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3> With(Any value1, T2 value2, Any value3);
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3> With(T1 value1, Any value2, Any value3);
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3> With(Any value1, Any value2, Any value3);
IActionWithHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3> With(Either<T1, Any> value1,
Either<T2, Any> value2,
Either<T3, Any> value3);

IActionWhereHandler<IActionMatcher<T1, T2, T3>, T1, T2, T3> Where(Func<T1, T2, T3, bool> expression);

IActionMatcherAfterElse Else(Action<T1, T2, T3> action);
Expand Down
6 changes: 2 additions & 4 deletions SuccincT/PatternMatchers/IActionMatcher{T1,T2}.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using SuccincT.Unions;

namespace SuccincT.PatternMatchers
{
public interface IActionMatcher<T1, T2>
{
IActionWithHandler<IActionMatcher<T1, T2>, T1, T2> With(T1 value1, T2 value2);
IActionWithHandler<IActionMatcher<T1, T2>, T1, T2> With(Any value1, T2 value2);
IActionWithHandler<IActionMatcher<T1, T2>, T1, T2> With(T1 value1, Any value2);
IActionWithHandler<IActionMatcher<T1, T2>, T1, T2> With(Any value1, Any value2);
IActionWithHandler<IActionMatcher<T1, T2>, T1, T2> With(Either<T1, Any> value1, Either<T2, Any> value2);
IActionWhereHandler<IActionMatcher<T1, T2>, T1, T2> Where(Func<T1, T2, bool> expression);
IActionMatcherAfterElse Else(Action<T1, T2> action);
IActionMatcherAfterElse IgnoreElse();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using SuccincT.Unions;

namespace SuccincT.PatternMatchers
{
public interface IActionWithHandler<out TMatcher, T1, T2, T3, T4>
{
IActionWithHandler<TMatcher, T1, T2, T3, T4> Or(T1 value1, T2 value2, T3 value3, T4 value4);
IActionWithHandler<TMatcher, T1, T2, T3, T4> Or(Either<T1, Any> value1,
Either<T2, Any> value2,
Either<T3, Any> value3,
Either<T4, Any> value4);

TMatcher Do(Action<T1, T2, T3, T4> action);
}
Expand Down
12 changes: 4 additions & 8 deletions SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2,T3}.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using System;
using SuccincT.Unions;

namespace SuccincT.PatternMatchers
{
public interface IActionWithHandler<out TMatcher, T1, T2, T3>
{
IActionWithHandler<TMatcher, T1, T2, T3> Or(T1 value1, T2 value2, T3 value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(Any value1, T2 value2, T3 value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(T1 value1, Any value2, T3 value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(T1 value1, T2 value2, Any value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(Any value1, Any value2, T3 value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(Any value1, T2 value2, Any value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(T1 value1, Any value2, Any value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(Any value1, Any value2, Any value3);
IActionWithHandler<TMatcher, T1, T2, T3> Or(Either<T1, Any> value1,
Either<T2, Any> value2,
Either<T3, Any> value3);

TMatcher Do(Action<T1, T2, T3> action);
}
Expand Down
6 changes: 2 additions & 4 deletions SuccincT/PatternMatchers/IActionWithHandler{TM,T1,T2}.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using SuccincT.Unions;

namespace SuccincT.PatternMatchers
{
public interface IActionWithHandler<out TMatcher, T1, T2>
{
IActionWithHandler<TMatcher, T1, T2> Or(T1 value1, T2 value2);
IActionWithHandler<TMatcher, T1, T2> Or(T1 value1, Any value2);
IActionWithHandler<TMatcher, T1, T2> Or(Any value1, T2 value2);
IActionWithHandler<TMatcher, T1, T2> Or(Any value1, Any value2);
IActionWithHandler<TMatcher, T1, T2> Or(Either<T1, Any> value1, Either<T2, Any> value2);

TMatcher Do(Action<T1, T2> action);
}
Expand Down
9 changes: 5 additions & 4 deletions SuccincT/PatternMatchers/IFuncMatcher{T1,T2,T3,T4,TR}.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using SuccincT.Unions;

namespace SuccincT.PatternMatchers
{
public interface IFuncMatcher<T1, T2, T3, T4, TResult>
{
IFuncWithHandler<IFuncMatcher<T1, T2, T3, T4, TResult>, T1, T2, T3, T4, TResult> With(T1 value1,
T2 value2,
T3 value3,
T4 value4);
IFuncWithHandler<IFuncMatcher<T1, T2, T3, T4, TResult>, T1, T2, T3, T4, TResult> With(Either<T1, Any> value1,
Either<T2, Any> value2,
Either<T3, Any> value3,
Either<T4, Any> value4);

IFuncWhereHandler<IFuncMatcher<T1, T2, T3, T4, TResult>, T1, T2, T3, T4, TResult> Where(
Func<T1, T2, T3, T4, bool> expression);
Expand Down
Loading

0 comments on commit 44f2fd2

Please sign in to comment.