iter

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

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

Go to latest
Published: Jun 24, 2024 License: BSD-3-Clause Imports: 7 Imported by: 2

README

Iter: simple golang iterator using generics

example1: simple iteration

s := strings.Split("동해물과 백두산이 마르고 닳도록", " ")
fmt.Printf("%v\n", s)

it := iter.Of(s...)
for v, ok := it.Next(); ok; v, ok = it.Next() {
    fmt.Printf("%s\n", v)
}

output:

[동해물과 백두산이 마르고 닳도록]
동해물과
백두산이
마르고
닳도록

example 2: filter and to slice

s := []int{1, 2, 3, 4, 5, 6}
fmt.Printf("%v\n", s)

it := iter.Of(s...)
r := it.
    Filter(iter.Even[int]). // 2,4,6
    Slice()                 // [2, 4, 6]

fmt.Printf("%v\n", r)

output:

[1 2 3 4 5 6]
[2 4 6]

example 3: filter, map and reduce

s := []int{1, 2, 3, 4, 5, 6}
fmt.Printf("%v\n", s)

it := iter.Of(s...)
r := it.
    Filter(iter.Even[int]). // 2,4,6
    Map(iter.Multiply(2)).  // 4, 8, 12
    Reduce(iter.Add[int])   // 4 + 8 + 12

fmt.Printf("%v\n", r)

output:

[1 2 3 4 5 6]
24

chan iteration

s := []int{1, 2, 3, 4, 5, 6}
fmt.Printf("%v\n", s)

ch := make(chan int)
go func() {
    defer close(ch)
    for _, e := range s {
        ch <- e
    }
}()

it := iter.C(ch)
r := make([]int, 0)
for v, ok := it.Next(); ok; v, ok = it.Next() {
    r = append(r, v)
}

fmt.Printf("%v\n", r)

output

[1 2 3 4 5 6]
[1 2 3 4 5 6]

benchmark

goos: darwin
goarch: amd64
pkg: github.com/whitekid/iter
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkWordCount/single-16                 1 1079390286 ns/op  8811840 B/op      235 allocs/op
BenchmarkWordCount/goroutine-16              7  151540820 ns/op 19696067 B/op    32406 allocs/op
BenchmarkWordCount/iterator-16               7  153911633 ns/op 19717408 B/op    32477 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add[T Number | string](a, b T) T

Sample reducer functions

func Asending

func Asending[T constraints.Integer | constraints.Float](a, b T) int

func Descending

func Descending[T constraints.Integer | constraints.Float](a, b T) int

func Even

func Even[T constraints.Integer](x T) bool

Sample filter functions

func Multiply

func Multiply[T Number](factor T) func(x T) T

func Odd

func Odd[T constraints.Integer](x T) bool

func StrToInt

func StrToInt(s string) (v int)

Sample map functions

func Sum

func Sum[T Number | string](it Iterator[T]) T

Types

type Item

type Item[K comparable, V any] struct {
	Key   K
	Value V
}

type Iterator

type Iterator[T any] interface {
	Next() (T, bool)

	Filter(func(T) bool) Iterator[T]
	Map(func(T) T) Iterator[T]
	// TODO Map(func(T, T) T) Iterator[T2] go1.19 does not supports yet
	TakeWhile(func(T) bool) Iterator[T]
	DropWhile(func(T) bool) Iterator[T]
	Skip(n int) Iterator[T]

	Reduce(func(T, T) T) T
	Slice() []T
	Each(func(T))
	EachIdx(func(int, T))
}

NOTE Next()와 Value()가 thread safe하지 않음..

func C

func C[T any](ch <-chan T) Iterator[T]

func Chunk

func Chunk[T any](it Iterator[T], size int) Iterator[[]T]

func Concat

func Concat[T any](it ...Iterator[T]) Iterator[T]

func Map

func Map[T1, T2 any](it Iterator[T1], mapper func(T1) T2) Iterator[T2]

Map map mapper using goroutine

func Of

func Of[T any](s ...T) Iterator[T]

func Reverse

func Reverse[T any](it Iterator[T]) Iterator[T]

func S

func S[S ~[]T, T any](s S) Iterator[T]

func Sorted

func Sorted[T constraints.Ordered](it Iterator[T]) Iterator[T]

func SortedFunc

func SortedFunc[T any](it Iterator[T], less Less[T]) Iterator[T]

type Less

type Less[T any] func(T, T) int

type MapIterator

type MapIterator[K comparable, V any] interface {
	Keys() Iterator[K]
	Values() Iterator[V]
	Items() Iterator[Item[K, V]]

	Each(func(K, V))
}

func M

func M[K comparable, V any](m map[K]V) MapIterator[K, V]

type Number

type Number interface {
	constraints.Integer | constraints.Float | constraints.Complex
}

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

func (*Queue[T]) Close

func (q *Queue[T]) Close()

func (*Queue[T]) Pop

func (q *Queue[T]) Pop() (T, bool)

func (*Queue[T]) Push

func (q *Queue[T]) Push(v T) T

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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