iterable

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

iterable is an interface that represents an iterable collection of elements.

Example (FromMap)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/iterable"
)

func main() {
	a := map[int]string{
		1: "one",
		2: "two",
		3: "three",
		4: "four",
		5: "five",
	}
	iter := iterable.FromMap(a)
	for iter.HasNext() {
		fmt.Println(iter.Next())
	}
}
Output:

1 one true
2 two true
3 three true
4 four true
5 five true
Example (FromSlice)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/iterable"
)

func main() {
	a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	iter := iterable.FromSlice(a)
	for iter.HasNext() {
		fmt.Println(iter.Next())
	}
}
Output:

0 1 true
1 2 true
2 3 true
3 4 true
4 5 true
5 6 true
6 7 true
7 8 true
8 9 true
9 10 true
Example (Generator)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/iterable"
)

func main() {
	generator := iterable.NewGenerator(0, 0, func(k int, v int) (int, int) {
		return k, v + 1
	})
	count := 0
	for generator.HasNext() && count < 10 {
		fmt.Println(generator.Next())
		count++
	}
}
Output:

0 1 true
0 2 true
0 3 true
0 4 true
0 5 true
0 6 true
0 7 true
0 8 true
0 9 true
0 10 true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToMap

func ToMap[K comparable, V any](i Iterable[K, V]) map[K]V

ToMap is a function that converts an iterable collection to a map.

func ToSlice

func ToSlice[V any](i Iterable[int, V]) []V

ToSlice is a function that converts an iterable collection to a slice.

Types

type Generator

type Generator[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Generator is a structure that generates elements from a given function.

func (*Generator[K, V]) HasNext

func (g *Generator[K, V]) HasNext() bool

HasNext returns true if the generator has next element.

func (*Generator[K, V]) Next

func (g *Generator[K, V]) Next() (key K, value V, exist bool)

Next returns the next elements in the generator.

func (*Generator[K, V]) Reset

func (g *Generator[K, V]) Reset()

Reset resets the generator.

type Iterable

type Iterable[K comparable, V any] interface {
	Next() (K, V, bool)
	HasNext() bool
	Reset()
}

iterable is an interface that represents an iterable collection of elements.

func FromMap

func FromMap[K comparable, V any](inner map[K]V) Iterable[K, V]

FromMap returns a new Table with given map.

func FromMapWithKeyList

func FromMapWithKeyList[K comparable, V any](inner map[K]V, keys []K) Iterable[K, V]

FromMapWithKeyList returns a new Table with given map and key list.

func FromSlice

func FromSlice[V any](inner []V) Iterable[int, V]

FromSlice returns a new Slice with given slice.

func Map

func Map[K, NK comparable, V, NV any](i Iterable[K, V], fn func(K, V) (NK, NV)) Iterable[NK, NV]

Map is a function that converts an iterable collection to new iterable collection with given function.

func NewGenerator

func NewGenerator[K comparable, V any](key K, value V, inner func(K, V) (K, V)) Iterable[K, V]

NewGenerator returns a new generator with given key and value.

func Of

func Of[V any](values ...V) Iterable[int, V]

Of returns a new Slice with given values.

type Slice

type Slice[V any] struct {
	// contains filtered or unexported fields
}

Slice is a iterable structure based on slice.

func (*Slice[V]) HasNext

func (s *Slice[V]) HasNext() bool

HasNext returns true if the Slice has next element.

func (*Slice[V]) Next

func (s *Slice[V]) Next() (key int, value V, exist bool)

Next returns the next element in the Slice.

func (*Slice[V]) Reset

func (s *Slice[V]) Reset()

Reset resets the index of Slice.

type Table

type Table[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Table is a iterable structure based on map.

func (*Table[K, V]) HasNext

func (m *Table[K, V]) HasNext() bool

HasNext returns true if the Table has next element.

func (*Table[K, V]) Next

func (m *Table[K, V]) Next() (key K, value V, exist bool)

Next returns the next element in the Table.

func (*Table[K, V]) Reset

func (m *Table[K, V]) Reset()

Reset resets the index of Table.

Directories

Path Synopsis
sequence
sequence

Jump to

Keyboard shortcuts

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