This repository was archived by the owner on Aug 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_map_example_test.go
More file actions
93 lines (76 loc) · 2.38 KB
/
4_map_example_test.go
File metadata and controls
93 lines (76 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package mu_test
import (
"errors"
"fmt"
"strings"
"github.com/appthrust/mu"
)
func ExampleOption_Map() {
// Map with Some value
some42 := mu.Some(42)
mapped := some42.Map(func(x int) int { return x * 2 })
fmt.Println("Some(42).Map(x*2):", mapped.OrZero())
// Map with None value
none := mu.None[int]()
mapped2 := none.Map(func(x int) int { return x * 2 })
fmt.Println("None.Map(x*2):", mapped2.IsNone())
// Output:
// Some(42).Map(x*2): 84
// None.Map(x*2): true
}
func ExampleEither_MapLeft() {
// MapLeft with Left value
left := mu.Left[string, int]("error")
mapped := left.MapLeft(func(s string) string { return strings.ToUpper(s) })
fmt.Println("Left(\"error\").MapLeft(upper):", mapped.LeftOrZero())
// MapLeft with Right value
right := mu.Right[string, int](42)
mapped2 := right.MapLeft(func(s string) string { return strings.ToUpper(s) })
fmt.Println("Right(42).MapLeft(upper):", mapped2.OrZero())
// Output:
// Left("error").MapLeft(upper): ERROR
// Right(42).MapLeft(upper): 42
}
func ExampleEither_Map() {
// Map with Right value
right := mu.Right[string, int](42)
mapped := right.Map(func(x int) int { return x * 2 })
fmt.Println("Right(42).Map(x*2):", mapped.OrZero())
// Map with Left value
left := mu.Left[string, int]("error")
mapped2 := left.Map(func(x int) int { return x * 2 })
fmt.Println("Left(\"error\").Map(x*2):", mapped2.LeftOrZero())
// Output:
// Right(42).Map(x*2): 84
// Left("error").Map(x*2): error
}
func ExampleResult_Map() {
// Map with Ok value
ok := mu.Ok(42)
mapped := ok.Map(func(x int) int { return x * 2 })
fmt.Println("Ok(42).Map(x*2):", mapped.OrZero())
// Map with Err value
err := mu.Err[int](errors.New("error"))
mapped2 := err.Map(func(x int) int { return x * 2 })
fmt.Println("Err.Map(x*2):", mapped2.ErrOrZero().Error())
// Output:
// Ok(42).Map(x*2): 84
// Err.Map(x*2): error
}
func ExampleResult_MapErr() {
// MapErr with Err value
err := mu.Err[int](errors.New("connection failed"))
mapped := err.MapErr(func(e error) error {
return errors.New("wrapped: " + e.Error())
})
fmt.Println("Err.MapErr(wrap):", mapped.ErrOrZero().Error())
// MapErr with Ok value
ok := mu.Ok(42)
mapped2 := ok.MapErr(func(e error) error {
return errors.New("wrapped: " + e.Error())
})
fmt.Println("Ok(42).MapErr(wrap):", mapped2.OrZero())
// Output:
// Err.MapErr(wrap): wrapped: connection failed
// Ok(42).MapErr(wrap): 42
}