Refactored Traverse implementation #553
Open
leandromoh wants to merge 2 commits intomorelinq:masterfrom
Open
Conversation
Orace
reviewed
Oct 31, 2019
| // if we want to traverse the returned list in the same order as was returned to us | ||
|
|
||
| var stack = new Stack<T>(); | ||
| return TraverseImpl(root, x => childrenSelector(x).Reverse(), stack.Push, stack.Pop, stack); |
Contributor
There was a problem hiding this comment.
We can avoid the reverse by using a stack (or it is a queue), of children enumerator. We should take care of the dispose.
Member
There was a problem hiding this comment.
Maybe that can be a separate change.
fsateler
requested changes
Dec 28, 2022
| if (childrenSelector == null) throw new ArgumentNullException(nameof(childrenSelector)); | ||
|
|
||
| // because a stack pops the elements out in LIFO order, we need to push them in reverse | ||
| // if we want to traverse the returned list in the same order as was returned to us |
Member
There was a problem hiding this comment.
This comment is a bit out of context here. Maybe it would be good to be explicit that this applies to the Reverse in the childrenSelector
| T root, | ||
| Func<T, IEnumerable<T>> childrenSelector, | ||
| Action<T> Push, | ||
| Func<T> Pop, |
Member
There was a problem hiding this comment.
I don't think we have any other method naming arguments in Capital case.
| // if we want to traverse the returned list in the same order as was returned to us | ||
|
|
||
| var stack = new Stack<T>(); | ||
| return TraverseImpl(root, x => childrenSelector(x).Reverse(), stack.Push, stack.Pop, stack); |
Member
There was a problem hiding this comment.
Maybe that can be a separate change.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refactored Traverse implementation to avoid repetitive code.