Categorygithub.com/calvernaz/go-iterators
modulepackage
0.0.0-20191014204505-d1c08542fac3
Repository: https://github.com/calvernaz/go-iterators.git
Documentation: pkg.go.dev

# 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

# Packages

No description provided by the author

# Functions

Appends multiple iterators together exposing them as a single virtual iterator.
Dedup eliminates duplicates.
Creates a wrapper-iterator over the original that will filter elements according to the filter function specified.
Specific case of Filter that returns a wrapper-iterator over the original that will return only the non nil items.
Creates an wrapper-iterator over the original that will iterate until there are no more items or the 'upperBound' is reached.
Merge combines multiple sorted iterators into a single sorted iterator.
Given a way to compute next and a close handler, return a closeable iterator.
Given a way to compute next, returns an iterator.
Creates an wrapper-iterator over the original that will skip the first 'numberOfElementsToSkip' items.
Creates an wrapper-iterator over the original that will transform elements according to the filter function specified.

# Constants

We have reached the end of the data and are finished.
We've suffered an error, kaput !!.
We haven't yet computed or have already returned the element.
We have computed the next element and haven't returned it yet.

# Structs

No description provided by the author

# Interfaces

An iterator over a stream of data.

# Type aliases

No description provided by the author
No description provided by the author
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.
EqualsFunc returns true is items are equal.
No description provided by the author
No description provided by the author
No description provided by the author