itr

package module
v0.0.0-...-68900ef Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

README

itr: Go iterator utils

Documentation

Overview

Example
package main

import (
	"fmt"
	"iter"

	"github.com/Warashi/itr"
)

func main() {
	var (
		primes  iter.Seq[int]
		isPrime func(int) bool
		cleanup func()
	)

	isPrime = func(n int) bool {
		for p := range primes {
			if p*p > n {
				return true
			}
			if n%p == 0 {
				return false
			}
		}
		return true
	}

	primes, cleanup = itr.Cache(func(yield func(int) bool) {
		if !yield(2) {
			return
		}
		if !yield(3) {
			return
		}
		for i := 5; ; i += 2 {
			if isPrime(i) {
				if !yield(i) {
					return
				}
			}
		}
	})

	defer cleanup()

	for i := range itr.Take(10, primes) {
		fmt.Println(i)
	}

}
Output:

2
3
5
7
11
13
17
19
23
29

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cache

func Cache[T any](it iter.Seq[T]) (_ iter.Seq[T], cleanup func())

Cache caches the elements of the iterator.

Example
package main

import (
	"fmt"
	"iter"

	"github.com/Warashi/itr"
)

func main() {
	it := func() iter.Seq[int] {
		return func(yield func(int) bool) {
			for i := range 3 {
				fmt.Println("yield", i)
				if !yield(i) {
					return
				}
			}
		}
	}

	cached, stop := itr.Cache(it())
	defer stop()

	for i := range cached {
		fmt.Println("consume", i)
	}
	for i := range cached {
		fmt.Println("consume", i)
	}

}
Output:

yield 0
consume 0
yield 1
consume 1
yield 2
consume 2
consume 0
consume 1
consume 2

func Skip

func Skip[T any](n int, it iter.Seq[T]) iter.Seq[T]

Skip skips the first n elements.

Example
package main

import (
	"fmt"

	"github.com/Warashi/itr"
)

func main() {
	it := func(yield func(int) bool) {
		for i := range 10 {
			if !yield(i) {
				return
			}
			fmt.Println("yield", i)
		}
	}

	skip := itr.Skip(3, it)
	for i := range skip {
		fmt.Println("consume", i)
	}

}
Output:

yield 0
yield 1
yield 2
consume 3
yield 3
consume 4
yield 4
consume 5
yield 5
consume 6
yield 6
consume 7
yield 7
consume 8
yield 8
consume 9
yield 9

func SkipWhile

func SkipWhile[T any](pred func(T) bool, it iter.Seq[T]) iter.Seq[T]

SkipWhile skips elements while the predicate is true.

Example
package main

import (
	"fmt"

	"github.com/Warashi/itr"
)

func main() {
	it := func(yield func(int) bool) {
		for i := range 10 {
			if !yield(i) {
				return
			}
			fmt.Println("yield", i)
		}
	}

	skip := itr.SkipWhile(func(i int) bool { return i < 3 }, it)
	for i := range skip {
		fmt.Println("consume", i)
	}

}
Output:

yield 0
yield 1
yield 2
consume 3
yield 3
consume 4
yield 4
consume 5
yield 5
consume 6
yield 6
consume 7
yield 7
consume 8
yield 8
consume 9
yield 9

func Take

func Take[T any](n int, it iter.Seq[T]) iter.Seq[T]

Take takes the first n elements.

Example
package main

import (
	"fmt"

	"github.com/Warashi/itr"
)

func main() {
	it := func(yield func(int) bool) {
		for i := range 10 {
			if !yield(i) {
				return
			}
			fmt.Println("yield", i)
		}
	}

	take := itr.Take(3, it)
	for i := range take {
		fmt.Println("consume", i)
	}

}
Output:

consume 0
yield 0
consume 1
yield 1
consume 2
yield 2

func TakeWhile

func TakeWhile[T any](pred func(T) bool, it iter.Seq[T]) iter.Seq[T]

TakeWhile takes elements while the predicate is true.

Example
package main

import (
	"fmt"

	"github.com/Warashi/itr"
)

func main() {
	it := func(yield func(int) bool) {
		for i := range 10 {
			if !yield(i) {
				return
			}
			fmt.Println("yield", i)
		}
	}

	take := itr.TakeWhile(func(i int) bool { return i < 3 }, it)
	for i := range take {
		fmt.Println("consume", i)
	}

}
Output:

consume 0
yield 0
consume 1
yield 1
consume 2
yield 2

Types

This section is empty.

Jump to

Keyboard shortcuts

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