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 ¶
- func Cache[T any](it iter.Seq[T]) (_ iter.Seq[T], cleanup func())
- func Skip[T any](n int, it iter.Seq[T]) iter.Seq[T]
- func SkipWhile[T any](pred func(T) bool, it iter.Seq[T]) iter.Seq[T]
- func Take[T any](n int, it iter.Seq[T]) iter.Seq[T]
- func TakeWhile[T any](pred func(T) bool, it iter.Seq[T]) iter.Seq[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cache ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
Click to show internal directories.
Click to hide internal directories.