aboutsummaryrefslogtreecommitdiffstats
path: root/tree.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-08-15 23:09:33 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2016-08-15 23:09:33 +0200
commit6b9a59be60de5b66aee14e9160ace80734008eca (patch)
treec302776360b0fd4f774f67f177870fda478b4258 /tree.go
parented2e3b299e03e4bfd4c37bf5232e9fde05c0600d (diff)
downloadgo-git-6b9a59be60de5b66aee14e9160ace80734008eca.tar.gz
core: *Iter.ForEach method
Diffstat (limited to 'tree.go')
-rw-r--r--tree.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/tree.go b/tree.go
index d8682d0..150cdab 100644
--- a/tree.go
+++ b/tree.go
@@ -241,6 +241,33 @@ func (iter *TreeIter) Next() (*Tree, error) {
}
}
+// ForEach call the cb function for each tree contained on this iter until
+// an error happends or the end of the iter is reached. If core.ErrStop is sent
+// the iteration is stop but no error is returned
+func (iter *TreeIter) ForEach(cb func(*Tree) error) error {
+ i := &TreeIter{w: *NewTreeWalker(iter.w.r, iter.w.t)}
+ defer i.Close()
+
+ for {
+ t, err := i.Next()
+ if err != nil {
+ if err == io.EOF {
+ return nil
+ }
+
+ return err
+ }
+
+ if err := cb(t); err != nil {
+ if err == core.ErrStop {
+ return nil
+ }
+
+ return err
+ }
+ }
+}
+
// Close closes the TreeIter
func (iter *TreeIter) Close() {
iter.w.Close()