-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from taman9333/fold-result
Add Fold function to Result monad
- Loading branch information
Showing
9 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package mo | ||
|
||
// Foldable represents a type that can be folded into a single value | ||
// based on its state. | ||
// | ||
// - T: the type of the value in the failure state (e.g., an error type). | ||
// - U: the type of the value in the success state. | ||
type Foldable[T any, U any] interface { | ||
leftValue() T | ||
rightValue() U | ||
hasLeftValue() bool | ||
} | ||
|
||
// Fold applies one of the two functions based on the state of the Foldable type, | ||
// and it returns the result of applying either successFunc or failureFunc. | ||
// | ||
// - T: the type of the failure value (e.g., an error type) | ||
// - U: the type of the success value | ||
// - R: the type of the return value from the folding functions | ||
// | ||
// successFunc is applied when the Foldable is in the success state (i.e., isLeft() is false). | ||
// failureFunc is applied when the Foldable is in the failure state (i.e., isLeft() is true). | ||
func Fold[T, U, R any](f Foldable[T, U], successFunc func(U) R, failureFunc func(T) R) R { | ||
if f.hasLeftValue() { | ||
return failureFunc(f.leftValue()) | ||
} | ||
return successFunc(f.rightValue()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters