-
Notifications
You must be signed in to change notification settings - Fork 415
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 simpler Unfold
overload
#1015
base: master
Are you sure you want to change the base?
Conversation
How is this different than |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1015 +/- ##
==========================================
+ Coverage 93.23% 93.24% +0.01%
==========================================
Files 112 112
Lines 3400 3407 +7
Branches 962 965 +3
==========================================
+ Hits 3170 3177 +7
Misses 214 214
Partials 16 16
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
That specific example is not, but For comparison, below is a slightly more involved example that generates the Fibonacci sequence where all numbers are less than 100. Using var fibonacciNumbersLowerThan100 =
MoreEnumerable.Generate((Curr: 0, Next: 1), s => (s.Next, s.Curr + s.Next))
.TakeWhile(s => s.Curr < 100)
.Select(s => s.Curr); Using var fibonacciNumbersLowerThan100 =
MoreEnumerable.Unfold((Curr: 0, Next: 1),
s => (s.Curr, Next: (Curr: s.Next, Next: s.Curr + s.Next)),
s => s.Curr < 100,
s => s.Next,
s => s.Curr); Using the overload proposed here (requires 1 lambda): var fibonacciNumbersLowerThan100 =
MoreEnumerable.Unfold((Curr: 0, Next: 1),
s => s.Curr < 100 ? (true, (s.Next, s.Curr + s.Next), s.Curr) : default); |
nit: for this example, this would be cleaner as it does not rely on a ternary or var fibonacciNumbersLowerThan100 =
MoreEnumerable.Unfold((Curr: 0, Next: 1),
s => (s.Curr < 100, (s.Next, s.Curr + s.Next), s.Curr)); That said, I'm curious to whom you see this as a potential benefit.
|
Yes, but with the ternary (for illustration) and more generally, you avoid wasteful computation (like even
People used to unfold from other languages and/or runtimes/libraries (such as someone used to
It's not trying to be simpler than
The implementation is immaterial as it can change so the point I was making is how many lambdas the developer has to author.
There's no performance argument being made for now. |
Conflicts resolved: - MoreLinq/PublicAPI/net6.0/PublicAPI.Unshipped.txt - MoreLinq/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt - MoreLinq/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt
This PR adds a simpler overload of
Unfold
that requires only a single function instead of 3. Example usage:Prints:
Pending work:
(bool, TState, TResult)
or(bool, (TState, TResult))
?TState
thenTResult
orTResult
thenTState
?