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 Report
@@ Coverage Diff @@
## master #1015 +/- ##
=======================================
Coverage 92.57% 92.58%
=======================================
Files 113 113
Lines 3422 3426 +4
Branches 1055 1057 +2
=======================================
+ Hits 3168 3172 +4
Misses 191 191
Partials 63 63
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
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 makign is how many lambdas the developer has to author.
There's no performance argument being made for now. |
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
?