coro

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2023 License: 0BSD Imports: 3 Imported by: 0

README

coro

Coroutines for Go version 1.18 and later.

Documentation

Overview

Package coro provides an implementation of coroutines built on top of Go's goroutines for the execution, suspension and resuming of generalized subroutines and functions for cooperative multitasking.

Based on the wonderful blog post shared by Rus Cox.

Index

Constants

This section is empty.

Variables

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

ErrStopped signals to the generator that it must stop.

Functions

func PullFn

func PullFn[I, O any](fn GeneratorFn[I, O]) (func(I) (O, bool), func())

func PullSub

func PullSub[I, O any](fn GeneratorSub[I, O]) (func(I) (O, bool), func())

Types

type C

type C[I, O any] struct {
	// contains filtered or unexported fields
}

C represents a coroutine encapsulating a generator that is executed in a goroutine.

func NewFn

func NewFn[I, O any](fn GeneratorFn[I, O]) *C[I, O]

NewFn encapsulates the given GeneratorFn generator in a C coroutine.

func NewSub

func NewSub[I, O any](fn GeneratorSub[I, O]) *C[I, O]

NewSub encapsulates the given GeneratorSub generator in a C coroutine.

func (*C[I, O]) Resume

func (cr *C[I, O]) Resume(inp I) (out O, ok bool)

Resume resumes the coroutine.

Resume sends a given value to the encapsulated generator, causing the encapsulated generator to resume its operation and blocks the caller until the generator sends back a new value, returns or panics.

func (*C[I, O]) Stop

func (cr *C[I, O]) Stop()

Stop stops the coroutine.

Stop causes the encapsulated generator to panic with an ErrStopped derived value on the next invocation of YieldFn.

It is expected that the generator does not recover from the panic invocation and returns from its execution.

type GeneratorFn

type GeneratorFn[I, O any] func(I, func(O) I) O

GeneratorFn is a function that generates values which are sent back through the given YieldFn function.

The function has a return value that will be sent back as the last value generated by the generator.

type GeneratorSub

type GeneratorSub[I, O any] func(I, func(O) I)

GeneratorSub is a subroutine (a function without a return value) that generates values which are sent back through the given YieldFn function.

Contrary to GeneratorFn the function has no return value that will be sent back as the last value produced by the generator.

type YieldFn

type YieldFn[I, O any] func(O) I

YieldFn is a function that is called by a generator to send back values generated by the same generator.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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