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_flatten_example_test.go
More file actions
102 lines (85 loc) · 2.98 KB
/
4_flatten_example_test.go
File metadata and controls
102 lines (85 loc) · 2.98 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
94
95
96
97
98
99
100
101
102
package mu_test
import (
"errors"
"fmt"
"github.com/appthrust/mu"
)
func ExampleOption_Flatten() {
// Note: Flatten works on properly nested Option types through type assertion
// Create a nested Option[Option[int]] scenario
innerSome := mu.Some(42)
outerSome := mu.Some(innerSome)
// For demonstration, we use the Flatten method which attempts type assertion
result := outerSome.Flatten()
fmt.Println("Nested Some contains value:", result.IsSome())
// Example with None inner value
innerNone := mu.None[int]()
outerWithNone := mu.Some(innerNone)
result2 := outerWithNone.Flatten()
fmt.Println("Nested with inner None:", result2.IsNone())
// Example with outer None
outerNone := mu.None[mu.Option[int]]()
result3 := outerNone.Flatten()
fmt.Println("Outer None flattened:", result3.IsNone())
// Output:
// Nested Some contains value: true
// Nested with inner None: false
// Outer None flattened: true
}
func ExampleEither_Flatten() {
// Note: Flatten works on properly nested Either types through type assertion
// Create nested Either scenario
innerRight := mu.Right[string, int](42)
outerRight := mu.Right[string, mu.Either[string, int]](innerRight)
result := outerRight.Flatten()
fmt.Println("Nested Right contains value:", result.IsRight())
fmt.Println("Value:", result.OrZero())
// Example with inner Left
innerLeft := mu.Left[string, int]("inner error")
outerWithLeft := mu.Right[string, mu.Either[string, int]](innerLeft)
result2 := outerWithLeft.Flatten()
fmt.Println("Nested with inner Left:", result2.IsLeft())
fmt.Printf("Left value:%s\n", result2.LeftOrZero())
// Example with outer Left
outerLeft := mu.Left[string, mu.Either[string, int]]("outer error")
result3 := outerLeft.Flatten()
fmt.Println("Outer Left flattened:", result3.IsLeft())
fmt.Println("Left value:", result3.LeftOrZero())
// Output:
// Nested Right contains value: true
// Value: { 42 false}
// Nested with inner Left: false
// Left value:
// Outer Left flattened: true
// Left value: outer error
}
func ExampleResult_Flatten() {
// Note: Flatten works on properly nested Result types through type assertion
// Create nested Result scenario
innerOk := mu.Ok(42)
outerOk := mu.Ok(innerOk)
result := outerOk.Flatten()
fmt.Println("Nested Ok contains value:", result.IsOk())
fmt.Println("Value:", result.OrZero())
// Example with inner Err
innerErr := mu.Err[int](errors.New("inner error"))
outerWithErr := mu.Ok(innerErr)
result2 := outerWithErr.Flatten()
fmt.Println("Nested with inner Err:", result2.IsErr())
if result2.IsErr() {
fmt.Println("Error:", result2.ErrOrZero().Error())
}
// Example with outer Err
outerErr := mu.Err[mu.Result[int]](errors.New("outer error"))
result3 := outerErr.Flatten()
fmt.Println("Outer Err flattened:", result3.IsErr())
if result3.IsErr() {
fmt.Println("Error:", result3.ErrOrZero().Error())
}
// Output:
// Nested Ok contains value: true
// Value: {42 <nil>}
// Nested with inner Err: false
// Outer Err flattened: true
// Error: outer error
}