iter

package
v0.0.0-...-3a06bfb Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrEmpty   = errors.New("iterator has length == 0")
	ErrTooMany = errors.New("iterator has length >= 2")
)
View Source
var Stop = error(stop{})

Functions

func Chain

func Chain[T any](its ...Nexter[T]) *chainIt[T]

func FilterMap

func FilterMap[T, U any, It Nexter[T]](it It, mapFunc mapFunc[T, U]) *mappedIt[T, U, It]

func FlatFilterMap

func FlatFilterMap[T, U any, It1 Nexter[T], It2 Nexter[It1]](it2 It2, mapFunc mapFunc[T, U]) *flatMappedIt[T, U, It1, It2]
Example
package main

import (
	"fmt"

	"github.com/hsfzxjy/imbed/util/iter"
	"github.com/hsfzxjy/tipe"
)

func Collect[T any](it iter.Ator[T]) []T {
	var res []T
	for it.HasNext() {
		res = append(res, it.Next().Unwrap())
	}
	return res
}

func main() {
	its := []iter.Nexter[int]{
		iter.Slice(1, 2, 3),
		iter.Slice(4, 5, 6),
		iter.Slice[int](),
	}
	flatten := iter.FlatFilterMap(iter.Slice(its...), tipe.Ok[int])
	fmt.Printf("%v\n", Collect(flatten))
	chain := iter.Chain(iter.Slice(1, 2, 3), iter.Slice(4, 5, 6))
	fmt.Printf("%v\n", Collect(chain))
}
Output:

[1 2 3 4 5 6]
[1 2 3 4 5 6]

func One

func One[T any, It Nexter[T]](it It) tipe.Result[T]

func Slice

func Slice[T any](data ...T) *sliceIt[T]

func Sorted

func Sorted[T any, It Nexter[T]](
	it It,
	cmpFunc cmpFunc[T],
	reversed bool,
) *sortedIt[T, It]
Example
package main

import (
	"cmp"
	"fmt"

	"github.com/hsfzxjy/imbed/util/iter"
)

func Collect[T any](it iter.Ator[T]) []T {
	var res []T
	for it.HasNext() {
		res = append(res, it.Next().Unwrap())
	}
	return res
}

func main() {
	it := iter.Sorted(iter.Slice(2, 4, 6, 5, 3, 1), cmp.Compare[int], false)
	fmt.Printf("%v\n", Collect(it))
}
Output:

[1 2 3 4 5 6]

func SortedKeyFunc

func SortedKeyFunc[T any, It Nexter[T], K cmp.Ordered](
	it It,
	keyFunc func(T) K,
	reversed bool,
) *sortedIt[T, It]

func Stopped

func Stopped[T any](r tipe.Result[T]) bool

Types

type Ator

type Ator[T any] interface {
	HasNext() bool
	Nexter[T]
}

type Nexter

type Nexter[T any] interface {
	Next() tipe.Result[T]
}

func NewFunc

func NewFunc[T any](f func() tipe.Result[T]) Nexter[T]

Jump to

Keyboard shortcuts

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