-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.go.template
More file actions
128 lines (107 loc) · 2.92 KB
/
parallel.go.template
File metadata and controls
128 lines (107 loc) · 2.92 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
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
// vim: ft=go
// parallel_template.go
// Pipe mode with concurrent goroutine processing.
// Magic variables available in the loop body:
// x / line - current line (string)
// i / idx - loop index (int)
// f / fields - split fields of x ([]string)
// Always available:
// lines - all stdin lines ([]string)
//
// NOTE: output order is NOT guaranteed — lines may interleave.
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"sync"
)
// ---- helpers ----
// die prints err to stderr and exits 1. No-op if err is nil.
func die(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}
// must unwraps a (value, error) pair, calling die on error.
//
// n, err := strconv.Atoi(s) → n := must(strconv.Atoi(s))
func must[T any](v T, err error) T {
die(err)
return v
}
// atoi parses s as an int. Exits on failure.
func atoi(s string) int { return must(strconv.Atoi(strings.TrimSpace(s))) }
// atof parses s as a float64. Exits on failure.
func atof(s string) float64 { return must(strconv.ParseFloat(strings.TrimSpace(s), 64)) }
// pp returns v formatted as indented JSON (falls back to %+v on error).
// Used with auto-print: pp(myMap) prints the JSON automatically.
func pp(v any) string {
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
return fmt.Sprintf("%+v", v)
}
return string(b)
}
// trim returns strings.TrimSpace(s).
func trim(s string) string { return strings.TrimSpace(s) }
// splitlines splits a multi-line string into a slice, dropping the trailing newline.
func splitlines(s string) []string { return strings.Split(strings.TrimRight(s, "\n"), "\n") }
var _reCache sync.Map
func _reCached(pat string) *regexp.Regexp {
if v, ok := _reCache.Load(pat); ok {
return v.(*regexp.Regexp)
}
r := regexp.MustCompile(pat)
_reCache.Store(pat, r)
return r
}
// match returns submatches: [0]=full match, [1+]=capture groups. nil if no match.
func match(pat, s string) []string { return _reCached(pat).FindStringSubmatch(s) }
// named returns named capture groups as a map. nil if no match.
func named(pat, s string) map[string]string {
r := _reCached(pat)
m := r.FindStringSubmatch(s)
if m == nil {
return nil
}
result := map[string]string{}
for i, name := range r.SubexpNames() {
if name != "" && i < len(m) {
result[name] = m[i]
}
}
return result
}
// ---- end helpers ----
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Buffer(make([]byte, 1024*1024), 1024*1024)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
_ = lines
// {{PIPE_BEFORE}}
var wg sync.WaitGroup
sem := make(chan struct{}, {{PARALLEL_N}})
for i, x := range lines {
wg.Add(1)
sem <- struct{}{}
go func(i int, x string) {
defer wg.Done()
defer func() { <-sem }()
line := x
idx := i
f := {{FIELDS_EXPR}}
fields := f
_ = line; _ = idx; _ = f; _ = fields
// {{PIPE_BODY}}
}(i, x)
}
wg.Wait()
}