Add Fold function to Result monad#42
Conversation
|
I would use the same prototype as other methods: Feel free to add this method to the other types. Or I will do it by myself. |
|
@samber Okay I think you are asking to have something like that func (r Result[T]) Fold(successFunc func(T) T, failureFunc func(error) T) T {
if r.err != nil {
return failureFunc(r.err)
}
return successFunc(r.value)
}However, I was thinking that the Fold function could map the result to a new type. This would allow the client (AKA the caller) to use custom functions that return any value based on their specific logic needs. For example, if the result monad encounters an error, instead of returning a fallback integer value, I would like to return a string containing an error message. So what do you think about that |
|
To date, Go does not allow type parameters on methods: golang/go#49085 . I don't really like returning interfaces, since it is not very typesafe. In your case, I would suggest an additional helper: IMO type foldable interface[T, U] {
left() T
right() U
} |
|
@samber Here are the changes. Are you okay with these changes before implementing the Foldable interface in the remaining types (Either & Option)? Also, I have a question: I've noticed different implementations of the fold function in various languages/packages. For instance, for the Result<TValue, TError> monad, the fold function always returns a value of type TValue, as you mentioned here. However, in other packages, I see that the fold function returns a new type. So, I'm unsure which approach makes more sense. Should we make the fold function consistently return the same type |
|
Looks good. I think we can return a different type in the case of |
| - uses: actions/checkout@v2 | ||
| - name: golangci-lint | ||
| uses: golangci/golangci-lint-action@v3 | ||
| uses: golangci/golangci-lint-action@v5 |
There was a problem hiding this comment.
Update to newer version as I got the following linting error
func Result[T].{{func_name}} is unused (unused)
regarding those functions
|
@samber I have changed method names for Foldable interface to make sure method names will not conflict with field names in the Either struct. Are you okay with that 👀 |
|
Looks good! Let's merge ;) |
This pull request introduces
Foldfunction to theResultmonad, enhancing its functionality and making it more versatile for handling both success and failure scenarios.Changes Made:
Foldfunction within theResultmonad, allowing for streamlined handling of success and failure cases.Foldfunction takes two callback functions as arguments:successFunc: A function to be applied if theResultcontains a successful value. It transforms the value into another type.failureFunc: A function to be applied if theResultcontains an error. It transforms the error into another type.Resultcontains an error, theFoldfunction applies thefailureFuncto the error and returns the transformed result.Resultcontains a successful value, theFoldfunction applies thesuccessFuncto the value and returns the transformed result.Benefits:
Resultmonad.Resultmonad by enabling developers to easily define custom behavior for different scenarios.Resultmonad itself.Example: