gostream

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2021 License: MIT Imports: 2 Imported by: 0

README

GoStream

GoStream是一个参考Java Stream API,基于go-linq实现的流式数据处理库。 它可以声明式地对数据进行转换、过滤、排序、分组、收集,而无需关心操作细节。

Get GoStream

go get github.com/a2dict/gostream

Example

See walkthrough.go

Base Example
package main

import (
	"fmt"
	"reflect"

	. "github.com/a2dict/gostream/core"
)

func main() {
	input := []int{4, 3, 2, 1}
	want := []int{6, 8}

	got := From(input).Map(func(it interface{}) interface{} {
		return 2 * it.(int)
	}).Filter(func(it interface{}) bool {
		return it.(int) > 5
	}).SortedBy(func(it interface{}) interface{} {
		return it
	}).Collect(ToSlice([]int(nil)))

	if !reflect.DeepEqual(got, want) {
		panic(fmt.Sprintf("%v != %v", got, want))
	}

	// walkthrough()
}
Map & FlatMap

Map和FlatMap的差别在于:FlatMap的mapper返回一个Stream,用于将多级数据打扁。

input := [][]int{{3, 2, 1}, {6, 5, 4}, {9, 8, 7}}
want := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
got := From(input).FlatMap(func(it interface{}) Stream {
   return From(it)
}).SortedBy(func(it interface{}) interface{} {
   return it
}).Collect(ToSlice([]int{}))
Collect ToSlice & ToMap

Collect将数据收集起来。受限于go的类型系统,需要显示传类型参数——一个目标类型的实例,可以为nil。

intput := []int{1, 2, 3, 4, 5}
identity := func(it interface{}) interface{} { return it }

// []int{1, 2, 3, 4, 5}
gotSlice := From(intput).Collect(ToSlice([]int{}))
// map[int]int{1: 1, 2: 2, 3: 3, 4: 4, 5: 5}
gotMap := From(intput).Collect(ToMap(map[int]int(nil), identity, identity))
Collect GroupBy

GroupBy定义一个分组收集器,参数依序分别为 类型参数、分类方法、下游收集器。 GroupBy可以和ToSlice、ToMap组合,GroupBy也可以多级嵌套,实现多级分组。

GroupBy(typ interface{}, classifier normalFn, downstream collector) collector

假设一组货物,需要按Status,Location进行分组,目标类型为 map[int]map[string][]*Cargo。

// Cargo 货物实体
type Cargo struct {
   ID       int
   Name     string
   Location string
   Status   int
}

input := []*Cargo{{
    ID:       1,
    Name:     "foo",
    Location: "shenzhen",
    Status:   1,
    }, {
    ID:       2,
    Name:     "bar",
    Location: "shenzhen",
    Status:   0,
    }, {
    ID:       3,
    Name:     "zhang",
    Location: "guangzhou",
    Status:   1,
}}

getStatus := func(it interface{}) interface{} { return it.(*Cargo).Status }
getLocation := func(it interface{}) interface{} { return it.(*Cargo).Location }

// result type: map[int]map[string][]*Cargo
got := From(input).Collect(
   GroupBy(map[int]map[string][]*Cargo(nil), getStatus,
      GroupBy(map[string][]*Cargo(nil), getLocation,
         ToSlice([]*Cargo(nil)))))
Flatten Group

这个示例演示如何将多级分组Map扁平化。map[int]map[string][]*Cargo => []*Cargo

From(cargoByLocationByTo).FlatMap(func(it interface{}) Stream {
   return From(it.(KeyValue).Value).FlatMap(func(it2 interface{}) Stream {
      return From(it2.(KeyValue).Value)
   })
}).Collect(ToSlice([]*Cargo{}))

Benchmark

$ go test -bench=.
goos: darwin
goarch: amd64
pkg: github.com/a2dict/gostream
BenchmarkToSliceRaw-12                      8216            132435 ns/op
BenchmarkToSliceStreamForeach-12             639           1819347 ns/op
BenchmarkCollectToSlice-12                    88          13006774 ns/op
BenchmarkCollectToSliceV2-12                 308           3847322 ns/op
BenchmarkLinqToSlice-12                      340           3513771 ns/op
BenchmarkToMapRaw-12                         184           6195945 ns/op
BenchmarkCollectToMap-12                      97          12378528 ns/op
BenchmarkCollectToMapV2-12                    98          12409819 ns/op
BenchmarkLinqToMap-12                        100          12293144 ns/op
BenchmarkGroupByRaw-12                        86          12673383 ns/op
BenchmarkGroupBy-12                           22          48737249 ns/op
BenchmarkGroupByV2-12                          9         116926833 ns/op
BenchmarkLinqGroupBy-12                       19          61038649 ns/op
BenchmarkPartition-12                        171           6949116 ns/op
BenchmarkCountRaw-12                         810           1468941 ns/op
BenchmarkCount-12                            170           7044074 ns/op
BenchmarkCountV2-12                          813           1468029 ns/op
BenchmarkGroupCount-12                        33          35121443 ns/op
BenchmarkGroupCountV2-12                      15          70305638 ns/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count

func Count() collector

Count 收集器,统计数量

func CountV2

func CountV2() collectorV2

CountV2 ...

func GroupBy

func GroupBy(typ interface{}, classifier normalizedFn, downstream collector) collector

GroupBy 分组收集器,将item分组收集。 参数说明:

classifier  分组函数
downstream  下游收集器

func GroupByV2

func GroupByV2(typ interface{}, classifier normalizedFn, downstream collectorV2) collectorV2

GroupByV2 ...

func ToMap

func ToMap(typ interface{}, keyMapper, valueMapper normalizedFn) collector

ToMap 收集器

func ToMapV2

func ToMapV2(typ interface{}, keyMapper, valueMapper normalizedFn) collectorV2

ToMapV2 ...

func ToSlice

func ToSlice(typ interface{}) collector

ToSlice 收集器,将item收集为slice。 typ为类型参数,允许为nil。 eg: []int{} or []int(nil)

func ToSliceBy

func ToSliceBy(typ interface{}, mapper normalizedFn) collector

ToSliceBy 收集器,将mapper应用于每一个item,再收集结果

func ToSliceByV2

func ToSliceByV2(typ interface{}, mapper normalizedFn) collectorV2

ToSliceByV2 ...

func ToSliceV2

func ToSliceV2(typ interface{}) collectorV2

ToSliceV2 ...

Types

type KeyValue

type KeyValue = linq.KeyValue

KeyValue ...

type Stream

type Stream linq.Query

Stream 流

func From

func From(source interface{}) Stream

From ...

func Zip2By

func Zip2By(left, right Stream, fn zip2Fn) Stream

Zip2By ...

func Zip3By

func Zip3By(first, second, third Stream, fn zip3Fn) Stream

Zip3By ...

func (Stream) All

func (s Stream) All(predicate func(interface{}) bool) bool

All 判断所有item满足条件,当stream为nil时默认返回true

func (Stream) Any

func (s Stream) Any() bool

Any 判断stream是否非空

func (Stream) AnyWith

func (s Stream) AnyWith(predicate func(interface{}) bool) bool

AnyWith 判断是否有item满足条件

func (Stream) Collect

func (s Stream) Collect(c collector) interface{}

Collect ...

func (Stream) CollectV2

func (s Stream) CollectV2(c collectorV2) interface{}

CollectV2 Collect的v2版本,基于go-linq实现

func (Stream) Contains

func (s Stream) Contains(value interface{}) bool

Contains 判断是否包含value。注意value必须为*值类型*

func (Stream) Count

func (s Stream) Count() int

Count 统计item数量

func (Stream) Drop

func (s Stream) Drop(n int) Stream

Drop 丢弃前n项

func (Stream) Filter

func (s Stream) Filter(predicate func(interface{}) bool) Stream

Filter ...

func (Stream) First

func (s Stream) First() (interface{}, bool)

First 返回第一个item,若stream为空,返回 (nil, false)

func (Stream) FlatMap

func (s Stream) FlatMap(mapper func(interface{}) Stream) Stream

FlatMap ...

func (Stream) ForEach

func (s Stream) ForEach(action func(interface{}))

ForEach 遍历item

func (Stream) ForEachIndexed

func (s Stream) ForEachIndexed(action func(int, interface{}))

ForEachIndexed 带index遍历item

func (Stream) Last

func (s Stream) Last() (interface{}, bool)

Last 返回最后一个item,若stream为空,返回(nil, false)

func (Stream) Limit

func (s Stream) Limit(n int) Stream

Limit 限制长多n项

func (Stream) Linq

func (s Stream) Linq() linq.Query

Linq 转换成go-linq Query

func (Stream) Map

func (s Stream) Map(mapper normalizedFn) Stream

Map ...

func (Stream) OutMap

func (s Stream) OutMap(out interface{})

OutMap 将item输出,out类型为 *map[K]V

func (Stream) OutSlice

func (s Stream) OutSlice(out interface{})

OutSlice 将item输出,out类型为*[]T

func (Stream) Partition

func (s Stream) Partition(typ interface{}, size int) Stream

Partition 将Stream按size分区

func (Stream) Peek

func (s Stream) Peek(fn func(interface{})) Stream

Peek 对经过的每一项item应用fn函数

func (Stream) Process

func (s Stream) Process(fn func(interface{}) error) error

Process 对每个item应用函数,如果其中一项返回err,中断并返回err

func (Stream) Reduce

func (s Stream) Reduce(accumulator accumulatorFn) interface{}

Reduce ...

func (Stream) ReduceWith

func (s Stream) ReduceWith(seed interface{}, accumulator accumulatorFn) interface{}

ReduceWith ...

func (Stream) SortedBy

func (s Stream) SortedBy(fn normalizedFn) Stream

SortedBy ...

func (Stream) SortedDescBy

func (s Stream) SortedDescBy(fn normalizedFn) Stream

SortedDescBy ...

func (Stream) ToMap

func (s Stream) ToMap(typ interface{}) interface{}

ToSlice 收集为Map, typ为类型参数。要求item类型为KeyValue

func (Stream) ToSlice

func (s Stream) ToSlice(typ interface{}) interface{}

ToSlice 收集为Slice, typ为类型参数

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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