Repository navigation
Expand file tree
/
Copy pathops.go
More file actions
74 lines (67 loc) · 1.87 KB
/
Copy pathops.go
File metadata and controls
74 lines (67 loc) · 1.87 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
package flow
import "context"
// Map builds a Processor from a plain transform. Use TryMap if it can fail.
func Map[I, O any](fn func(I) O) Processor[I, O] {
return ProcessorFunc[I, O](func(ctx context.Context, in I, emit func(O) error) error {
return emit(fn(in))
})
}
// TryMap builds a Processor from a fallible transform; a returned error aborts
// the pipeline (fail-fast).
func TryMap[I, O any](fn func(I) (O, error)) Processor[I, O] {
return ProcessorFunc[I, O](func(ctx context.Context, in I, emit func(O) error) error {
o, err := fn(in)
if err != nil {
return err
}
return emit(o)
})
}
// Filter builds a Processor that keeps values for which pred is true.
func Filter[T any](pred func(T) bool) Processor[T, T] {
return ProcessorFunc[T, T](func(ctx context.Context, in T, emit func(T) error) error {
if pred(in) {
return emit(in)
}
return nil
})
}
// TryFilter is Filter with a fallible predicate.
func TryFilter[T any](pred func(T) (bool, error)) Processor[T, T] {
return ProcessorFunc[T, T](func(ctx context.Context, in T, emit func(T) error) error {
ok, err := pred(in)
if err != nil {
return err
}
if ok {
return emit(in)
}
return nil
})
}
// FlatMap builds a Processor that expands each value into zero or more outputs.
func FlatMap[I, O any](fn func(I) []O) Processor[I, O] {
return ProcessorFunc[I, O](func(ctx context.Context, in I, emit func(O) error) error {
for _, o := range fn(in) {
if err := emit(o); err != nil {
return err
}
}
return nil
})
}
// TryFlatMap is FlatMap with a fallible expansion.
func TryFlatMap[I, O any](fn func(I) ([]O, error)) Processor[I, O] {
return ProcessorFunc[I, O](func(ctx context.Context, in I, emit func(O) error) error {
outs, err := fn(in)
if err != nil {
return err
}
for _, o := range outs {
if err := emit(o); err != nil {
return err
}
}
return nil
})
}