-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen-chains
executable file
·74 lines (56 loc) · 1.34 KB
/
gen-chains
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
#!/bin/env bash
set -e -o pipefail
# max number of pipeline stages
readonly N=16
# parameter check
if [ -z "$1" ]
then
echo >&2 "[error] $(basename "$0"): file name is not specified"
exit 1
fi
# redirect
exec 1>"$1"
# header
echo "// Code generated by $(basename "$0") tool. DO NOT EDIT.
package $GOPACKAGE"
# Chain2
echo '// Chain2 composes 2 pipeline stages into one.
func Chain2[A, B, C any](s1 Stage[A, B], s2 Stage[B, C]) Stage[A, C] {
return func(src Gen[A], yield func(C) error) error {
return s2(Bind(src, s1), yield)
}
}
'
readonly letters='ABCDEFGHIJKLMOPQRSTUVWXYZ'
for (( i=3; i<=N; i++ ))
do
# comment
echo "// Chain${i} composes ${i} pipeline stages into one."
# function signature start
echo -n "func Chain${i}[A, B, C"
# template parameter list
for (( j=3; j<=i; j++ ))
do
echo -n ", ${letters:j:1}"
done
# function parameters start
echo -n " any](s1 Stage[A, B], s2 Stage[B, C]"
# function parameters
for (( j=3; j<=i; j++ ))
do
echo -n ", s${j} Stage[${letters:((j-1)):1}, ${letters:j:1}]"
done
# end of function signature
echo ") Stage[A, ${letters:i:1}] {"
# implementation start
echo -n -e "\treturn Chain2(Chain$((i-1))(s1, s2"
# implementation chain
for (( j=3; j<i; j++ ))
do
echo -n ", s${j}"
done
# implementation end
echo -e "), s${i})\n}"
done
# and finally
goimports -w "$1" >/dev/null