{"id":17169450,"url":"https://github.com/johnmurray/bastard-go","last_synced_at":"2025-03-24T19:18:30.943Z","repository":{"id":137210603,"uuid":"112221780","full_name":"JohnMurray/bastard-go","owner":"JohnMurray","description":"Terrible things I wrote in Go","archived":false,"fork":false,"pushed_at":"2017-12-12T22:00:46.000Z","size":687,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-15T09:11:46.570Z","etag":null,"topics":["code-generation","functional-programming","generics","go","golang","terrible-idea"],"latest_commit_sha":null,"homepage":null,"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/JohnMurray.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-11-27T16:41:54.000Z","updated_at":"2024-06-19T10:23:35.726Z","dependencies_parsed_at":null,"dependency_job_id":"a988dfe7-5bc7-4a2d-bb99-93b627a63d46","html_url":"https://github.com/JohnMurray/bastard-go","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/JohnMurray%2Fbastard-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnMurray%2Fbastard-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnMurray%2Fbastard-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohnMurray%2Fbastard-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JohnMurray","download_url":"https://codeload.github.com/JohnMurray/bastard-go/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245334909,"owners_count":20598389,"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":["code-generation","functional-programming","generics","go","golang","terrible-idea"],"created_at":"2024-10-14T23:26:16.006Z","updated_at":"2025-03-24T19:18:30.913Z","avatar_url":"https://github.com/JohnMurray.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Bastard Go\n\nA collection of terrible things in I felt like writing in Go\n\nMost of the things in this repository use [`genny`][github_genny] to generate code, so to really play with\nany of this horrible stuff, you should run\n\n```bash\ngo get github.com/cheekybits/genny\ngo install github.com/cheekybits/genny\n\ngo get github.com/johnmurray/bastard-go\n```\n\nFor more details on Genny and how the generation works and options to use with the command, head on over\nto their GitHub page. For examples, see the `examples` folder.\n\n__Fair Warning:__ This code is just me playing around with what it would look like if Go has some sort of\ngeneric programming capability and/or me just feeling like writing something ridiculous in Go. You likely\nshouldn't use any of this in production code. If you really want to, feel free but I make absolutely no\npromises that any of this code is correct or safe.\n\n### TOC\n\n  * [__Concurrent Types__](#concurrent-types)\n    * [__Future__](#future)\n  * [__Data Structures__](#data-structures)\n    * [__coll.Ringbuffer__](#collringbuffer)\n  * [__Functional Types__](#functional-types)\n    * [__Either__](#either)\n    * [__Maybe__](#maybe)\n    * [__Try__](#try)\n  * [__Contributing__](#contributing)\n  * [__License__](#license)\n\n\n## Concurrent Types\n\n### Future\n\n// TODO: write docs\n\n## Data Structures\n\n### coll.Ringbuffer\n\nWhile writing a scheduler for [__Future__](#future)s, I needed a way to distribute work amongst workers for my\nscheduling code and wanted to use a ring-buffer because it sounded like it would be fun to write. What follows was\na non-thread-safe ring-buffer that is poorly tested. Either way the API is rather simple. Create a new one with a\nfixed size, push and pop, or empty the buffer. \n\nThe ring-buffer uses [`genny`][github_genny] to generate templates which allow it to work very similar to a version\nusing generics. To use in your own projects:\n\n```bash\ncd $YOUR_PROJECT\nmkdir buffer\n```\n\n```go\n// Generate for as many types as you like. Simply including a comma-delimited list for `Type` will\n// cause genny to generate multiple templates.\n//\n//go:generate genny -in=$GOPATH/src/github.com/johnmurray/bastard-go/coll/ringbuffer.go.tmp -out=buffer/ringbuffer.go gen \"Type=int,string,float\"\n```\n\n## Functional Types\n\n### Either\n\nThis is a right-bias `Either` type that uses [`genny`][github_genny] to generate templates which allow it to work\nvery vimilar to a version using generics. However, this is probably the clearest example (in this project) where\ngenerics would greatly simplify the boilerplate of code-generation as it is a multi-type container that has the\nability to map to other `Either` types with different underlying \"generic\" types. Creating a many-to-many mapping\nthat makes generation tricky and cumbersome.\n\nTo use in your own projects:\n\n```bash\ncd $YOUR_PROJECT\nmkdir either\n```\n\n```go\n// Generate for as many types as you like. Simply including a comma-delimited list for\n// `TypeA` and `TypeB` will cause genny to generate multiple templates. Note that providing\n// multiple for both inputs will create every possible combination.\n//\n//go:generate genny -in=$GOPATH/src/github.com/johnmurray/bastard-go/either/either_base.go.tmp -out=either/either_base.go gen \"TypeA=int,string,bool TypeB=int,string,bool\"\n\n// Another command will generate the \"composition\" functions which allow you to convert\n// from one type to another // when using Map, FlatMap, etc. This is also necessary for\n// generating same-type conversions. Such as:\n//   eitherStringOrInt.MapRightToInt(func (i int) int { return i * 2 })\n//\n// Note that to avoid generating duplicate functions (due to so many input types and overlapping\n// righ/left combinations) the `ToTypeA` and `ToTypeB` should be done one at a time, using the same\n// value for each:\n//\n//go:generate genny -in=$GOPATH/src/github.com/johnmurray/bastard-go/either/either_compose.go.tmp -out=either/either_compose_1.go gen \"FromTypeA=int,string,bool FromTypeB=int,string,bool ToTypeA=int ToTypeB=int\"\n//go:generate genny -in=$GOPATH/src/github.com/johnmurray/bastard-go/either/either_compose.go.tmp -out=either/either_compose_2.go gen \"FromTypeA=int,string,bool FromTypeB=int,string,bool ToTypeA=string ToTypeB=string\"\n//go:generate genny -in=$GOPATH/src/github.com/johnmurray/bastard-go/either/either_compose.go.tmp -out=either/either_compose_3.go gen \"FromTypeA=int,string,bool FromTypeB=int,string,bool ToTypeA=bool ToTypeB=bool\"\n```\n\n### Maybe\n\nThis is a Scala/Haskell like `Some`/`Maybe` object that uses [`genny`][github_genny] to generate templates which\nallow it to work very similar to a version using generics. To use in your own projects:\n\n```bash\ncd $YOUR_PROJECT\nmkdir maybe\n```\n\n```go\n// Generate for as many types as you like. Simply including a comma-delimited list for\n// `Type` will cause genny to generate multiple templates.\n//\n//go:generate genny -in=$GOPATH/src/github.com/johnmurray/bastard-go/maybe/maybe_base.go.tmp -out=maybe/maybe_base.go gen \"Type=int,string,bool\"\n\n// Another command will generate the \"composition\" functions which allow you to convert\n// from one type to another // when using Map, FlatMap, etc. This is also necessary for\n// generating same-type conversions. Such as:\n//   IntMaybe.MapToInt(func (i int) int { return i * 2 })\n//\n// Note that by providing multiple input types, all permutations will be generated.\n//\n//go:generate genny -in=$GOPATH/src/github.com/johnmurray/bastard-go/maybe/maybe_compose.go.tmp -out=maybe/maybe_compose.go gen \"FromType=int,string,bool ToType=int,string,bool\"\n```\n\n\n### Try\n\nThis is a Scala-like `Try` object that uses [`genny`][github_genny] to generate templates which allow it to\nwork very similar to the Scala version with composition functions (`Map`, `FlatMap`, `Recover`, etc). To use\nin your own projects:\n\n```bash\ncd $YOUR_PROJECT\nmkdir try\n```\n\n```go\n// Generate for as many types as you like. Simply including a comma-delimited list for\n// `Type` will cause genny to generate multiple templates.\n//\n//go:generate genny -in=$GOPATH/src/github.com/johnmurray/bastard-go/try/try_base.go.tmp -out=try/try_base.go gen \"Type=int,string,bool\"\n\n// Another command will generate the \"composition\" functions which allow you to convert\n// from one type to another // when using Map, FlatMap, etc. This is also necessary for\n// generating same-type conversions. Such as:\n//   IntTry.MapToTry(func (i int) int { return i * 2 })\n//\n// Note that by providing multiple input types, all permutations will be generated.\n//\n//go:generate genny -in=$GOPATH/src/github.com/johnmurray/bastard-go/try/try_compose.go.tmp -out=try/try_compose.go gen \"FromType=int,string,bool ToType=int,string,bool\"\n```\n\nOf course you can use `genny` to generate it in a different package, so read more about `genny` if you're not familiar.\nSee my [blog post][johnmurray_io] for more details.\n\n\n## Contributing\n\nHave you'd like to implement that you think belongs in this repository? Feel free to open a PR to talk about\nyour idea, or to present some code you think would fit in. The more the merrier! :-D\n\n## License\n\nProject is licensed under MIT license. Please see `LICENSE` file for full details.\n\n\n  [github_genny]: https://github.com/cheekybits/genny\n  [johnmurray_io]: http://www.johnmurray.io/log/2017/11/27/Go-Try.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnmurray%2Fbastard-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohnmurray%2Fbastard-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnmurray%2Fbastard-go/lists"}