Standard library / iter

iter.Pull()

Converts a push sequence to next and stop functions. next returns zero,false after exhaustion or stop. Call stop if abandoning early; concurrent next/stop calls are invalid. Expected stdout: '7 true\n0 false\n'.

Formal Syntax & Structure

go syntaxgo
func Pull[V any](seq Seq[V]) (next func() (V, bool), stop func())

Parameters & Attributes

NameTypeDescription
seqSeq[V]Iterator function producing the input sequence.

Return Value

(next func() (V, bool), stop func())

next retrieves a value and validity flag; stop terminates the iterator.

Working Example

Example Snippetgo
package main
import (  "fmt"  "iter"  "slices")
func main() {  next, stop := iter.Pull(slices.Values([]int{7, 8}))  defer stop()  v, ok := next()  fmt.Println(v, ok)  stop()  v, ok = next()  fmt.Println(v, ok)}
Go Reference