goterator

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2020 License: MIT Imports: 1 Imported by: 0

README

Goterator

Test and Build PkgGoDev GoDoc Go Report Coverage

Iterator implementation for Golang to provide map and reduce functionalities.

Package

import (
    "github.com/yaa110/goterator"
    "github.com/yaa110/goterator/generator"
)

Getting Started

  • Create a generator from slices
words := []interface{}{"an", "example", "of", "goterator"}

gen := generator.NewSlice(words)
  • Create a generator from channels
chn := make(chan interface{}, 4)
chn <- "an"
chn <- "example"
chn <- "of"
chn <- "goterator"
close(chn)

gen := generator.NewChannel(chn)
  • Create custom generators
type TestGenerator struct {
    words []string
    i     int
    value string
}

func (g *TestGenerator) Next() bool {
    if g.i == len(g.words) {
        return false
    }
    g.value = g.words[g.i]
    g.i++
    return true
}

func (g *TestGenerator) Value() interface{} {
    return g.value
}

gen := &TestGenerator{
    words: []string{"an", "example", "of", "goterator"},
    i: 0,
    value: "",
}
  • Iterate over generators
lengths := goterator.New(gen).Map(func(word interface{}) interface{} {
    return len(word.(string))
}).Collect()

assert.Equal([]interface{}{2, 7, 2, 9}, lengths)

Please for more information about mappers and reducers (consumers) check the documentation.

Documentation

Overview

Package goterator provides map and reduce functionality of iterators

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IterFunc

type IterFunc = func(element interface{})

IterFunc gets an element.

type Iterator

type Iterator struct {
	// contains filtered or unexported fields
}

Iterator represents an iterator object to apply map and reduce functions.

func New

func New(gen generator.Generator) *Iterator

New creates a new instance of `Iterator`.

func (*Iterator) All

func (iter *Iterator) All(f PredicateFunc) bool

All consumes elements and returns true if `f` returns true for all elements. Returns `true` for empty iterators.

func (*Iterator) Any

func (iter *Iterator) Any(f PredicateFunc) bool

Any consumes elements and returns true if `f` returns true for at least one element. Returns `false` for empty iterators.

func (*Iterator) Collect

func (iter *Iterator) Collect() []interface{}

Collect consumes elements and returns a slice of converted elements.

func (*Iterator) Count

func (iter *Iterator) Count() int

Count consumes elements and returns the length of elements.

func (*Iterator) Filter

func (iter *Iterator) Filter(f PredicateFunc) *Iterator

Filter lazily yields element if `f` returns true.

func (*Iterator) Find

func (iter *Iterator) Find(f PredicateFunc) (interface{}, bool)

Find consumes elements and returns the first element that satisfies `f` (returning `true`). Returns `nil, false` if no element is found.

func (*Iterator) ForEach

func (iter *Iterator) ForEach(f IterFunc)

ForEach consumes elements and runs `f` for each element. The iteration is broken if `f` returns false.

func (*Iterator) Last

func (iter *Iterator) Last() interface{}

Last consumes elements and returns the last element.

func (*Iterator) Map

func (iter *Iterator) Map(f MapFunc) *Iterator

Map lazily applies `f` on each element and returns iterator.

func (*Iterator) Max

func (iter *Iterator) Max(f LessFunc) interface{}

Max consumes elements and returns the maximum element.

func (*Iterator) Min

func (iter *Iterator) Min(f LessFunc) interface{}

Min consumes elements and returns the minimum element.

func (*Iterator) Nth

func (iter *Iterator) Nth(n int) (interface{}, bool)

Nth consumes elements and returns the `n`th element. Indexing starts from `0`. Returns an nil, false` if the length of iterator is less than `n`.

func (*Iterator) Reduce

func (iter *Iterator) Reduce(initialState interface{}, f ReduceFunc) interface{}

Reduce consumes elements and runs `f` for each element. Returns the final state after iteration over all elements.

func (*Iterator) Skip

func (iter *Iterator) Skip(n int) *Iterator

Skip lazily skips `n` elements.

func (*Iterator) SkipWhile

func (iter *Iterator) SkipWhile(f PredicateFunc) *Iterator

SkipWhile lazily skips element while `f` returns true. After returning `false` from `f`, this mapper is over.

func (*Iterator) Take

func (iter *Iterator) Take(n int) *Iterator

Take lazily yields `n` elements.

func (*Iterator) TakeWhile

func (iter *Iterator) TakeWhile(f PredicateFunc) *Iterator

TakeWhile lazily yields element while `f` returns true.

type LessFunc

type LessFunc = func(first, second interface{}) bool

LessFunc returns true if first < second

type MapFunc

type MapFunc = func(element interface{}) interface{}

MapFunc get an element and returns a converted element.

type PredicateFunc

type PredicateFunc = func(element interface{}) bool

PredicateFunc gets an element and returns true if the element should be yielded.

type ReduceFunc

type ReduceFunc = func(state, element interface{}) interface{}

ReduceFunc gets a state and an element, then returns the state.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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