samber / mo

🦄 Monads and popular FP abstractions, powered by Go 1.18+ Generics (Option, Result, Either...)
https://pkg.go.dev/github.com/samber/mo
MIT License
2.47k stars 80 forks source link

Add Helper function that would work like Either.Match but would return another type #6

Open CorentinClabaut opened 1 year ago

CorentinClabaut commented 1 year ago

Could we have a helper function like the following for Either?

func Map[T any](e Either[L, R], onLeft func(L) T, onRight func(R) T) T 

It would be useful to be able to Map an Either to another type.

civilizeddev commented 1 year ago

I also want there:

func Map[T, R any](a Functor[T], f func(value T) R) Functor[R]
func Ap[T, R any](a Applicative[T], ff Applicative[func(value T) R]) Applicative[R]
func FlatMap[T, R any](a Monad[T], f func(value T) Monad[R]) Monad[R]

But I'm not sure these can be implemented in Go.

vikstrous2 commented 3 months ago

I would love to have this function too. Was thinking that the name "Merge" could also describe it well, but maybe it's called map in other languages? 🤷 What does haskel call it?

taman9333 commented 4 weeks ago

@CorentinClabaut @civilizeddev @vikstrous2 Fold Can do this functionality & it runs against Result, Either & Option monads