{"id":13413840,"url":"https://github.com/youthlin/stream","last_synced_at":"2026-01-18T12:26:21.556Z","repository":{"id":55907864,"uuid":"312161631","full_name":"youthlin/stream","owner":"youthlin","description":"Go Stream, like Java 8 Stream.","archived":false,"fork":false,"pushed_at":"2024-02-08T10:00:38.000Z","size":30,"stargazers_count":88,"open_issues_count":1,"forks_count":11,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-07-31T20:52:54.806Z","etag":null,"topics":["filter","flatmap","foreach","functional","functional-programming","go-stream","map","reduce"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/youthlin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-12T03:52:50.000Z","updated_at":"2024-07-19T06:25:55.000Z","dependencies_parsed_at":"2024-06-18T18:37:37.217Z","dependency_job_id":null,"html_url":"https://github.com/youthlin/stream","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/youthlin/stream","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/youthlin%2Fstream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/youthlin%2Fstream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/youthlin%2Fstream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/youthlin%2Fstream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/youthlin","download_url":"https://codeload.github.com/youthlin/stream/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/youthlin%2Fstream/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28536001,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-18T10:13:46.436Z","status":"ssl_error","status_checked_at":"2026-01-18T10:13:11.045Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["filter","flatmap","foreach","functional","functional-programming","go-stream","map","reduce"],"created_at":"2024-07-30T20:01:50.897Z","updated_at":"2026-01-18T12:26:21.533Z","avatar_url":"https://github.com/youthlin.png","language":"Go","readme":"# Go Stream\n\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/youthlin/stream)](https://pkg.go.dev/github.com/youthlin/stream)\n[![Go Report Card](https://goreportcard.com/badge/github.com/youthlin/stream)](https://goreportcard.com/report/github.com/youthlin/stream)\n[![Build Status](https://travis-ci.org/youthlin/stream.svg?branch=main)](https://travis-ci.org/youthlin/stream)\n[![codecov](https://codecov.io/gh/youthlin/stream/branch/main/graph/badge.svg?token=1CqmLWbsYL)](https://codecov.io/gh/youthlin/stream)\n\nGo Stream, like Java 8 Stream.\n\nBlog Post: https://youthlin.com/?p=1755\n\n## How to get\n\n```shell script\ngo get github.com/youthlin/stream\n```\n\n国内镜像: https://gitee.com/youthlin/stream  \n在 `go.mod` 中引入模块路径 `github.com/youthlin/stream` 及版本后，  \n再添加 replace 即可：\n\n```go\n// go.mod\n\nrequire github.com/youthlin/stream latest\n\nreplace github.com/youthlin/stream latest =\u003e gitee.com/youthlin/stream latest\n\n```\n\n## Play online\n\nhttps://play.golang.org/p/nPQJYqA3-Jr\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/youthlin/stream\"\n\t\"github.com/youthlin/stream/types\"\n)\n\nfunc main() {\n\tm := stream.IntRange(0, 10).\n\t\tFilter(func(e types.T) bool {\n\t\t\treturn e.(int)%2 == 0\n\t\t}).\n\t\tMap(func(e types.T) types.R {\n\t\t\treturn e.(int) * 2\n\t\t}).\n\t\tReduceWith(map[int]string{}, func(acc types.R, e types.T) types.R {\n\t\t\tm := acc.(map[int]string)\n\t\t\tm[e.(int)] = fmt.Sprintf(\"\u003c%d\u003e\", e)\n\t\t\treturn m\n\t\t})\n\tfmt.Println(m)\n\t// Output:\n\t// map[0:\u003c0\u003e 4:\u003c4\u003e 8:\u003c8\u003e 12:\u003c12\u003e 16:\u003c16\u003e]\n}\n```\n\n## Examples\n\n```go\n\ntype Stream interface {\n\t// stateless operate 无状态操作\n\n\tFilter(types.Predicate) Stream         // 过滤\n\tMap(types.Function) Stream             // 转换\n\tFlatMap(func(t types.T) Stream) Stream // 打平\n\tPeek(types.Consumer) Stream            // peek 每个元素\n\n\t// stateful operate 有状态操作\n\n\tDistinct(types.IntFunction) Stream // 去重\n\tSorted(types.Comparator) Stream    // 排序\n\tLimit(int64) Stream                // 限制个数\n\tSkip(int64) Stream                 // 跳过个数\n\n\t// terminal operate 终止操作\n\n\t// 遍历\n\tForEach(types.Consumer)\n\t// return []T 转为切片\n\tToSlice() []types.T\n\t// return []X which X is the type of some\n\tToElementSlice(some types.T) types.R\n\t// return []X which X is same as the `typ` representation\n\tToSliceOf(typ reflect.Type) types.R\n\t// 测试是否所有元素满足条件\n\tAllMatch(types.Predicate) bool\n\t// 测试是否没有元素满足条件\n\tNoneMatch(types.Predicate) bool\n\t// 测试是否有任意元素满足条件\n\tAnyMatch(types.Predicate) bool\n\t// Reduce return optional.Empty if no element. calculate result by (T, T) -\u003e T from first element\n\tReduce(accumulator types.BinaryOperator) optional.Optional\n\t// type of initValue is same as element.  (T, T) -\u003e T\n\tReduceFrom(initValue types.T, accumulator types.BinaryOperator) types.T\n\t// type of initValue is different from element. (R, T) -\u003e R\n\tReduceWith(initValue types.R, accumulator func(types.R, types.T) types.R) types.R\n\tFindFirst() optional.Optional\n\t// 返回元素个数\n\tCount() int64\n}\n\n\nfunc ExampleOf() {\n\tfmt.Println(stream.Of().Count())\n\tfmt.Println(stream.Of(1).Count())\n\tfmt.Println(stream.Of(\"a\", \"b\").Count())\n\tvar s = []int{1, 2, 3, 4}\n\tstream.Of(stream.Slice(s)...).ForEach(func(t types.T) {\n\t\tfmt.Printf(\"%d,\", t)\n\t})\n\t// Output:\n\t// 0\n\t// 1\n\t// 2\n\t// 1,2,3,4,\n}\n\nfunc ExampleOfSlice() {\n\tvar intArr = []int{1, 2, 3, 4}\n\tstream.OfSlice(intArr).ForEach(func(e types.T) {\n\t\tfmt.Printf(\"%d,\", e)\n\t})\n\tvar nilArr []int\n\tstream.OfSlice(nilArr).ForEach(func(e types.T) {\n\t\tfmt.Printf(\"should not print\")\n\t})\n\tvar strArr = []string{\"a\", \"b\"}\n\tstream.OfSlice(strArr).\n\t\tMap(func(e types.T) types.R {\n\t\t\treturn fmt.Sprintf(\"\u003c%s\u003e\", e)\n\t\t}).\n\t\tForEach(func(e types.T) {\n\t\t\tfmt.Printf(\"%s,\", e)\n\t\t})\n\t// Output:\n\t// 1,2,3,4,\u003ca\u003e,\u003cb\u003e,\n}\n\nfunc ExampleOfMap() {\n\tvar m1 = map[int]string{\n\t\t3: \"c\",\n\t\t2: \"b\",\n\t\t1: \"a\",\n\t}\n\ts := stream.OfMap(m1).\n\t\tMap(func(e types.T) types.R {\n\t\t\tp := e.(types.Pair)\n\t\t\tp.First, p.Second = p.Second, p.First\n\t\t\treturn p\n\t\t}).\n\t\tSorted(func(left types.T, right types.T) int {\n\t\t\tp1 := left.(types.Pair)\n\t\t\tp2 := right.(types.Pair)\n\t\t\treturn p1.Second.(int) - p2.Second.(int)\n\t\t}).\n\t\tToSlice()\n\tfmt.Println(s)\n\tstream.OfMap(nil).ForEach(func(e types.T) {\n\t\tfmt.Println(\"not print\")\n\t})\n\t// Output:\n\t// [{a 1} {b 2} {c 3}]\n}\n\nfunc ExampleStream_Filter() {\n\tstream.Of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).\n\t\tFilter(func(e types.T) bool {\n\t\t\treturn e.(int)%3 == 0\n\t\t}).\n\t\tForEach(func(e types.T) {\n\t\t\tfmt.Println(e)\n\t\t})\n\t// Output:\n\t// 0\n\t// 3\n\t// 6\n\t// 9\n}\nfunc ExampleStream_Map() {\n\tstream.IntRange(0, 5).\n\t\tMap(func(t types.T) types.R {\n\t\t\treturn fmt.Sprintf(\"\u003c%d\u003e\", t.(int))\n\t\t}).\n\t\tForEach(func(t types.T) {\n\t\t\tfmt.Printf(\"%v\", t)\n\t\t})\n\t// Output:\n\t// \u003c0\u003e\u003c1\u003e\u003c2\u003e\u003c3\u003e\u003c4\u003e\n}\nfunc ExampleStream_FlatMap() {\n\tstream.Of([]int{0, 2, 4, 6, 8}, []int{1, 3, 5, 7, 9}).\n\t\tFlatMap(func(t types.T) stream.Stream {\n\t\t\treturn stream.Of(stream.Slice(t)...)\n\t\t}).\n\t\tForEach(func(t types.T) {\n\t\t\tfmt.Printf(\"%d\", t)\n\t\t})\n\t// Output:\n\t// 0246813579\n}\nfunc ExampleStream_Sorted() {\n\tstream.IntRange(1, 10).\n\t\tSorted(types.ReverseOrder(types.IntComparator)).\n\t\tForEach(func(t types.T) {\n\t\t\tfmt.Printf(\"%d,\", t)\n\t\t})\n\t// Output:\n\t// 9,8,7,6,5,4,3,2,1,\n}\nfunc TestToMap(t *testing.T) {\n\tm := stream.IntRange(0, 10).ReduceWith(make(map[int]int), func(acc types.R, t types.T) types.R {\n\t\tacc.(map[int]int)[t.(int)] = t.(int) * 10\n\t\treturn acc\n\t})\n\tt.Log(m)\n\t// Output:\n\t// map[0:0 1:10 2:20 3:30 4:40 5:50 6:60 7:70 8:80 9:90]\n}\n\n```\n\n## Change Log\n\n- v0.0.3 2020-12-08 add factory method: OfInts, OfInt64s, OfFloat32s, OfFloat64s, OfStrings;  \n  add Stream method: ReduceBy\n- v0.0.2 2020-12-07 add factory method: OfSlice, OfMap\n- v0.0.1 2020-11-12 first version\n\n## Todo\n\n- [ ] add Benchmark test\n- [ ] support parallel stream\n","funding_links":[],"categories":["Stream Processing","Relational Databases","流处理","服务端应用","Go"],"sub_categories":["HTTP Clients","HTTP客户端","查询语"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyouthlin%2Fstream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyouthlin%2Fstream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyouthlin%2Fstream/lists"}