I've been playing with with rangefunc experiment, with help from
https://go.dev/wiki/RangefuncExperiment and the possible idioms that might come
out of it (https://blog.perfects.engineering/go_range_over_funcs is a good
read).
One somewhat eccentric use of nested iterators I built in the past in python
was turning an SQL widerows or domain aggregate result into a set of nested
objects, so one could use the results in something like the following way:
for p in people:
for c in p.cars:
for t in c.tickets:
print("person {} in car {} got ticket {}", p, c, t)
While I was able to get a very janky version of this type of behaviour with
https://go.dev/play/p/gFUcKNSrbMV?v=gotip this only has an iterator on the left
hand side and series of nested structs through slices. My attempts to use more
iterators (for cars and tickets) fails as these of course stop after the first
set of cars and tickets respectively have been yielded.
I realise this is a contrived example, but I wonder if there might be more
general cases where iterators could be stopped and restarted. (The docs to
iter.Pull suggest next() and stop() are non-resettable also.) Perhaps there
could be hidden new iter.Seq constructors in the container for when the cars
and tickets iterators are exhausted...hmm...
I'd be grateful for any thoughts about this casual and hypothetical case,
although I guess it could be helpful for something like retrieving nested data
from an sql cursor efficiently.
Cheers
Rory
That code above turns:
a a1 a2 b1 b2 b3 c1 c2 c3
a a1 a2 b1 b2 b3 c4 c5 c6
a a1 a2 b1 b2 b3 c7 c8 c9
a a1 a2 b4 b5 b6 c10 c11 c12
d d1 d2 e1 e2 e3 f1 f2 f3
d d1 d2 e1 e2 e3 f4 f5 f6
g g1 g2 h1 h2 h3 i1 i2 i3
into:
[a a1 a2]
> [b1 b2 b3]
> > [c1 c2 c3]
> > [c4 c5 c6]
> > [c7 c8 c9]
> [b4 b5 b6]
> > [c10 c11 c12]
[d d1 d2]
> [e1 e2 e3]
> > [f1 f2 f3]
> > [f4 f5 f6]
[g g1 g2]
> [h1 h2 h3]
> > [i1 i2 i3]
called like this:
for a := range collection.Iter() { // iter.Seq
fmt.Println(a.r)
for _, b := range a.s { // slice -- could be iter.Seq?
fmt.Println("> ", b.r)
for _, c := range b.s { // slice -- could be
iter.Seq?
fmt.Println("> > ", c.r)
}
}
}
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/Zk94CqLUJ6fJjmuA%40campbell-lange.net.