Add support for Trie.Walk() and Node.SetValue()#4
Conversation
… Value, using eval function
…o avoid non deterministic sort.
|
hi, I have push on my fork some comments, and update the check so, it can check the node.SetValue() during the Walk. The Filter func is easier to understand, because it return a searchResult and that what you expect with a search by value. You have a simple signature with a selector function. I have rewrite the filter, as you do the walk. look at filter.go |
|
I think I disagree. Yes, filter function that returns the result list is convenient - users don't have to write code to append to a result that they would've to do with Walk func. But Walk func in general gives users more control over what to do in the traversal - one of them being filtering. Therefore, it's a super-feature to filtering. Example of when Walk func will be faster than filtering+trie.Put would be when you need to update value in tree traversal: tri.Walk(nil, func (key []string, node *Node) error {
if node.Value() == 2 {
node.SetValue(3)
}
return nil
}) |
|
Yes I agree Walk function is great to update tree along the traversal, because it is it main purpose. var selected []string
walker := func(key []string, node *trie.Node) error {
what := node.Value().(int)
if what == 3 {
selected = key
return errors.New("found")
}
return nil
}the function use selected variable define in is outer scope, as if this was a closure, it's hard to understand. Whereas the filter function return a result, and it is quite ubiquitous to many language (ruby , js ). the 2 functions have differents usage, sure you can do select and update with Walk, but you to explain usage in documentation, because that's hard to figure. |
|
I don't think Walk/Anonymous functions are ugly. Only when the anon function definition becomes huge it becomes a code smell. At which point it can be moved to a separate function. Overall, I feel that Filter functionality should be written by the users as required using Walk. Don't feel it needs to be part of the library. So, I'm going to merge this PR now. func Filter(tri *trie.Trie, f func(interface{}) bool) (filtered []*trie.SearchResult) {
_ = tri.Walk(nil, func(key []string, node *trie.Node) error {
if f(node.Value()) {
filtered = append(filtered, &trie.SearchResult{
Key: key,
Value: node.Value(),
})
}
return nil
})
return
} |
|
hi, |
Extending on work done here: #3