https://github.com/genkami/dogs
Make Go functional with dogs
https://github.com/genkami/dogs
functional go golang
Last synced: about 1 year ago
JSON representation
Make Go functional with dogs
- Host: GitHub
- URL: https://github.com/genkami/dogs
- Owner: genkami
- License: apache-2.0
- Created: 2021-09-27T08:06:15.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-11-08T21:57:12.000Z (over 1 year ago)
- Last Synced: 2024-11-08T22:34:03.359Z (over 1 year ago)
- Topics: functional, go, golang
- Language: Go
- Homepage:
- Size: 145 KB
- Stars: 38
- Watchers: 4
- Forks: 5
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# dogs

[](https://pkg.go.dev/github.com/genkami/dogs)

Make Go functional with dogs
# Caution
This is a **highly-experimental** package. Any changes will be made in a backward-incompatible manner.
This package will not compile without `gotip` since type parameters are not supported currently in any of Go releases.
Probably **you don't need this** even after type parameters becomes GA. Even if you feel you do, maybe **you shouldn't use this**. It's against Go's philosophy.
# Features
We will continue to implement more utility types and functions.
## Type classes
* Eq
* Ord
* Semigroup
* Monoid
## Data types
* Pair
* List
* Slice
* Map
* Set
* Iterator
# Examples
More examples [here](https://github.com/genkami/dogs/tree/main/examples).
## FizzBuzz
```go
func main() {
monoid := option.DeriveMonoid[string](algebra.DeriveAdditiveSemigroup[string]())
fizzBuzz := func(i int) string {
fizz := option.Filter(option.Some[string]("Fizz"), func(_ string) bool { return i%3 == 0 })
buzz := option.Filter(option.Some[string]("Buzz"), func(_ string) bool { return i%5 == 0 })
return option.UnwrapOr(monoid.Combine(fizz, buzz), fmt.Sprint(i))
}
it := iterator.Map(iterator.Range[int](1, 15), fizzBuzz)
iterator.ForEach(it, func(s string) { fmt.Println(s) })
}
```
## Fibonacci
```go
func main() {
type Pair = pair.Pair[int, int]
it := iterator.Unfold(
Pair{1, 1},
func(p Pair) (Pair, int, bool) {
a, b := p.Values()
return Pair{b, a + b}, a, true
},
)
iterator.ForEach(
iterator.Take(it, 5),
func(i int) { fmt.Println(i) },
)
}
```
# Acknowledgements
This library is inspired mainly by:
* [Haskell standard library](https://hackage.haskell.org/package/base)
* [Scala Cats](https://typelevel.org/cats/)
and many other functional languages.
# License
Distributed under the Apache License, Version 2.0. See LICENSE for more information.