monads

Monads, For Golang, Using Generics
git clone git://git.lair.cx/monads
Log | Files | Refs | README | LICENSE

commit 3fa43aeb4238886beefd0198dc26ce7b059a6e35
parent 5e265f180e96ceb4317ff2e4a8ef7a4eff80b998
Author: Yongbin Kim <iam@yongbin.kim>
Date:   Mon, 12 Sep 2022 15:18:59 +0900

Added README, LICENSE

Signed-off-by: Yongbin Kim <iam@yongbin.kim>

Diffstat:
ALICENSE | 25+++++++++++++++++++++++++
AREADME | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 103 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,25 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to <https://unlicense.org> + diff --git a/README b/README @@ -0,0 +1,78 @@ +go.lair.cx/monads +================= + +Tiny monad utilities for Go, built with generics. + +Requires Go 1.18+. + + +futures +------- + +Also known as Promise. + +``` +job := futures.Run(func() (int, error) { + // Do something here + return 42, nil +}) + +// Wait until finish +result, err := job.Await() + +// Context-like done +ch := job.Done() + +// Chaining (Mapping) +futures.Map(job, func(v int) (uint32, error) { + return uint32(v), nil +}) +// or +futures.FlatMap(job, func(v int) *Future[uint32] { + if v != 42 { + return futures.Reject(errors.New("wrong answer")) + } + return futures.Resolve(uint32(v)) +}) +``` + + +options +------- + +``` +opt := options.Wrap(42) // Option[int] +opt = options.Map(opt, foo) // Option[int] (foo returns int) +opt = options.Map(opt, func(i int) bool { // Option[bool] + return false +}) +opt = options.FlatMap(opt, func(b bool) Option[int] { // Option[int] + // b is false, so, this function returns empty. + if b { + return options.Wrap(42) + } + return options.Empty() +}) +opt = options.Map(opt, bar) // opt is empty, so, bar is not called. +``` + + +slices +------ + +``` +slices.Filter([]int{1, 2, 3, 4, 5}, func(v int) bool { + return v > 3 +}) +// returns []int{4, 5} + +slices.Map([]int{1, 2, 3, 4, 5}, func(v int) uint64 { + return uint64(v) +}) +// returns []uint64{1, 2, 3, 4, 5} + +slices.FlatMap([]int{1, 2, 3, 4, 5}, func(v int) []int { + return []int{v, v + 1} +}) +// returns []int{1, 2, 2, 3, 3, 4, 4, 5, 5, 6} +```