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 ¶
- func ToMap[K comparable, V any](i Iterable[K, V]) map[K]V
- func ToSlice[V any](i Iterable[int, V]) []V
- type Generator
- type Iterable
- func FromMap[K comparable, V any](inner map[K]V) Iterable[K, V]
- func FromMapWithKeyList[K comparable, V any](inner map[K]V, keys []K) Iterable[K, V]
- func FromSlice[V any](inner []V) Iterable[int, V]
- func Map[K, NK comparable, V, NV any](i Iterable[K, V], fn func(K, V) (NK, NV)) Iterable[NK, NV]
- func NewGenerator[K comparable, V any](key K, value V, inner func(K, V) (K, V)) Iterable[K, V]
- func Of[V any](values ...V) Iterable[int, V]
- type Slice
- type Table
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.
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.
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 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.
type Slice ¶
type Slice[V any] struct { // contains filtered or unexported fields }
Slice is a iterable structure based on slice.
Click to show internal directories.
Click to hide internal directories.