iterator

package module
v0.0.0-...-d1c0854 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2019 License: MIT Imports: 2 Imported by: 0

README

Build Status Coverage Status

go-iterators

The go-iterators project is a library offering the iterator pattern for Golang.

Why iterators are useful?

  • They can be lazy and the data will be fetched just when needed.
  • They fit a lots of use cases. From a simple slice iteration to data transformation to tree traversals.

Usage examples

Examples

Create an iterator

Creating an iterator is as simple as defining a function, the function will have to compute the next item in the iteration.

iter := NewDefaultIterator(func() (next interface{}, eod bool, err error) { 
    // Here put the logic that is computing the next element.
    // 1. If there is a next element return: next, false, nil
    // 2. If an error occurs computing the next element return: nil, false, error
    // 3. If there is no next element return: nil, true, nil 
})


defer iter.Close()
Create an iterator from a slice
func FisIterator(fis []os.FileInfo) iterator.Iterator {
	i := 0
	return iterator.NewDefaultIterator(func() (interface{}, bool, error) {
		if i >= len(fis) {
			return nil, true, nil
		}
		
		file := &fis[i]
		i++
		return file, false, nil
	})
}

Credits

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Closer

type Closer func() error

type CompareFunc

type CompareFunc func(item1 interface{}, item2 interface{}) int

type ComputeNext

type ComputeNext func() (next interface{}, eod bool, err error)

If there is a next element return: next, false, nil If an error occurs computing the next element return: nil, false, error If there is no next element return: nil, true, nil

type DefaultIterator

type DefaultIterator struct {
	ComputeNext ComputeNext
	// contains filtered or unexported fields
}

func (*DefaultIterator) Close

func (it *DefaultIterator) Close() error

func (*DefaultIterator) HasNext

func (it *DefaultIterator) HasNext() bool

Returns true if the iterator can be continued or false if the end of data has been reached. It returns an error if the check fails.

func (*DefaultIterator) Next

func (it *DefaultIterator) Next() (interface{}, error)

Returns the next item in the iteration. This method should be always called in combination with the HasNext. If the iterator reached the end of data, the method will return an error

func (*DefaultIterator) Peek

func (it *DefaultIterator) Peek() (interface{}, error)

Returns the next element without continuing the iteration.

type EqualsFunc

type EqualsFunc func(item1 interface{}, item2 interface{}) bool

EqualsFunc returns true is items are equal

type Iterator

type Iterator interface {
	HasNext() bool
	Next() (interface{}, error)
	Peek() (item interface{}, e error)
	io.Closer
}

An iterator over a stream of data

func Concat

func Concat(iterators ...Iterator) Iterator

Appends multiple iterators together exposing them as a single virtual iterator.

func Dedup

func Dedup(it Iterator, equalsFn EqualsFunc) Iterator

Dedup eliminates duplicates

func Filter

func Filter(iter Iterator, test PredicateFunc) Iterator

Creates a wrapper-iterator over the original that will filter elements according to the filter function specified

func FilterNonNil

func FilterNonNil(it Iterator) Iterator

Specific case of Filter that returns a wrapper-iterator over the original that will return only the non nil items

func Limit

func Limit(it Iterator, upperBound int) Iterator

Creates an wrapper-iterator over the original that will iterate until there are no more items or the 'upperBound' is reached.

func Merge

func Merge(compareFn CompareFunc, iterators ...Iterator) Iterator

Merge combines multiple sorted iterators into a single sorted iterator.

func NewCloseableIterator

func NewCloseableIterator(computeNext ComputeNext, closer Closer) Iterator

Given a way to compute next and a close handler, return a closeable iterator

func NewDefaultIterator

func NewDefaultIterator(computeNext ComputeNext) Iterator

Given a way to compute next, returns an iterator

func Skip

func Skip(it Iterator, howMany int) Iterator

Creates an wrapper-iterator over the original that will skip the first 'numberOfElementsToSkip' items

func Transform

func Transform(iter Iterator, fn TransformFunc) Iterator

Creates an wrapper-iterator over the original that will transform elements according to the filter function specified

type PredicateFunc

type PredicateFunc func(item interface{}) (bool, error)

type State

type State int
const (
	// We haven't yet computed or have already returned the element
	NotReady State = iota
	// We have computed the next element and haven't returned it yet.
	Ready
	// We have reached the end of the data and are finished.
	Done
	// We've suffered an error, kaput !!.
	Failed
)

type TransformFunc

type TransformFunc func(item interface{}) (interface{}, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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