-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #36 - started adding Match().ReduceTo() functionality.
- Loading branch information
Showing
10 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace SuccincT.PatternMatchers | ||
{ | ||
public interface IReducerMatcher<T, TResult> | ||
{ | ||
IReducerNoneHandler<T, TResult> Empty(); | ||
|
||
IReducerSingleHandler<T, TResult> Single(); | ||
|
||
//IReducerRecursiveConsHandler<T, TResult> RecursiveCons(); | ||
|
||
IEnumerable<TResult> Result(); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace SuccincT.PatternMatchers | ||
{ | ||
public interface IReducerNoneHandler<T, TResult> | ||
{ | ||
IReducerMatcher<T, TResult> Do(TResult doValue); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
|
||
namespace SuccincT.PatternMatchers | ||
{ | ||
public interface IReducerSingleHandler<T, TResult> | ||
{ | ||
IReducerMatcher<T, TResult> Do(TResult doValue); | ||
IReducerMatcher<T, TResult> Do(Func<T,TResult> doFunc); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using SuccincT.Functional; | ||
|
||
namespace SuccincT.PatternMatchers | ||
{ | ||
internal class ReducerMatcher<T, TResult> : IReducerMatcher<T, TResult>, | ||
IReducerNoneHandler<T, TResult>, | ||
IReducerSingleHandler<T, TResult> | ||
{ | ||
private readonly IEnumerable<T> _collection; | ||
private TResult _noneValue; | ||
private List<Func<T, TResult>> _singleTestDos; | ||
|
||
public ReducerMatcher(IEnumerable<T> collection) | ||
{ | ||
_collection = collection; | ||
_singleTestDos = new List<Func<T, TResult>>(); | ||
} | ||
|
||
IReducerNoneHandler<T, TResult> IReducerMatcher<T, TResult>.Empty() => this; | ||
|
||
IReducerSingleHandler<T, TResult> IReducerMatcher<T, TResult>.Single() => this; | ||
|
||
IReducerMatcher<T, TResult> IReducerNoneHandler<T, TResult>.Do(TResult doValue) | ||
{ | ||
_noneValue = doValue; | ||
return this; | ||
} | ||
|
||
IReducerMatcher<T, TResult> IReducerSingleHandler<T, TResult>.Do(Func<T, TResult> doFunc) | ||
{ | ||
_singleTestDos.Add(doFunc); | ||
return this; | ||
} | ||
|
||
IReducerMatcher<T, TResult> IReducerSingleHandler<T, TResult>.Do(TResult doValue) | ||
{ | ||
_singleTestDos.Add(_ => doValue); | ||
return this; | ||
} | ||
|
||
IEnumerable<TResult> IReducerMatcher<T, TResult>.Result() | ||
{ | ||
if (!_collection.Any()) | ||
{ | ||
return new ConsEnumerable<TResult>(_noneValue); | ||
} | ||
return new ConsEnumerable<TResult>(_singleTestDos[0](_collection.First())); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
54 changes: 54 additions & 0 deletions
54
tests/SuccincT.Tests/SuccincT/PatternMatchers/ReducerMatcherTests.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NUnit.Framework; | ||
using SuccincT.Functional; | ||
using SuccincT.PatternMatchers; | ||
|
||
namespace SuccincTTests.SuccincT.PatternMatchers | ||
{ | ||
[TestFixture] | ||
public class ReducerMatcherTests | ||
{ | ||
[Test] | ||
public void EmptyList_ResultsInOneElementEnumerationWhenEmptyClauseUsed() | ||
{ | ||
var list = new List<int>(); | ||
var result = list.Match().ReduceTo<(int, int)>() | ||
.Empty().Do((0, 0)) | ||
.Result(); | ||
|
||
var consResult = result.ToConsEnumerable(); | ||
Assert.AreEqual(1, consResult.Count()); | ||
var (head, _) = consResult; | ||
Assert.AreEqual((0, 0), head); | ||
} | ||
|
||
[Test] | ||
public void SingleList_ResultsInOneElementEnumerationWhenValueSingleClauseUsed() | ||
{ | ||
var list = new List<int> { 1 }; | ||
var result = list.Match().ReduceTo<(int, int)>() | ||
.Single().Do((0, 1)) | ||
.Result(); | ||
|
||
var consResult = result.ToConsEnumerable(); | ||
Assert.AreEqual(1, consResult.Count()); | ||
var (head, _) = consResult; | ||
Assert.AreEqual((0, 1), head); | ||
} | ||
|
||
[Test] | ||
public void SingleList_ResultsInOneElementEnumerationWhenFuncSingleClauseUsed() | ||
{ | ||
var list = new List<int> { 1 }; | ||
var result = list.Match().ReduceTo<(int, int)>() | ||
.Single().Do(x => (x, 0)) | ||
.Result(); | ||
|
||
var consResult = result.ToConsEnumerable(); | ||
Assert.AreEqual(1, consResult.Count()); | ||
var (head, _) = consResult; | ||
Assert.AreEqual((1, 0), head); | ||
} | ||
} | ||
} |