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

Refactored Traverse implementation #553

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

leandromoh
Copy link
Collaborator

Refactored Traverse implementation to avoid repetitive code.

@atifaziz atifaziz requested a review from fsateler December 31, 2018 12:33
// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid the reverse by using a stack (or it is a queue), of children enumerator. We should take care of the dispose.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe that can be a separate change.

Copy link
Member

@fsateler fsateler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I'm not sure the deduplicated code really makes things more readable. There is a level on indirection on methods that are quite small. @atifaziz WDYT?

@@ -88,19 +78,30 @@ public static IEnumerable<T> TraverseDepthFirst<T>(T root, Func<T, IEnumerable<T
{
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe that can be a separate change.

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

Successfully merging this pull request may close these issues.

3 participants