-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
137 lines (113 loc) · 3.52 KB
/
example_test.go
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package kytsya
import (
"errors"
"fmt"
"strconv"
"testing"
)
// Need to run a goroutines? No problem!
func TestRunGoroutines(t *testing.T) {
/////////////////////////////////////////////////////////////////
// Need to run a singe goroutine?
Goroutine().Spawn(func() { fmt.Println("🐱") })
/////////////////////////////////////////////////////////////////
// Add recovery handler? Stack trace will be printed to stdout
Goroutine().
WithRecover().
Spawn(func() { fmt.Println("🐱") })
/////////////////////////////////////////////////////////////////
// Wait until it's done?
Goroutine().
WithRecover().
WithWaitGroup().
Spawn(func() { fmt.Println("🐱") }).Wait()
}
// Need to run goroutine that return a result/content/data? No problems!
func TestRunErroutine(t *testing.T) {
res := <-Erroutine[string]().Spawn(func() Result[string] {
return Result[string]{Data: "🐱"}
}).WaitAsync()
fmt.Println(res)
/////////////////////////////////////////////////////////////////
// There are few goroutines we need to run? Cool!
box := NewErrorBox[string]()
ForEach([]int{1, 2, 3}, func(i int, val int) {
box.AddTask(func() Result[string] {
return Result[string]{Data: strconv.Itoa(val)}
})
})
ForChan(box.Run(), func(val Result[string]) {
fmt.Println(val.Data, val.Err)
})
/////////////////////////////////////////////////////////////////
// Also could be "one liner"
resCh := NewErrorBox[string]().
AddTask(func() Result[string] {
return Result[string]{Data: strconv.Itoa(1)}
}).
AddTask(func() Result[string] {
return Result[string]{Data: strconv.Itoa(2)}
}).
AddTask(func() Result[string] {
return Result[string]{Data: strconv.Itoa(3)}
}).Run()
ForChan(resCh, func(val Result[string]) {
fmt.Println(val.Data, val.Err)
})
/////////////////////////////////////////////////////////////////
// Also! Could be an EachRunner!
resCh = NewEachRunner[int, string]([]int{1, 2, 3}).Handle(func(val int) Result[string] {
return Result[string]{Data: strconv.Itoa(val)}
})
ForChan(resCh, func(val Result[string]) {
fmt.Println(val.Data, val.Err)
})
/////////////////////////////////////////////////////////////////
// Feel some possibility to have a panic? Easy!
resCh = NewEachRunner[int, string]([]int{1, 2, 3}).
WithRecover().
Handle(func(val int) Result[string] {
if val == 2 {
panic("Houston, we have a problem")
}
return Result[string]{Data: strconv.Itoa(val)}
})
ForChan(resCh, func(val Result[string]) {
fmt.Println(val.Data, val.Err) //<----- panic stacktrace will be returned as an error!
})
}
// Needs to make gophers life a bit "functional"?
func TestFunctionalEra(t *testing.T) {
// Range it!
ForEach([]int{1, 2, 3, 4, 5, 6}, func(i, val int) {
fmt.Printf("index: %d value: %d", i, val)
})
// Filter it!
// output: [2 4 6]
fmt.Println(Filter([]int{1, 2, 3, 4, 5, 6}, func(i, val int) bool {
return val%2 == 0
}))
// Map it!
resMap := Map([]int{1, 2, 3, 4, 5, 6}, func(i, val int) string {
return strconv.Itoa(val)
})
// output: [1 2 3 4 5 6] as an array of string
fmt.Println(resMap)
// Reduce it!
// output: 21
fmt.Println(Reduce([]int{1, 2, 3, 4, 5, 6}, func(val, acc int) int {
return val + acc
}))
}
func TestMapErr(t *testing.T) {
res, err := MapErr([]int{1, 2, 3, 4, 5}, func(i, val int) (string, error) {
if val == 3 {
return "", errors.New("it's a 3!!!")
}
return strconv.Itoa(val), nil
})
if err != nil {
//panic(err) could be here
}
fmt.Println(res) // result will print empty slice while we got an error during the iteration.
}