forked from earthboundkid/flowmatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
63 lines (58 loc) · 1.38 KB
/
map.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
package flowmatic
import (
"context"
)
// Map starts numWorkers concurrent workers (or GOMAXPROCS workers if numWorkers < 1)
// and attempts to map the input slice to an output slice.
// Each task receives a child context.
// The first error or panic returned by a task
// cancels the child context
// and halts further task scheduling.
// If a task panics during execution,
// the panic will be caught and rethrown in the parent Goroutine.
func Map[Input, Output any](ctx context.Context, numWorkers int, items []Input, task func(context.Context, Input) (Output, error)) (results []Output, err error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
inch, ouch := TaskPool(numWorkers, func(pos int) (Output, error) {
item := items[pos]
return task(ctx, item)
})
var panicVal any
n := 0
closeinch := false
results = make([]Output, len(items))
for {
if n >= len(items) {
closeinch = true
}
if closeinch && inch != nil {
close(inch)
inch = nil
}
select {
case inch <- n:
n++
case r, ok := <-ouch:
if !ok {
if panicVal != nil {
panic(panicVal)
}
if err != nil {
return nil, err
}
return results, nil
}
if r.Err != nil && err == nil {
cancel()
closeinch = true
err = r.Err
}
if r.Panic != nil && panicVal == nil {
cancel()
closeinch = true
panicVal = r.Panic
}
results[r.In] = r.Out
}
}
}