{"id":27972742,"url":"https://github.com/datosh/algo","last_synced_at":"2025-06-15T16:08:36.531Z","repository":{"id":57655403,"uuid":"455339876","full_name":"datosh/algo","owner":"datosh","description":"Algorithms in golang using type parameters","archived":false,"fork":false,"pushed_at":"2022-02-06T00:39:57.000Z","size":11,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-06-20T12:50:49.905Z","etag":null,"topics":["algorithm","algorithms","golang","type-parameters"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/datosh/algo","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/datosh.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":"2022-02-03T21:49:01.000Z","updated_at":"2024-06-20T12:50:49.906Z","dependencies_parsed_at":"2022-09-01T01:40:48.853Z","dependency_job_id":null,"html_url":"https://github.com/datosh/algo","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datosh%2Falgo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datosh%2Falgo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datosh%2Falgo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/datosh%2Falgo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/datosh","download_url":"https://codeload.github.com/datosh/algo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252968499,"owners_count":21833308,"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":["algorithm","algorithms","golang","type-parameters"],"created_at":"2025-05-07T23:19:33.014Z","updated_at":"2025-05-07T23:19:33.630Z","avatar_url":"https://github.com/datosh.png","language":"Go","readme":"# algo\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/datosh/algo.svg)](https://pkg.go.dev/github.com/datosh/algo)\n\nAlgorithm library for golang1.18+ using\n[type parameters.](https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md)\n\n## Installation\n\n```bash\n# Or any other way to install go1.18 beta.\n# https://go.dev/blog/go1.18beta2\ngo install golang.org/dl/go1.18beta2@latest\ngo1.18beta2 download\n# Install library\ngo1.18beta2 get github.com/datosh/algo\n```\n\n## Usage\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/datosh/algo\"\n)\n\nfunc main() {\n\tsum := algo.Fold(func(a, b int) int { return a + b })\n\tnumbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}\n\tfmt.Println(sum(numbers)) // 45\n}\n\n```\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n\n\t\"github.com/datosh/algo\"\n)\n\nfunc main() {\n\tonlyShortWords := algo.Filter(func(s string) bool { return len(s) \u003c= 5 })\n\tshout := algo.Map(func(s string) string { return strings.ToUpper(s) })\n\tconcat := algo.Fold(func(t1, t2 string) string { return t1 + \" \" + t2 })\n\n\twords := []string{\"Hello\", \"gophers\", \"World\"}\n\n\tsentence := concat(shout(onlyShortWords(words)))\n\tfmt.Println(sentence) // HELLO WORLD\n}\n```\n\n## Goal\n\nExplore how a go library for algorithms COULD look like.\n\nTo get an idea what is usually implemented in such a library I looked at\n* [D Algorithms library](https://dlang.org/phobos/std_algorithm_iteration.html)\n* [C++ Algorithms library](https://en.cppreference.com/w/cpp/algorithm)\n\n## Limitations\n\n* Currently only support slices\n\n## Roadmap / Ideas\n\n* \"Type cast\" a slice of build-in types\n* \"Type cast\" a slice of complex types\n* Iterator based approach to support any data structure\n    * [On Iteration - Andrei Alexandrescu](https://www.informit.com/articles/printerfriendly/1407357)\n\n## Performance\n\n\u003e :warning: **No performance optimizations were implemented, yet!**\n\nThese benchmarks were created to get a feeling for the rough performance\ncharacteristics of golangs new type parameters.\n\n**Baseline** is a custom implementation for a specific type.  \n**Filter/Map** is the generic implementation returning a filter/map function.  \n**Filter2/Map2** is the generic implementation doing the operation in one call.  \n**Interfaced** is a `interface{}` based implementations.  \n\n\n**cpu: 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz**\n| Test | Iterations | Speed |\n| :- | :-: | -: |\n| Filter Baseline | 506 | 2989602 ns/op |\n| Filter | 172 | 6455209 ns/op |\n| Filter2 | 451 | 8465454 ns/op |\n| Filter Interfaced | 48 | 25998184 ns/op |\n| Map Baseline | 97946026 | 12.43 ns/op |\n| Map | 6051519 | 182.3 ns/op |\n| Map2 | 4241458 | 292.0 ns/op |\n\nMy take-away is that a performance hit is to be expected when using type\nparemters compared to a custom implementation. On the other hand, if you\nrelied on `interface{}` \u0026 `reflection` based implementations in the past,\ntype parameters will provide you both with a performance as well as\nsafety (compiler checks) improvement.\n\n## Contributing\n\n## License\n[MIT](https://choosealicense.com/licenses/mit/)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdatosh%2Falgo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdatosh%2Falgo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdatosh%2Falgo/lists"}