genit

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package genit allows for stateful running goroutines to be used with iterators.

Iterator functions:

Index

Constants

This section is empty.

Variables

View Source
var ErrStopGenerator = errors.New("stop generator")

ErrStopGenerator signals to the generator that it must stop.

Functions

This section is empty.

Types

type G

type G[T any] struct {
	// contains filtered or unexported fields
}

G represents a Generator producing items which can be received through an iterator.

func (*G[T]) Send

func (g *G[T]) Send(v T)

Send is called from a running GeneratorFn and sends the given value to the iterator caller.

It panics when the Generator has to stop.

type GIterator

type GIterator[T any] struct {
	*chanit.ChannelIterator[T]
	// contains filtered or unexported fields
}

GIterator represents the receiver facing Iterator end of a Generator.

func Generator

func Generator[T any](fn GeneratorFn[T]) *GIterator[T]

Generator starts the GeneratorFn function as a new GIterator.

The Generator will be stopped automatically when the returned GIterator is garbage collected.

The Generator API is experimental and probably will change.

func GeneratorNoGC

func GeneratorNoGC[T any](fn GeneratorFn[T]) *GIterator[T]

GeneratorNoGC starts the GeneratorFn function as a new GIterator.

Please consider using Generator for simplicity reasons unless the Garbage Collector is a concern.

The Generator will not be stopped automatically and genit.GIterator.Stop must be called on the returned iterator to stop the generator manually.

The Generator API is experimental and probably will change.

func (*GIterator[T]) Iter

func (gi *GIterator[T]) Iter() itkit.Iterator[T]

Iter returns the underlying iterator of the GIterator yielding items produced by the generator until the generator function is gone.

func (*GIterator[T]) Next

func (gi *GIterator[T]) Next() bool

Next fetches the next Item produced by the Generator and returns true whenever there is a new item available and false otherwise.

func (*GIterator[T]) Stop

func (gi *GIterator[T]) Stop()

Stop stops the Generator.

The Generator Function will be stopped through a panic call that is meant to kill the running generator.

The Generator goroutine will be gone after this function completes.

type GeneratorFn

type GeneratorFn[T any] func(g *G[T])

GeneratorFn is a function that generates items and sends them to the Iterator through G.Send.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL