erroriter

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package erroriter provides ErrorIterator type and methods to work with iterations possibly containing errors.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrIterationStop = errors.New("iteration stop")

ErrIterationStop indicates that iteration is over.

Functions

This section is empty.

Types

type ErrorIterator

type ErrorIterator[T any] struct {
	*itertools.Iterator[itertools.Pair[T, error]]
}

ErrorIterator is an iterator yielding itertools.Pair of value of type T and error.

func Map

func Map[T, U any](i *itertools.Iterator[T], mapper func(T) (U, error)) *ErrorIterator[U]

Map creates new ErrorIterator which contains elements of type U produced by applying mapper to elements of source iterator.

Example
package main

import (
	"fmt"
	"github.com/KSpaceer/itertools"
	"github.com/KSpaceer/itertools/erroriter"
	"strconv"
)

func main() {
	data := []string{"1", "wdqe", "3", "4", "qwcqwqdq"}

	iter := erroriter.Map(
		itertools.NewSliceIterator(data),
		strconv.Atoi,
	)

	for iter.Next() {
		v, err := iter.Result()
		if err != nil {
			fmt.Println("got error")
		} else {
			fmt.Println(v)
		}
	}

}
Output:

1
got error
3
4
got error

func New

func New[T any](f func() (T, error)) *ErrorIterator[T]

New creates ErrorIterator that yields elements using function f. Iterator yields elements until returned error is ErrIterationStop.

func (*ErrorIterator[T]) CollectUntilError

func (i *ErrorIterator[T]) CollectUntilError() ([]T, error)

CollectUntilError collects actual values into slice. CollectUntilError returns this slice or first encountered error.

Example
package main

import (
	"fmt"
	"github.com/KSpaceer/itertools/erroriter"
	"strconv"
)

func main() {
	data := []int{1, 2, 3, 4, 5}
	var idx int

	iter := erroriter.New(func() (int, error) {
		if idx >= len(data) {
			return 0, erroriter.ErrIterationStop
		}
		v := data[idx]
		idx++
		return v, nil
	})

	result, err := iter.CollectUntilError()
	if err != nil {
		fmt.Println("got error")
	} else {
		fmt.Println(result)
	}

	sdata := []string{"1", "2", "adqweqw", "4", "5"}
	idx = 0

	iter = erroriter.New(func() (int, error) {
		if idx >= len(sdata) {
			return 0, erroriter.ErrIterationStop
		}
		s := sdata[idx]
		idx++
		return strconv.Atoi(s)
	})

	result, err = iter.CollectUntilError()
	if err != nil {
		fmt.Println("got error")
	} else {
		fmt.Println(result)
	}
}
Output:

[1 2 3 4 5]
got error

func (*ErrorIterator[T]) Result

func (i *ErrorIterator[T]) Result() (T, error)

Result unpacks itertools.Pair element, returning value and error.

Example
package main

import (
	"fmt"
	"github.com/KSpaceer/itertools/erroriter"
	"strconv"
)

func main() {
	iter := erroriter.New(func() (int, error) {
		return strconv.Atoi("16")
	})

	iter.Next()
	result, err := iter.Result()
	if err != nil {
		fmt.Println("got error")
	} else {
		fmt.Println(result)
	}

	iter = erroriter.New(func() (int, error) {
		return strconv.Atoi("xadqwe")
	})

	iter.Next()
	result, err = iter.Result()
	if err != nil {
		fmt.Println("got error")
	} else {
		fmt.Println(result)
	}
}
Output:

16
got error

Jump to

Keyboard shortcuts

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