Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cons pattern matching for IEnumerable<T> #36

Open
DavidArno opened this issue May 30, 2017 · 3 comments
Open

Add cons pattern matching for IEnumerable<T> #36

DavidArno opened this issue May 30, 2017 · 3 comments

Comments

@DavidArno
Copy link
Owner

var result = someEmptyList.Match().To<int>()
                          .Empty().Do(0)
                          .Result();
// result == 0

var list = new List<int>{1};
var result = list.Match().To<int>()
                 .Empty().Do(0)
                 .Single().Do(x => x)
                 .Result();
// result == 1

var list2 = new List<int>{0};
var result = list2.Match().To<int>()
                  .Empty().Do(0)
                  .Single().Where(x => x > 1).Do(1)
                  .Single().Do(2)
                  .Result();
// result == 2

var list3 = new List<int>{1, 2};
var result = list3.Match().To<int>()
                  .Empty().Do(0)
                  .Single().Do(x => x)
                  .Cons().Do((_, t) => t)
                  .Result();
// result == 2

Requiring Cons().Do() to recursively process the collection would be very inefficient. So RecurseTail method will be available to allow the collection to be iterated over:

var list4 = new List<int>{1, 2, 3, 4};
var result = list4.Match().To<int>()
                  .Single().Do(x => x)
                  .Cons().RecurseTail().Do((head, result) => head + result)
                  .Result();
// result == 1 + 2 + 3 + 4 == 10

My initial thoughts on how to best handle this, if RecurseTail is used, then we iterate over the elements, storing them in eg a stack and then pop them off one at a time, applying the Single match to the last item, and Cons matches to everything else to arrive at a final result.

Not sure yet if an Action version is even needed.

@DavidArno DavidArno added this to the V3.1.0 milestone May 30, 2017
DavidArno added a commit that referenced this issue May 30, 2017
This is taking a TDD approach, so code is currently written to make the tests pass and needs refactoring.
DavidArno added a commit that referenced this issue Jun 27, 2017
Reverted the changes made on the master branch for this new feature;
Created new v3.1.0 branch;
Re-instated those changes on that latter branch.
DavidArno added a commit that referenced this issue Aug 6, 2017
Additionally, some tidy up, switching code to take advantage of throw expression bodies and local functions.
@DavidArno
Copy link
Owner Author

Added to v3.1. Still needs documenting though.

@DavidArno DavidArno self-assigned this Oct 10, 2019
@DavidArno DavidArno modified the milestones: V3.1.0, V4.0 Oct 10, 2019
@DavidArno
Copy link
Owner Author

Moved to the v4 milestone as the documentation for this will be released with that release.

@DavidArno
Copy link
Owner Author

v4 is released, but it still needs documenting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant