{"id":13411512,"url":"https://github.com/disksing/iter","last_synced_at":"2025-04-07T11:32:43.730Z","repository":{"id":65288685,"uuid":"216337280","full_name":"disksing/iter","owner":"disksing","description":"Go implementation of C++ STL iterators and algorithms.","archived":false,"fork":false,"pushed_at":"2022-03-16T14:56:41.000Z","size":187,"stargazers_count":185,"open_issues_count":0,"forks_count":13,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-07-31T20:46:07.669Z","etag":null,"topics":["algorithms","datastructures","stl"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/disksing.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}},"created_at":"2019-10-20T09:29:49.000Z","updated_at":"2024-04-24T06:51:42.000Z","dependencies_parsed_at":"2023-01-16T15:01:44.115Z","dependency_job_id":null,"html_url":"https://github.com/disksing/iter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/disksing%2Fiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/disksing%2Fiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/disksing%2Fiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/disksing%2Fiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/disksing","download_url":"https://codeload.github.com/disksing/iter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247645008,"owners_count":20972392,"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","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":["algorithms","datastructures","stl"],"created_at":"2024-07-30T20:01:14.167Z","updated_at":"2025-04-07T11:32:43.080Z","avatar_url":"https://github.com/disksing.png","language":"Go","readme":"# iter\n\n**Note: I'm currently working on making it work with Go generics. For previous version, please check `v1` branch.**\n\nGo implementation of C++ STL iterators and algorithms.\n\nLess hand-written loops, more expressive code.\n\nREADME translations: [简体中文](README_ZH.md)\n\n[![GoDoc](https://godoc.org/github.com/disksing/iter?status.svg)](https://godoc.org/github.com/disksing/iter)\n[![Build Status](https://travis-ci.com/disksing/iter.svg?branch=master)](https://travis-ci.com/disksing/iter)\n[![codecov](https://codecov.io/gh/disksing/iter/branch/master/graph/badge.svg)](https://codecov.io/gh/disksing/iter)\n[![Go Report Card](https://goreportcard.com/badge/github.com/disksing/iter)](https://goreportcard.com/report/github.com/disksing/iter)\n\n## Motivation\n\nAlthough Go doesn't have generics, we deserve to have reuseable general algorithms. `iter` helps improving Go code in several ways:\n\n- Some simple loops are unlikely to be wrong or inefficient, but calling algorithm instead will **make the code more concise and easier to comprehend**. Such as [AllOf](https://godoc.org/github.com/disksing/iter#AllOf), [FindIf](https://godoc.org/github.com/disksing/iter#FindIf), [Accumulate](https://godoc.org/github.com/disksing/iter#Accumulate).\n\n- Some algorithms are not complicated, but it is not easy to write them correctly. **Reusing code makes them easier to reason for correctness**. Such as [Shuffle](https://godoc.org/github.com/disksing/iter#Shuffle), [Sample](https://godoc.org/github.com/disksing/iter#Sample), [Partition](https://godoc.org/github.com/disksing/iter#Partition).\n\n- STL also includes some complicated algorithms that may take hours to make it correct. **Implementing it manually is impractical**. Such as [NthElement](https://godoc.org/github.com/disksing/iter#NthElement), [StablePartition](https://godoc.org/github.com/disksing/iter#StablePartition), [NextPermutation](https://godoc.org/github.com/disksing/iter#NextPermutation).\n\n- The implementation in the library contains some **imperceptible performance optimizations**. For instance, [MinmaxElement](https://godoc.org/github.com/disksing/iter#MinmaxElement) is done by taking two elements at a time. In this way, the overall number of comparisons is significantly reduced.\n\nThere are alternative libraries have similar goals, such as [gostl](https://github.com/liyue201/gostl), [gods](https://github.com/emirpasic/gods) and [go-stp](https://github.com/itrabbit/go-stp). What makes `iter` unique is:\n\n- **Non-intrusive**. Instead of introducing new containers, `iter` tends to reuse existed containers in Go (slice, string, list.List, etc.) and use iterators to adapt them to algorithms.\n\n- **Full algorithms (\u003e100)**. It includes almost all algorithms come before C++17. Check the [Full List](https://github.com/disksing/iter/wiki/Algorithms).\n\n## Examples\n\n\u003e The examples are run with some function alias to make it simple. See [example_test.go](https://github.com/disksing/iter/blob/master/examples_test.go) for the detail.\n\n\u003ctable\u003e\n\u003cthead\u003e\u003ctr\u003e\u003cth colspan=\"2\"\u003ePrint a list.List\u003c/th\u003e\u003c/tr\u003e\u003c/thead\u003e\n\u003ctbody\u003e\u003ctd\u003e\n\n```go\nl := list.New()\nfor i := 1; i \u003c= 5; i++ {\n  l.PushBack(i)\n}\nfor e := l.Front(); e != nil; e = e.Next() {\n  fmt.Print(e.Value)\n  if e.Next() != nil {\n    fmt.Print(\"-\u003e\")\n  }\n}\n// Output:\n// 1-\u003e2-\u003e3-\u003e4-\u003e5\n```\n\n\u003c/td\u003e\u003ctd\u003e\n\n```go\nl := list.New()\nGenerateN(ListBackInserter(l), 5, IotaGenerator(1))\nCopy(lBegin(l), lEnd(l), IOWriter(os.Stdout, \"-\u003e\"))\n// Output:\n// 1-\u003e2-\u003e3-\u003e4-\u003e5\n```\n\n\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\n\n\u003cthead\u003e\u003ctr\u003e\u003cth colspan=\"2\"\u003eReverse a string\u003c/th\u003e\u003c/tr\u003e\u003c/thead\u003e\n\u003ctbody\u003e\u003ctr\u003e\u003ctd\u003e\n\n```go\ns := \"!dlrow olleH\"\nvar sb strings.Builder\nfor i := len(s) - 1; i \u003e= 0; i-- {\n  sb.WriteByte(s[i])\n}\nfmt.Println(sb.String())\n\nb := []byte(s)\nfor i := len(s)/2 - 1; i \u003e= 0; i-- {\n  j := len(s) - 1 - i\n  b[i], b[j] = b[j], b[i]\n}\nfmt.Println(string(b))\n// Output:\n// Hello world!\n// Hello world!\n```\n\n\u003c/td\u003e\u003ctd\u003e\n\n```go\ns := \"!dlrow olleH\"\nfmt.Println(MakeString(StringRBegin(s), StringREnd(s)))\n\nb := []byte(s)\nReverse(begin(b), end(b))\nfmt.Println(string(b))\n// Output:\n// Hello world!\n// Hello world!\n```\n\n\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\n\n\u003cthead\u003e\u003ctr\u003e\u003cth colspan=\"2\"\u003eIn-place deduplicate (from \u003ca href=\"https://github.com/golang/go/wiki/SliceTricks#in-place-deduplicate-comparable\"\u003eSliceTricks\u003c/a\u003e, with minor change)\u003c/th\u003e\u003c/tr\u003e\u003c/thead\u003e\n\u003ctbody\u003e\u003ctr\u003e\u003ctd\u003e\n\n```go\nin := []int{3, 2, 1, 4, 3, 2, 1, 4, 1}\nsort.Ints(in)\nj := 0\nfor i := 1; i \u003c len(in); i++ {\n  if in[j] == in[i] {\n    continue\n  }\n  j++\n  in[j] = in[i]\n}\nin = in[:j+1]\nfmt.Println(in)\n// Output:\n// [1 2 3 4]\n```\n\n\u003c/td\u003e\u003ctd\u003e\n\n```go\nin := []int{3, 2, 1, 4, 3, 2, 1, 4, 1}\nSort(begin(in), end(in))\nErase(\u0026in, Unique(begin(in), end(in)))\nfmt.Println(in)\n// Output:\n// [1 2 3 4]\n```\n\n\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\n\n\u003cthead\u003e\u003ctr\u003e\u003cth colspan=\"2\"\u003eSum all integers received from a channel\u003c/th\u003e\u003c/tr\u003e\u003c/thead\u003e\n\u003ctbody\u003e\u003ctr\u003e\u003ctd\u003e\n\n```go\nch := make(chan int)\ngo func() {\n  for _, x := range rand.Perm(100) {\n    ch \u003c- x + 1\n  }\n  close(ch)\n}()\nvar sum int\nfor x := range ch {\n  sum += x\n}\nfmt.Println(sum)\n// Output:\n// 5050\n```\n\n\u003c/td\u003e\u003ctd\u003e\n\n```go\nch := make(chan int)\ngo func() {\n  CopyN(IotaReader(1), 100, ChanWriter(ch))\n  close(ch)\n}()\nfmt.Println(Accumulate(ChanReader(ch), ChanEOF, 0))\n// Output:\n// 5050\n```\n\n\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\n\n\u003cthead\u003e\u003ctr\u003e\u003cth colspan=\"2\"\u003eRemove consecutive spaces in a string\u003c/th\u003e\u003c/tr\u003e\u003c/thead\u003e\n\u003ctbody\u003e\u003ctr\u003e\u003ctd\u003e\n\n```go\nstr := \"  a  quick   brown  fox  \"\nvar sb strings.Builder\nvar prevIsSpace bool\nfor i := 0; i \u003c len(str); i++ {\n  if str[i] != ' ' || !prevIsSpace {\n    sb.WriteByte(str[i])\n  }\n  prevIsSpace = str[i] == ' '\n}\nfmt.Println(sb.String())\n// Output:\n// a quick brown fox\n```\n\n\u003c/td\u003e\u003ctd\u003e\n\n```go\nstr := \"  a  quick   brown  fox  \"\nvar sb StringBuilderInserter\nUniqueCopyIf(sBegin(str), sEnd(str), \u0026sb,\n  func(x, y Any) bool { return x.(byte) == ' ' \u0026\u0026 y.(byte) == ' ' })\nfmt.Println(sb.String())\n// Output:\n// a quick brown fox\n```\n\n\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\n\n\u003cthead\u003e\u003ctr\u003e\u003cth colspan=\"2\"\u003eCollect N maximum elements from a channel\u003c/th\u003e\u003c/tr\u003e\u003c/thead\u003e\n\u003ctbody\u003e\u003ctr\u003e\u003ctd\u003e\n\n```go\n// Need to manually mantain a min-heap.\n```\n\n\u003c/td\u003e\u003ctd\u003e\n\n```go\ntop := make([]int, 5)\nPartialSortCopyBy(ChanReader(ch), ChanEOF, begin(top), end(top),\n  func(x, y Any) bool { return x.(int) \u003e y.(int) })\nCopy(begin(top), end(top), IOWriter(os.Stdout, \", \"))\n```\n\n\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\n\n\u003cthead\u003e\u003ctr\u003e\u003cth colspan=\"2\"\u003ePrint all permutations of [\"a\", \"b\", \"c\"]\u003c/th\u003e\u003c/tr\u003e\u003c/thead\u003e\n\u003ctbody\u003e\u003ctr\u003e\u003ctd\u003e\n\n```go\n// Usually requires some sort of recursion\n```\n\n\u003c/td\u003e\u003ctd\u003e\n\n```go\ns := []string{\"a\", \"b\", \"c\"}\nfor ok := true; ok; ok = NextPermutation(begin(s), end(s)) {\n  fmt.Println(s)\n}\n// Output:\n// [a b c]\n// [a c b]\n// [b a c]\n// [b c a]\n// [c a b]\n// [c b a]\n```\n\n\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\n\u003c/table\u003e\n\n## Thanks\n\n- [cppreference.com](https://en.cppreference.com/)\n- [LLVM libc++](https://libcxx.llvm.org/)\n\n## License\n\nBSD 3-Clause\n","funding_links":[],"categories":["数据结构与算法","Data Structures and Algorithms","Uncategorized","Data Structures","数据结构","数据结构`go语言实现的数据结构与算法`","Data Integration Frameworks","Generators"],"sub_categories":["迭代器","Iterators","Advanced Console UIs","标准 CLI"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdisksing%2Fiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdisksing%2Fiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdisksing%2Fiter/lists"}