streams

Basic Stream API written in Go.(Only supports Go1.18+)

CircleCI codecov Go Report Card GoDoc

Functions

Filter

package main

import (
	"fmt"

	"github.com/go-the-way/streams"
)

func main() {
	fmt.Println(streams.Filter[int](func(e int) bool {
		return e%2 == 0
	}, 1, 2, 3, 4, 6, 7, 8, 9, 10))
}

Map

package main

import (
	"fmt"

	"github.com/go-the-way/streams"
)

func main() {
	fmt.Println(streams.Map[int, string](func(in int) string {
		return fmt.Sprintf("%x", in)
	}, 1, 2, 3, 4, 5))
}

Reduce

package main

import (
	"fmt"

	"github.com/go-the-way/streams"
)

type (
	intReduce struct{ int }
)

func main() {
	fmt.Println(streams.Reduce[int, *intReduce](
		func(e *intReduce, sum *int) { *sum += e.int },
		*new(int),
		&intReduce{10}, &intReduce{10}))
}

Skip

package main

import (
	"fmt"

	"github.com/go-the-way/streams"
)

func main() {
	fmt.Println(streams.Skip(1, 1, 2, 3, 4))
}

GroupBy

package main

import (
	"fmt"

	"github.com/go-the-way/streams"
)

func main() {
	type groupBy struct {
		id   int
		name string
	}
	fmt.Println(streams.GroupBy(func(e *groupBy) (string, *groupBy) {
		return e.name, e
	},
		&groupBy{10, "Apple"},
		&groupBy{10, "Banana"},
		&groupBy{30, "Banana"},
		&groupBy{10, "Pear"},
		&groupBy{20, "Pear"},
	))
}

ToMap

package main

import (
	"fmt"

	"github.com/go-the-way/streams"
)

func main() {
	type toMap struct {
		id   int
		name string
	}
	fmt.Println(streams.ToMap(func(e *toMap) (int, string) {
		return e.id, e.name
	}, &toMap{10, "Apple"}, &toMap{20, "Pear"}))
}

ToMap2

package main

import (
	"fmt"

	"github.com/go-the-way/streams"
)

func main() {
	type toMap struct {
		id   int
		name string
	}
	fmt.Println(streams.ToMap2(func(e *toMap) (int, string) {
		return e.id, e.name
	}, func(e *toMap) (string, int) {
		return e.name, e.id
	}, &toMap{10, "Apple"}, &toMap{20, "Pear"}))
}

GitHub

View Github