{"id":18806995,"url":"https://github.com/tk103331/stream","last_synced_at":"2025-10-06T14:51:57.949Z","repository":{"id":58835907,"uuid":"108144215","full_name":"tk103331/stream","owner":"tk103331","description":"A Go language implementation of the Java Stream API.","archived":false,"fork":false,"pushed_at":"2024-06-05T14:31:00.000Z","size":82,"stargazers_count":38,"open_issues_count":0,"forks_count":12,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-09-09T16:16:23.806Z","etag":null,"topics":["go","golang","stream"],"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/tk103331.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":"2017-10-24T15:10:03.000Z","updated_at":"2024-12-04T09:40:07.000Z","dependencies_parsed_at":"2024-06-18T21:32:48.882Z","dependency_job_id":"884bbb43-0749-45f8-8f1e-afe5d28d4083","html_url":"https://github.com/tk103331/stream","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/tk103331/stream","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tk103331%2Fstream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tk103331%2Fstream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tk103331%2Fstream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tk103331%2Fstream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tk103331","download_url":"https://codeload.github.com/tk103331/stream/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tk103331%2Fstream/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278629037,"owners_count":26018480,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-06T02:00:05.630Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["go","golang","stream"],"created_at":"2024-11-07T22:50:28.190Z","updated_at":"2025-10-06T14:51:57.909Z","avatar_url":"https://github.com/tk103331.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# stream \r\n\r\n[![Travis CI](https://travis-ci.org/tk103331/stream.svg?branch=master)](https://app.travis-ci.com/github/tk103331/stream)\r\n[![Coverage Status](https://coveralls.io/repos/github/tk103331/stream/badge.svg?branch=master\u0026_t=0)](https://coveralls.io/github/tk103331/stream?branch=master)\r\n[![Go Report Card](https://goreportcard.com/badge/github.com/tk103331/stream?_t=0)](https://goreportcard.com/report/github.com/tk103331/stream)\r\n[![Go Reference](https://pkg.go.dev/badge/github.com/tk103331/stream.svg)](https://pkg.go.dev/github.com/tk103331/stream)\r\n![GitHub repo size](https://img.shields.io/github/repo-size/tk103331/stream)\r\n![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/tk103331/stream)\r\n![GitHub](https://img.shields.io/github/license/tk103331/stream)\r\n\r\nA Go language implementation of the Java Stream API.\r\n\r\n----------\r\n\r\n**Preparation**\r\n\r\n    type student struct {\r\n    \tid int\r\n    \tname   string\r\n    \tageint\r\n    \tscores []int\r\n    }\r\n    \r\n    func (s *student) String() string {\r\n    \treturn fmt.Sprintf(\"{id:%d, name:%s, age:%d,scores:%v}\", s.id, s.name, s.age, s.scores)\r\n    }\r\n    \r\n    func createStudents() []student {\r\n    \tnames := []string{\"Tom\", \"Kate\", \"Lucy\", \"Jim\", \"Jack\", \"King\", \"Lee\", \"Mask\"}\r\n    \tstudents := make([]student, 10)\r\n    \trnd := func(start, end int) int { return rand.Intn(end-start) + start }\r\n    \tfor i := 0; i \u003c 10; i++ {\r\n    \t\tstudents[i] = student{\r\n    \t\t\tid: i + 1,\r\n    \t\t\tname:   names[rand.Intn(len(names))],\r\n    \t\t\tage:rnd(15, 26),\r\n    \t\t\tscores: []int{rnd(60, 100), rnd(60, 100), rnd(60, 100)},\r\n    \t\t}\r\n    \t}\r\n    \treturn students\r\n    }\r\n    \r\n    type node struct {\r\n    \tid   int\r\n    \tnext *node\r\n    }\r\n    \r\n    func createNodes() *node {\r\n    \ti := 10\r\n    \tn := \u0026node{id: i}\r\n    \tfor i \u003e 0 {\r\n    \t\ti--\r\n    \t\tn = \u0026node{id: i, next: n}\r\n    \t}\r\n    \treturn n\r\n    }\r\n\r\n### ForEach ###\r\nForEach operation. actFunc: func(o T)\r\n\r\n    func (s *stream) ForEach(actFunc interface{})\r\n\r\nSample:\r\n\r\n\tstudents := createStudents()\r\n\tstream, _ := New(students)\r\n\r\n\tstream.ForEach(func(s student) {\r\n\t\tfmt.Printf(\"\\t%s\\n\", s.String())\r\n\t})\r\n\r\nOutput:\r\n\r\n\t{id:1, name:Kate, age:16,scores:[67 79 61]}\r\n\t{id:2, name:Lee, age:22,scores:[80 76 80]}\r\n\t{id:3, name:Lee, age:15,scores:[62 69 68]}\r\n\t{id:4, name:Lucy, age:22,scores:[65 97 86]}\r\n\t{id:5, name:Mask, age:15,scores:[68 78 67]}\r\n\t{id:6, name:Jim, age:20,scores:[68 90 75]}\r\n\t{id:7, name:King, age:22,scores:[87 91 89]}\r\n\t{id:8, name:Jack, age:16,scores:[91 65 86]}\r\n\t{id:9, name:King, age:21,scores:[94 63 93]}\r\n\t{id:10, name:Jim, age:20,scores:[64 99 93]}\r\n\r\n### Iterate ###\r\nIt create a stream from a iterator.itFunc: func(prev T) (next T,more bool)\r\n\r\n    func It(initValue interface{}, itFunc interface{}) (*stream, error)\r\n\r\nSample:\r\n\r\n\tstream, _ := It(root, func(n *node) (*node, bool) {\r\n\t\treturn n.next, n.next.next != nil\r\n\t})\r\n\tstream.ForEach(func(n *node) {\r\n\t\tfmt.Printf(\"\\tnode{id:%d}\\n\", n.id)\r\n\t})\r\n\r\nOutput:\r\n\r\n    node{id:1}\r\n    node{id:2}\r\n    node{id:3}\r\n    node{id:4}\r\n    node{id:5}\r\n    node{id:6}\r\n    node{id:7}\r\n    node{id:8}\r\n    node{id:9}\r\n    node{id:10}\r\n\r\n### Generate ###\r\nGen create a stream by invoke genFunc. genFunc: func() (next T,more bool)\r\n\r\n    func Gen(genFunc interface{}) (*stream, error)\r\n\r\nSapmle:\r\n\r\n\tstream, _ := Gen(func() (int, bool) {\r\n\t\tx := rand.Intn(10)\r\n\t\treturn x, x \u003c 8\r\n\t})\r\n\tstream.ForEach(func(x int) {\r\n\t\tfmt.Printf(\"\\t%d\\n\", x)\r\n\t})\r\n\r\nOutput:\r\n\r\n\t1\r\n\t7\r\n\t7\r\n\t9\r\n\r\n### Filter ###\r\nFilter operation. filterFunc: func(o T) bool\r\n\r\n    func (s *stream) Filter(filterFunc interface{}) *stream\r\n\r\nSample:\r\n\r\n\tstudents := createStudents()\r\n\tstream, _ := New(students)\r\n\r\n\tstream.Filter(func(s student) bool {\r\n\t\treturn s.age \u003e 20\r\n\t}).ForEach(func(s student) {\r\n\t\tfmt.Printf(\"\\t%s\\n\", s.String())\r\n\t})\r\n\r\nOutput:\r\n\r\n\t{id:2, name:Lee, age:22,scores:[80 76 80]}\r\n\t{id:4, name:Lucy, age:22,scores:[65 97 86]}\r\n\t{id:7, name:King, age:22,scores:[87 91 89]}\r\n\t{id:9, name:King, age:21,scores:[94 63 93]}\r\n\r\n### Map ###\r\nMap operation. Map one to one.mapFunc: func(o T1) T2\r\n\r\n    func (s *stream) Map(mapFunc interface{}) *stream\r\n\r\nSample:\r\n\r\n\tstudents := createStudents()\r\n\tstream, _ := New(students)\r\n\r\n\tstream.Map(func(s student) string {\r\n\t\treturn s.name\r\n\t}).ForEach(func(s string) {\r\n\t\tfmt.Printf(\"\\t%s\\n\", s)\r\n\t})\r\n\r\nOutput:\r\n\r\n\tKate\r\n\tLee\r\n\tLee\r\n\tLucy\r\n\tMask\r\n\tJim\r\n\tKing\r\n\tJack\r\n\tKing\r\n\tJim\r\n\r\n### FlatMap ###\r\n\r\nFlatMap operation. Map one to many.mapFunc: func(o T1) []T2\r\n\r\n    func (s *stream) FlatMap(mapFunc interface{}) *stream\r\n\r\nSample:\r\n\r\n\tstudents := createStudents()\r\n\tstream, _ := New(students)\r\n\tvar data []int\r\n\tstream.FlatMap(func(s student) []int {\r\n\t\treturn s.scores\r\n\t}).ToSlice(\u0026data)\r\n\tfmt.Printf(\"\\t%v\\n\", data)\r\n\r\nOutput:\r\n\r\n    [67 79 61 80 76 80 62 69 68 65 97 86 68 78 67 68 90 75 87 91 89 91 65 86 94 63 93 64 99 93]\r\n\r\n### Sort ###\r\nSort operation. lessFunc: func(o1,o2 T) bool\r\n\r\n    func (s *stream) Sort(lessFunc interface{}) *stream\r\n\r\nSample:\r\n\r\n\tstudents := createStudents()\r\n\tstream, _ := New(students)\r\n\r\n\tstream.Sort(func(s1, s2 student) bool {\r\n\t\treturn s1.scores[0]+s1.scores[1]+s1.scores[2] \u003e s2.scores[0]+s2.scores[1]+s2.scores[2]\r\n\t}).ForEach(func(s student) {\r\n\t\tfmt.Printf(\"\\t%s\\n\", s.String())\r\n\t})\r\n\r\nOutput:\r\n\r\n\t{id:7, name:King, age:22,scores:[87 91 89]}\r\n\t{id:10, name:Jim, age:20,scores:[64 99 93]}\r\n\t{id:9, name:King, age:21,scores:[94 63 93]}\r\n\t{id:4, name:Lucy, age:22,scores:[65 97 86]}\r\n\t{id:8, name:Jack, age:16,scores:[91 65 86]}\r\n\t{id:2, name:Lee, age:22,scores:[80 76 80]}\r\n\t{id:6, name:Jim, age:20,scores:[68 90 75]}\r\n\t{id:5, name:Mask, age:15,scores:[68 78 67]}\r\n\t{id:1, name:Kate, age:16,scores:[67 79 61]}\r\n\t{id:3, name:Lee, age:15,scores:[62 69 68]}\r\n\r\n### Distinct ###\r\nDistinct operation. equalFunc: func(o1,o2 T) bool\r\n\r\n    func (s *stream) Distinct(equalFunc interface{}) *stream\r\n\r\nSample:\r\n\r\n\tstudents := createStudents()\r\n\tstream, _ := New(students)\r\n\r\n\tstream.Map(func(s student) string {\r\n\t\treturn s.name\r\n\t}).Distinct(func(p1, p2 string) bool {\r\n\t\treturn p1 == p2\r\n\t}).ForEach(func(s string) {\r\n\t\tfmt.Printf(\"\\t%s\\n\", s)\r\n\t})\r\n\r\nOutput:\r\n\r\n\tKate\r\n\tLee\r\n\tLucy\r\n\tMask\r\n\tJim\r\n\tKing\r\n\tJack\r\n\r\n### Peek ###\r\nPeek operation. peekFunc: func(o T)\r\n\r\n    func (s *stream) Peek(peekFunc interface{}) *stream\r\n\r\nSample:\r\n\r\n\tstudents := createStudents()\r\n\tstream, _ := New(students)\r\n\r\n\tstream.Filter(func(s student) bool {\r\n\t\treturn s.age%2 == 0\r\n\t}).Call(func() {\r\n\t\tfmt.Println(\"\\tfilter by age % 2 == 0\")\r\n\t}).Peek(func(s student) {\r\n\t\tfmt.Printf(\"\\t%s\\n\", s.String())\r\n\t}).Filter(func(s student) bool {\r\n\t\treturn s.age \u003e 18\r\n\t}).Call(func() {\r\n\t\tfmt.Println(\"\\tfilter by age \u003e 18\")\r\n\t}).Peek(func(s student) {\r\n\t\tfmt.Printf(\"\\t%s\\n\", s.String())\r\n\t}).Exec()\r\n\r\nOutput:\r\n\r\n\tfilter by age % 2 == 0\r\n\t{id:1, name:Kate, age:16,scores:[67 79 61]}\r\n\t{id:2, name:Lee, age:22,scores:[80 76 80]}\r\n\t{id:4, name:Lucy, age:22,scores:[65 97 86]}\r\n\t{id:6, name:Jim, age:20,scores:[68 90 75]}\r\n\t{id:7, name:King, age:22,scores:[87 91 89]}\r\n\t{id:8, name:Jack, age:16,scores:[91 65 86]}\r\n\t{id:10, name:Jim, age:20,scores:[64 99 93]}\r\n\tfilter by age \u003e 18\r\n\t{id:2, name:Lee, age:22,scores:[80 76 80]}\r\n\t{id:4, name:Lucy, age:22,scores:[65 97 86]}\r\n\t{id:6, name:Jim, age:20,scores:[68 90 75]}\r\n\t{id:7, name:King, age:22,scores:[87 91 89]}\r\n\t{id:10, name:Jim, age:20,scores:[64 99 93]}\r\n\r\n### Call ###\r\nCall operation. Call function with the data.callFunc: func()\r\n\r\n    func (s *stream) Call(callFunc interface{}) *stream\r\n\r\n### Check ###\r\nCheck operation. Check if should be continue process data.checkFunc: func(o []T) bool ,checkFunc must return if should be continue process data.\r\n\r\n    func (s *stream) Check(checkFunc interface{}) *stream\r\n\r\n### Limit ###\r\nLimit operation.\r\n\r\n    func (s *stream) Limit(num int) *stream\r\n\r\nSample:\r\n\t\r\n\tstudents := createStudents()\r\n\tstream, _ := New(students)\r\n\r\n\tstream.Limit(5).Call(func() {\r\n\t\tfmt.Println(\"\\tlimit by 5\")\r\n\t}).ForEach(func(s student) {\r\n\t\tfmt.Printf(\"\\t%s\\n\", s.String())\r\n\t})\r\n\r\nOutput:\r\n\r\n\tlimit by 5\r\n\t{id:1, name:Kate, age:16,scores:[67 79 61]}\r\n\t{id:2, name:Lee, age:22,scores:[80 76 80]}\r\n\t{id:3, name:Lee, age:15,scores:[62 69 68]}\r\n\t{id:4, name:Lucy, age:22,scores:[65 97 86]}\r\n\t{id:5, name:Mask, age:15,scores:[68 78 67]}\r\n\r\n### Skip ###\r\nSkip operation.\r\n\r\n    func (s *stream) Skip(num int) *stream\r\n\r\nSample:\r\n\r\n\tstream.Skip(5).Call(func() {\r\n\t\tfmt.Println(\"\\tskip by 5\")\r\n\t}).ForEach(func(s student) {\r\n\t\tfmt.Printf(\"\\t%s\\n\", s.String())\r\n\t})\r\n\r\nOutput:\r\n\r\n\tskip by 5\r\n\t{id:6, name:Jim, age:20,scores:[68 90 75]}\r\n\t{id:7, name:King, age:22,scores:[87 91 89]}\r\n\t{id:8, name:Jack, age:16,scores:[91 65 86]}\r\n\t{id:9, name:King, age:21,scores:[94 63 93]}\r\n\t{id:10, name:Jim, age:20,scores:[64 99 93]}\r\n\r\n### AllMatch ###\r\nAllMatch operation. matchFunc: func(o T) bool\r\n\r\n    func (s *stream) AllMatch(matchFunc interface{}) bool\r\n\r\n### AnyMatch ###\r\nAnyMatch operation. matchFunc: func(o T) bool\r\n\r\n    func (s *stream) AnyMatch(matchFunc interface{}) bool\r\n\r\n### NoneMatch ###\r\nNoneMatch operation. matchFunc: func(o T) bool\r\n\r\n    func (s *stream) NoneMatch(matchFunc interface{}) bool\r\n\r\nSample:\r\n\r\n\tstudents := createStudents()\r\n\tstream, _ := New(students)\r\n\r\n\tr1 := stream.AllMatch(func(s student) bool {\r\n\t\treturn s.age \u003e 20\r\n\t})\r\n\tstream.Reset()\r\n\tr2 := stream.AnyMatch(func(s student) bool {\r\n\t\treturn s.name == \"Jim\"\r\n\t})\r\n\tstream.Reset()\r\n\tr3 := stream.NoneMatch(func(s student) bool {\r\n\t\treturn s.scores[0]+s.scores[1]+s.scores[2] \u003e 270\r\n\t})\r\n\tfmt.Printf(\"\\tAllMatch: %t, AnyMatch: %t, NoneMatch: %t \\n\", r1, r2, r3)\r\n\r\nOutput:\r\n\r\n    AllMatch: false, AnyMatch: true, NoneMatch: true\r\n\r\n### Count ###\r\nCount operation.Return the count of elements in stream.\r\n\r\n    func (s *stream) Count() int\r\n\r\nSample:\r\n\r\n\tstudents := createStudents()\r\n\tstream, _ := New(students)\r\n\r\n\tr := stream.Count()\r\n\tfmt.Printf(\"\\t%d\\n\", r)\r\n\r\nOutput:\r\n\r\n    10\r\n\r\n### Group ###\r\nGroup operation. Group values by key.groupFunc: func(o T1) (key T2,value T3). Return map[T2]T3.\r\n\r\n    func (s *stream) Group(groupFunc interface{}) interface{}\\\r\n\r\n### Max ###\r\nMax operation.lessFunc: func(o1,o2 T) bool\r\n\r\n    func (s *stream) Max(lessFunc interface{}) interface{}\r\n\r\n### Min ###\r\nMin operation.lessFunc: func(o1,o2 T) bool\r\n\r\n    func (s *stream) Min(lessFunc interface{}) interface{}\r\n\r\nSample:\r\n\r\n\tstudents := createStudents()\r\n\tstream, _ := New(students)\r\n\r\n\tr1 := stream.Max(func(s1, s2 student) bool {\r\n\t\treturn s1.scores[0]+s1.scores[1]+s1.scores[2] \u003c s2.scores[0]+s2.scores[1]+s2.scores[2]\r\n\t})\r\n\tstream.Reset()\r\n\tr2 := stream.Min(func(s1, s2 student) bool {\r\n\t\treturn s1.scores[0]+s1.scores[1]+s1.scores[2] \u003c s2.scores[0]+s2.scores[1]+s2.scores[2]\r\n\t})\r\n\tfmt.Printf(\"\\tMax: %v, Min: %v \\n\", r1, r2)\r\n\r\nOutput:\r\n\r\n    Max: {7 King 22 [87 91 89]}, Min: {3 Lee 15 [62 69 68]} \r\n\r\n### First ###\r\nFirst operation. matchFunc: func(o T) bool\r\n\r\n    func (s *stream) First(matchFunc interface{}) interface{}\r\n\r\n### Last ###\r\nLast operation. matchFunc: func(o T) bool\r\n\r\n    func (s *stream) Last(matchFunc interface{}) interface{}\r\n\r\n### Reduce ###\r\nReduce operation. reduceFunc: func(r T2,o T) T2\r\n\r\n    func (s *stream) Reduce(initValue interface{}, reduceFunc interface{}) interface{}\r\n\r\nSample:\r\n\r\n\tstudents := createStudents()\r\n\tstream, _ := New(students)\r\n\r\n\tr := 0\r\n\tr = stream.Map(func(s student) int {\r\n\t\treturn s.scores[0]\r\n\t}).Reduce(r, func(sum int, i int) int {\r\n\t\treturn sum + i\r\n\t}).(int)\r\n\tfmt.Printf(\"\\t%d\\n\", r)\r\n\r\nOutput:\r\n\r\n    746\r\n\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftk103331%2Fstream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftk103331%2Fstream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftk103331%2Fstream/lists"}