-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
52 lines (40 loc) · 905 Bytes
/
Copy pathmain.go
File metadata and controls
52 lines (40 loc) · 905 Bytes
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
package main
import (
"context"
"fmt"
"mpragliola/pype/pkg/pipe"
"time"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
a := pipe.Of([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
b := pipe.Filter(ctx, evenFilter, a)
c, _ := pipe.Map(ctx, squareFilter, b)
d, errCh := pipe.Map(ctx, hate64function, c)
e, _ := pipe.Map(ctx, slowMap, d)
okFn := func(i int) { fmt.Println("-> ", i) }
errFn := func(err error) {
fmt.Println("ERROR", err)
}
pipe.Consume(ctx, e, errCh, okFn, errFn)
}
// ---
func evenFilter(i int) bool {
return i%2 == 0
}
func squareFilter(i int) (int, error) {
return i * i, nil
}
func slowMap(i int) (int, error) {
fmt.Println("slow map", i)
// Simulate a slow operation
time.Sleep(1 * time.Second)
return i, nil
}
func hate64function(i int) (int, error) {
if i == 1 {
return 0, fmt.Errorf("i hate 64")
}
return i, nil
}