Why there is no getAllNodes() function?
ZigGreen opened this issue · 4 comments
How would you like to use that API?
I know that it's possible to get nodes over forEach but code looks more complicated.
And I would like to apply some patterns like filtering or map/reduce
Map/reduce makes sense. I wanted to keep underlying data structure private, so that we could have consistent representation of a graph. There are several potential ways to implement getAllNodes()
and I couldn't justify adding either of them into the core due to drawbacks.
Option 1: Return array of nodes as is
This will be O(1)
time/space complexity, which is awesome. The drawback is that someone can mutate the array, and damage consistency. The graph will not notify consumers that it was modified.
Option 2: Make a copy
This will make sure that the graph model is consistent. The drawback is that it will require O(n)
time/space complexity, which is not nice at all.
That said, I think implementing a utility package outside of the core is perfectly valid. It could be something similar to lodash for graphs: Let consumers perform map/reduce/filter/etc. operations on graph nodes.
I'd be glad to provide feedback if you decide to implement it. Please let me know.