Skip to content

Commit

Permalink
Add Fold testable example
Browse files Browse the repository at this point in the history
  • Loading branch information
taman9333 committed Jun 22, 2024
1 parent 1c2de46 commit 85daf42
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
25 changes: 25 additions & 0 deletions fold_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mo

import (
"errors"
"fmt"
)

func ExampleFold() {
res1 := Result[int]{value: 42, isErr: false, err: nil}
res2 := Result[int]{value: 0, isErr: true, err: errors.New("error")}

successFunc := func(val int) string {
return fmt.Sprintf("Success with value %d", val)
}

failureFunc := func(err error) string {
return fmt.Sprintf("Failure with error %s", err)
}

fmt.Println(Fold[error, int, string](res1, successFunc, failureFunc))
fmt.Println(Fold[error, int, string](res2, successFunc, failureFunc))
// Output:
// Success with value 42
// Failure with error error
}
10 changes: 0 additions & 10 deletions result_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,3 @@ func ExampleResult_FlatMap_err() {
fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
// Output: true 0 error
}

func exampleResult_Fold() {
res := Result[int]{value: 42, err: nil}
foldResult := Fold[error, int, string](res, func(v int) string {
return "Success"
}, func(_ error) string {
return "Failure"
})
println(foldResult)
}

0 comments on commit 85daf42

Please sign in to comment.