itermania

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2024 License: MIT Imports: 2 Imported by: 0

README

itermania

proof of concept of Go 1.23 range over func

example

prime := Bind(Inc(2), func(n int) Gen[int] {
	return Where(Const(n), All(Not(Eq(Mod(Const(n), Range(2, n, 1)), Const(0)))))
})

for i := range Head(prime, 10)() {
	fmt.Println(i)
}
2
3
5
7
11
13
17
19
23
29

test

GOEXPERIMENT=aliastypeparams GODEBUG=gotypesalias=1 go test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bin

func Bin[V, W any](op func(V, V) W) func(Gen[V], Gen[V]) Gen[W]

Bin returns a generator from two generators and a binary operation

func Head[V any](gen Gen[V], n int) func() iter.Seq[V]

Head returns a generator to iterator first n elements in gen.

func ToSlice

func ToSlice[V any](gen Gen[V]) []V

ToSlice produces a slice iterated from gen.

Caution: This hangs up if the iteration is infinite.

func Uni

func Uni[V, W any](op func(V) W) func(Gen[V]) Gen[W]

Uni returns a generator from a generators and a unary operation

Types

type Gen

type Gen[V any] = func() iter.Seq[V]

Gen is a generator function that generates a new iterator

func Add

func Add[V constraints.Ordered](xGen Gen[V], yGen Gen[V]) Gen[V]

func All

func All(gen Gen[bool]) Gen[bool]

func And

func And[V bool](xGen Gen[bool], yGen Gen[bool]) Gen[bool]

func Any

func Any(gen Gen[bool]) Gen[bool]

func Bind

func Bind[V, W any](gen Gen[V], f func(V) Gen[W]) Gen[W]

Bind applies f to each values iterated from gen.

func Const

func Const[V any](v V) Gen[V]

Const returns a generator to iterate the argument v once.

func Dec

func Dec[V constraints.Integer](v V) Gen[V]

Dec returns a generator of integers decreasing by one from v.

func Div

func Div[V Number](xGen Gen[V], yGen Gen[V]) Gen[V]

func Eq

func Eq[V comparable](xGen Gen[V], yGen Gen[V]) Gen[bool]

func FromSlice

func FromSlice[V any](values []V) Gen[V]

FromSlice creates a generator which iterates over the slice.

func Ge

func Ge[V constraints.Ordered](xGen Gen[V], yGen Gen[V]) Gen[bool]

func Gt

func Gt[V constraints.Ordered](xGen Gen[V], yGen Gen[V]) Gen[bool]

func If

func If[V any](condGen Gen[bool], thenGen Gen[V], elseGen Gen[V]) Gen[V]

If works as an if-expression for generators.

NOTE: regardless of cond, both then and else are always evaluated

func Inc

func Inc[V constraints.Integer](v V) Gen[V]

Inc returns a generator of integers increasing by one from v.

func Le

func Le[V constraints.Ordered](xGen Gen[V], yGen Gen[V]) Gen[bool]

func Loop

func Loop[V any](v V) Gen[V]

Loop returns a generator to iterate the argument v infinitely.

func Lt

func Lt[V constraints.Ordered](xGen Gen[V], yGen Gen[V]) Gen[bool]

func Mod

func Mod[V constraints.Integer](xGen Gen[V], yGen Gen[V]) Gen[V]

func Mul

func Mul[V Number](xGen Gen[V], yGen Gen[V]) Gen[V]

func Neq

func Neq[V comparable](xGen Gen[V], yGen Gen[V]) Gen[bool]

func Not

func Not(gen Gen[bool]) Gen[bool]

func Or

func Or[V bool](xGen Gen[bool], yGen Gen[bool]) Gen[bool]

func Range

func Range[V constraints.Integer](start, stop, step V) Gen[V]

Range returns a generator of integer range.

func Sub

func Sub[V Number](xGen Gen[V], yGen Gen[V]) Gen[V]

func Where

func Where[V any](gen Gen[V], condGen Gen[bool]) Gen[V]

Where returns a generator that iterates values only when condGen is true.

type Number

type Number interface {
	constraints.Integer | constraints.Float
}

Jump to

Keyboard shortcuts

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