proposal: iter: add Push
function
#72083
Labels
LibraryProposal
Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool
Proposal
Milestone
Proposal Details
This proposal stems from a thread in golang-nuts.
The idea is to add a
Push
function to packageiter
(and likely a similarPush2
):This is similar in concept to the
Pull
andPull2
functions, except thatyield
andstop
are used to push values intoseq
.It is based on a similar proposal by @ianlancetaylor in the thread, which unfortunately would not hide goroutine creation/management (a goal of mine).
Their worry with my counter proposal (this one) is, quoting:
However, ISTM that it is possible with my proposal (although potentially racy) to pass the iterator to another goroutine, you just need to ensure this goroutine terminates before
consumer
(perhaps with async.WaitGroup
). The potential races don't seem worse than those caused by passingnext
/stop
fromPull
to other goroutines.Purpose
The purpose of this function is basically the same as
Pull
: adapt/plug existing iteration APIs to the "standard"iter.Seq
interface.It came up when trying to use a function with this signature:
func processor(seq iter.Seq[float64]) float64
(a function that receives a sequence of floats, and outputs a single float, e.g. the sum/average/etc) to implement this interface (which allows you to implement custom aggregate functions in SQLite:In the simple case SQLite calls the
AggregateFunction.Step
once for each row (arg
are the columns of the row, a single column isarg[0].Float()
), and thenValue
once to get the aggregate result (you callctx.ResultFloat(x)
to return the result).It turns out that it's impossible to use a
processor
function like the above to implement the interface, without:Proposed semantics and implementation based on channels
There's a race (and potential
panic: send on closed channel
) between theclose(swtch)
and theswtch <- struct{}{}
inconsumer
ifconsumer
starts a goroutine and theseq
outlivesconsumer
. Otherwise, there is no parallelism, only concurrency.The implementation takes liberally from
iter.Pull
and can translate toruntime.newcoro
/coroswitch
by replacing theswtch
channel. In that case, thepanic
becomesfatal error: coroswitch on exited coro
.The text was updated successfully, but these errors were encountered: