- Notifications
You must be signed in to change notification settings - Fork11
Go Stream, like Java 8 Stream.
License
NotificationsYou must be signed in to change notification settings
youthlin/stream
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Go Stream, like Java 8 Stream.
Blog Post:https://youthlin.com/?p=1755
go get github.com/youthlin/stream
国内镜像:https://gitee.com/youthlin/stream
在go.mod
中引入模块路径github.com/youthlin/stream
及版本后,
再添加 replace 即可:
// go.modrequiregithub.com/youthlin/streamlatestreplacegithub.com/youthlin/streamlatest=>gitee.com/youthlin/streamlatest
https://play.golang.org/p/nPQJYqA3-Jr
package mainimport ("fmt""github.com/youthlin/stream""github.com/youthlin/stream/types")funcmain() {m:=stream.IntRange(0,10).Filter(func(e types.T)bool {returne.(int)%2==0}).Map(func(e types.T) types.R {returne.(int)*2}).ReduceWith(map[int]string{},func(acc types.R,e types.T) types.R {m:=acc.(map[int]string)m[e.(int)]=fmt.Sprintf("<%d>",e)returnm})fmt.Println(m)// Output:// map[0:<0> 4:<4> 8:<8> 12:<12> 16:<16>]}
typeStreaminterface {// stateless operate 无状态操作Filter(types.Predicate)Stream// 过滤Map(types.Function)Stream// 转换FlatMap(func(t types.T)Stream)Stream// 打平Peek(types.Consumer)Stream// peek 每个元素// stateful operate 有状态操作Distinct(types.IntFunction)Stream// 去重Sorted(types.Comparator)Stream// 排序Limit(int64)Stream// 限制个数Skip(int64)Stream// 跳过个数// terminal operate 终止操作// 遍历ForEach(types.Consumer)// return []T 转为切片ToSlice() []types.T// return []X which X is the type of someToElementSlice(some types.T) types.R// return []X which X is same as the `typ` representationToSliceOf(typ reflect.Type) types.R// 测试是否所有元素满足条件AllMatch(types.Predicate)bool// 测试是否没有元素满足条件NoneMatch(types.Predicate)bool// 测试是否有任意元素满足条件AnyMatch(types.Predicate)bool// Reduce return optional.Empty if no element. calculate result by (T, T) -> T from first elementReduce(accumulator types.BinaryOperator) optional.Optional// type of initValue is same as element. (T, T) -> TReduceFrom(initValue types.T,accumulator types.BinaryOperator) types.T// type of initValue is different from element. (R, T) -> RReduceWith(initValue types.R,accumulatorfunc(types.R, types.T) types.R) types.RFindFirst() optional.Optional// 返回元素个数Count()int64}funcExampleOf() {fmt.Println(stream.Of().Count())fmt.Println(stream.Of(1).Count())fmt.Println(stream.Of("a","b").Count())vars= []int{1,2,3,4}stream.Of(stream.Slice(s)...).ForEach(func(t types.T) {fmt.Printf("%d,",t)})// Output:// 0// 1// 2// 1,2,3,4,}funcExampleOfSlice() {varintArr= []int{1,2,3,4}stream.OfSlice(intArr).ForEach(func(e types.T) {fmt.Printf("%d,",e)})varnilArr []intstream.OfSlice(nilArr).ForEach(func(e types.T) {fmt.Printf("should not print")})varstrArr= []string{"a","b"}stream.OfSlice(strArr).Map(func(e types.T) types.R {returnfmt.Sprintf("<%s>",e)}).ForEach(func(e types.T) {fmt.Printf("%s,",e)})// Output:// 1,2,3,4,<a>,<b>,}funcExampleOfMap() {varm1=map[int]string{3:"c",2:"b",1:"a",}s:=stream.OfMap(m1).Map(func(e types.T) types.R {p:=e.(types.Pair)p.First,p.Second=p.Second,p.Firstreturnp}).Sorted(func(left types.T,right types.T)int {p1:=left.(types.Pair)p2:=right.(types.Pair)returnp1.Second.(int)-p2.Second.(int)}).ToSlice()fmt.Println(s)stream.OfMap(nil).ForEach(func(e types.T) {fmt.Println("not print")})// Output:// [{a 1} {b 2} {c 3}]}funcExampleStream_Filter() {stream.Of(0,1,2,3,4,5,6,7,8,9).Filter(func(e types.T)bool {returne.(int)%3==0}).ForEach(func(e types.T) {fmt.Println(e)})// Output:// 0// 3// 6// 9}funcExampleStream_Map() {stream.IntRange(0,5).Map(func(t types.T) types.R {returnfmt.Sprintf("<%d>",t.(int))}).ForEach(func(t types.T) {fmt.Printf("%v",t)})// Output:// <0><1><2><3><4>}funcExampleStream_FlatMap() {stream.Of([]int{0,2,4,6,8}, []int{1,3,5,7,9}).FlatMap(func(t types.T) stream.Stream {returnstream.Of(stream.Slice(t)...)}).ForEach(func(t types.T) {fmt.Printf("%d",t)})// Output:// 0246813579}funcExampleStream_Sorted() {stream.IntRange(1,10).Sorted(types.ReverseOrder(types.IntComparator)).ForEach(func(t types.T) {fmt.Printf("%d,",t)})// Output:// 9,8,7,6,5,4,3,2,1,}funcTestToMap(t*testing.T) {m:=stream.IntRange(0,10).ReduceWith(make(map[int]int),func(acc types.R,t types.T) types.R {acc.(map[int]int)[t.(int)]=t.(int)*10returnacc})t.Log(m)// Output:// map[0:0 1:10 2:20 3:30 4:40 5:50 6:60 7:70 8:80 9:90]}
- v0.0.3 2020-12-08 add factory method: OfInts, OfInt64s, OfFloat32s, OfFloat64s, OfStrings;
add Stream method: ReduceBy - v0.0.2 2020-12-07 add factory method: OfSlice, OfMap
- v0.0.1 2020-11-12 first version
- add Benchmark test
- support parallel stream
About
Go Stream, like Java 8 Stream.
Topics
Resources
License
Stars
Watchers
Forks
Packages0
No packages published