Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/glours/go2funk
Simple Golang API to use functional types in Golang, such as immutable List, Options, Try, Either...
https://github.com/glours/go2funk
functional functional-programming generics go golang program
Last synced: 19 days ago
JSON representation
Simple Golang API to use functional types in Golang, such as immutable List, Options, Try, Either...
- Host: GitHub
- URL: https://github.com/glours/go2funk
- Owner: glours
- Created: 2020-09-16T22:59:39.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-07-22T20:06:49.000Z (over 1 year ago)
- Last Synced: 2024-10-17T19:46:18.056Z (29 days ago)
- Topics: functional, functional-programming, generics, go, golang, program
- Language: Go
- Homepage:
- Size: 58.6 KB
- Stars: 7
- Watchers: 5
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Go2Funk
Go2Funk is a pet project to test Go2 Generics and see how this new feature help us to implement some functional
structures easily.
The idea is to keep this repo as much as possible dependency free.## How to compile
Golang `1.18` has been release with type parameters.
You can now, compile and test your code as usual (`go build`, `go test` ...)You can also use Docker Dev Environments to develop this project:
* Open Docker Desktop
* Go to Dev Environments view
* Copy/Paste the git repo URL to start working!
Check the configuration of the base image in the `.docker/config.json` config file## Build
use `go build ./...` or if you
use [Goland then configure external tools](https://www.jetbrains.com/help/go/how-to-use-type-parameters-for-generic-programming.html)## Run tests
use `go test -v ./...` or if you
use [Goland then configure external tools](https://www.jetbrains.com/help/go/how-to-use-type-parameters-for-generic-programming.html)## Usage
### Collection types
#### List
```go
myIntArray := []int{1, 2, 3, 4, 5}
myList := OfSlice[int](myIntArray)myList = myList.Append(6)
fmt.Println(myList.Length())var mapper = func (value int) string { return strconv.Itoa(value) }
var evenPredicate = func (value int) bool { return value % 2 == 0 }
myList = MapList[int, string](myList.Filter(evenPredicate), mapper);
```For more usage details check [tests](./api/collection/list_test.go).
### Control types
#### Option
```go
empytOption := Empty[int]()
someOption := Of[int](10)fmt.Println(emptyOption.GetOrElse(5)) // Print 5
fmt.Println(someOption.GetOrElse(5)) // Print 10var evenPredicate = func (value int) bool { return value % 2 == 0 }
fmt.Println(some.Filter(eventPredicate).IsEmpty()) #Print falsevar mapper = func (value int) string { return strconv.Itoa(value) }
fmt.Println(MapOption(someOption, mapper)) // Print "10"
```For more usage details check [tests](./api/control/option_test.go).
#### Try
```go
defaultTryError := errors.New("default Try error")failure := FailureOf[int](defaultTryError)
success := SuccessOf[int](10)fmt.Println(failure.GetOrElse(5)) // Print 5
fmt.Println(someOption.GetOrElse(5)) // Print 10fmt.Println(failure.GetOrElseCause()) // Print "default Try error"
fmt.Println(someOption.GetOrElseCause()) // Print 10successLambda := func ()(int, error) { return 10, nil})
failureLambda := func ()(int, error) {return 0, defaultTryError }fmt.Println(TryOf(failureLambda).IsFailure()) // Print true
fmt.Println(TryOf(successLambda).IsFailure()) // Print false
```For more usage details check [tests](./api/control/try_test.go).
#### Either
```go
defaultEitherError = errors.New("default Either error")
right = RightOf[error, int](10)
left = LeftOf[error, int](defaultEitherError)fmt.Println(right.GetOrElse(20)) // Print 10
fmt.Println(left.GetOrElse(20)) // Print 20fmt.Println(right.GetLeftOrElse(error.New("new error"))) // Print "new error"
fmt.Println(left.GetOrElse(error.New("new error"))) // Print "default Either error"var mapper = func (value int) string { return strconv.Itoa(value) }
var mapRight Either[error, string] = MapEither(right, mapper)
fmt.Println(mapRight.GetOrElse("good")) // Print "10"
```For more usage details check [tests](./api/control/either_test.go).