Skip to content

feat: adding option/result/either/eitherX packages, with basic operations and pipelining #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

samber
Copy link
Owner

@samber samber commented Aug 10, 2025

Solving #6 #11 #37 #66 #77

This PR brings 2 improvements:

  • transformation of return type when using map/flatmap/match/etc...
  • chaining of multiple transformations

Note:

  • does not fully respect FP paradigm
  • does not allow monad conversion (eg: result to monad)

I would be happy to have some feedback!

@CorentinClabaut @civilizeddev @tbflw @Luviz @axaluss @aeramu

@aeramu
Copy link

aeramu commented Aug 11, 2025

Thanks a lot for the changes! The pipe looks good to me. Try to use it, and works well.
For the transform Map, I found it has different signature with the method

func (r Result[T]) Map(mapper func(value T) (T, error)) Result[T]

And for my use case, I actually need to create this helper function and some curry function to use PipeX. But for now, I think I can go with my own helper function

// Basic Mo function for single argument (it should be not needed if Map follow the same signature as the method)
func Mo[A any, B any](f func(A) (B, error)) func(mo.Result[A]) mo.Result[B] {
	return func(r mo.Result[A]) mo.Result[B] {
		if r.IsError() {
			return mo.Err[B](r.Error())
		}
		result, err := f(r.MustGet())
		if err != nil {
			return mo.Err[B](err)
		}
		return mo.Ok(result)
	}
}

// Mo function with context - context is passed through the pipeline
func MoCtx[A any, B any](ctx context.Context, f func(context.Context, A) (B, error)) func(mo.Result[A]) mo.Result[B] {
	return func(r mo.Result[A]) mo.Result[B] {
		if r.IsError() {
			return mo.Err[B](r.Error())
		}
		result, err := f(ctx, r.MustGet())
		if err != nil {
			return mo.Err[B](err)
		}
		return mo.Ok(result)
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants