Repository navigation
Expand file tree
/
Copy pathgenerate.py
More file actions
213 lines (194 loc) · 10.1 KB
/
Copy pathgenerate.py
File metadata and controls
213 lines (194 loc) · 10.1 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import jinja2
environment = jinja2.Environment()
finisher_template = environment.from_string("""/*
* Copyright (c) 2025-2026, Antonio Gabriel Muñoz Conejo <me at tonivade dot es>
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.diesel.function;
// generated code
@FunctionalInterface
public interface Finisher{{ value }}<{% for i in range(value) %}T{{ i }}, {% endfor %}R> {
R apply({% for i in range(value) %}T{{ i }} t{{ i }}{% if i < value - 1 %}, {% endif %}{% endfor %});
static <{% for i in range(value) %}T{{ i }}{% if i < value - 1 %}, {% endif %}{% endfor %}> Finisher{{ value }}<{% for i in range(value) %}T{{ i }}, {% endfor %}T0> first() {
return (t0{% for i in range(value - 1) %}, _{% endfor %}) -> t0;
}
static <{% for i in range(value) %}T{{ i }}{% if i < value - 1 %}, {% endif %}{% endfor %}> Finisher{{ value }}<{% for i in range(value) %}T{{ i }}, {% endfor %}T{{ value -1 }}> last() {
return ({% for i in range(value - 1) %}_, {% endfor %}t{{ value - 1}}) -> t{{ value - 1}};
}
}
""")
result_zip_template = environment.from_string("""
/**
* Combines the given results using the finisher function if all of them are successful.
* <p>
* If any of the results is a failure, the first failure by position is returned.
*
{% for i in range(value) %} * @param r{{ i }} a result to combine
{% endfor %} * @param finisher the function used to combine the results
* @param <F> the type of the failure
{% for i in range(value) %} * @param <T{{ i }}> the success type of {@code r{{ i }}}
{% endfor %} * @param <R> the type of the combined result
* @return a new result with the combined result, or the first failure
*/
static <F, {% for i in range(value) %}T{{ i }}, {% endfor %}R> Result<F, R> zip(
{% for i in range(value) %} Result<F, T{{ i }}> r{{ i }},
{% endfor %} Finisher{{ value }}<{% for i in range(value) %}T{{ i }}, {% endfor %}R> finisher) {
return {% for i in range(value - 1) %}r{{ i }}.flatMap(_{{ i }} ->
{% endfor %}
r{{ value - 1 }}.map(_{{ value -1 }} -> finisher.apply({% for i in range(value) %}_{{ i }}{% if i < value - 1 %}, {% endif %}{% endfor %}))
{% for i in range(value - 1) %}){% endfor %};
}
""")
program_zip_template = environment.from_string("""
/**
* Executes the given programs sequentially and combines their results using the finisher function.
* <p>
* The execution stops at the first program that fails, and the resulting program fails with
* that error. The programs after it are not executed.
*
{% for i in range(value) %} * @param p{{ i }} a program to combine
{% endfor %} * @param finisher the function used to combine the results
* @param <S> the type of the state
* @param <E> the type of the error
{% for i in range(value) %} * @param <T{{ i }}> the success type of {@code p{{ i }}}
{% endfor %} * @param <R> the type of the combined result
* @return a new program with the combined result, or the first failure
*/
public static <S, E, {% for i in range(value) %}T{{ i }}, {% endfor %}R> Program<S, E, R> zip(
{% for i in range(value) %} Program<S, E, T{{ i }}> p{{ i }},
{% endfor %} Finisher{{ value }}<{% for i in range(value) %}T{{ i }}, {% endfor %}R> finisher) {
return {% for i in range(value - 1) %}p{{ i }}.flatMap(_{{ i }} ->
{% endfor %}
p{{ value - 1 }}.map(_{{ value - 1 }} -> finisher.apply({% for i in range(value) %}_{{ i }}{% if i < value - 1 %}, {% endif %}{% endfor %}))
{% for i in range(value - 1) %}){% endfor %};
}
""")
program_parzip_template = environment.from_string("""
/**
* Executes the given programs in parallel using the provided executor and combines their results
* using the finisher function.
*
* <p>
* The execution is fail-fast: as soon as any program fails, the remaining programs are
* cancelled and the resulting program fails with that error. If several programs fail, the
* error returned is the first one to happen in time, not the first by position. Cancelled
* programs stop at their next step and run their finalizers, and the resulting program
* completes once they have stopped. Cancellation is cooperative, so code that is already
* running, like a blocking call inside {@code supply} or {@code task}, finishes first.
*
{% for i in range(value) %} * @param p{{ i }} a program to be executed in parallel
{% endfor %} * @param finisher the function used to combine the results
* @param executor the executor used to execute the programs in parallel
* @param <S> the type of the state
* @param <E> the type of the error
{% for i in range(value) %} * @param <T{{ i }}> the result type of {@code p{{ i }}}
{% endfor %} * @param <R> the type of the combined result
* @return a new program representing the parallel computation
*/
public static <S, E, {% for i in range(value) %}T{{ i }}, {% endfor %}R> Program<S, E, R> parZip(
{% for i in range(value) %} Program<S, E, T{{ i }}> p{{ i }},
{% endfor %} Finisher{{ value }}<{% for i in range(value) %}T{{ i }}, {% endfor %}R> finisher,
Executor executor) {
return zip(
{% for i in range(value) %} p{{ i }}.fork(executor),
{% endfor %} ({% for i in range(value) %}f{{ i }}{% if i < value - 1 %}, {% endif %}{% endfor %}) ->
Fiber.<S, E, R>joinAll(
List.of({% for i in range(value) %}f{{ i }}{% if i < value - 1 %}, {% endif %}{% endfor %}),
() -> Result.zip({% for i in range(value) %}f{{ i }}.resultNow(), {% endfor %}finisher)))
.flatMap(identity());
}
""")
program_parzip_default_template = environment.from_string("""
/**
* Executes the given programs in parallel using virtual threads and combines their results
* using the finisher function.
*
* <p>
* The execution is fail-fast: as soon as any program fails, the remaining programs are
* cancelled and the resulting program fails with that error. If several programs fail, the
* error returned is the first one to happen in time, not the first by position. Cancelled
* programs stop at their next step and run their finalizers, and the resulting program
* completes once they have stopped. Cancellation is cooperative, so code that is already
* running, like a blocking call inside {@code supply} or {@code task}, finishes first.
*
{% for i in range(value) %} * @param p{{ i }} a program to be executed in parallel
{% endfor %} * @param finisher the function used to combine the results
* @param <S> the type of the state
* @param <E> the type of the error
{% for i in range(value) %} * @param <T{{ i }}> the result type of {@code p{{ i }}}
{% endfor %} * @param <R> the type of the combined result
* @return a new program representing the parallel computation
*/
public static <S, E, {% for i in range(value) %}T{{ i }}, {% endfor %}R> Program<S, E, R> parZip(
{% for i in range(value) %} Program<S, E, T{{ i }}> p{{ i }},
{% endfor %} Finisher{{ value }}<{% for i in range(value) %}T{{ i }}, {% endfor %}R> finisher) {
return parZip({% for i in range(value) %}p{{ i }}, {% endfor %}finisher, DEFAULT_EXECUTOR);
}
""")
program_pipe_template = environment.from_string("""
/**
* Executes the given program and passes its result to the next function, which returns
* the next program to execute, and so on, returning the result of the last one.
* <p>
* Equivalent to chaining {@code flatMap} calls. The execution stops at the first program that
* fails, and the resulting program fails with that error.
*
* @param p0 the program to execute first
{% for i in range(value - 1) %} * @param p{{ i + 1 }} the function that receives the result of the previous step and returns the next program
{% endfor %} * @param <S> the type of the state
* @param <E> the type of the error
* @param <T0> the result type of {@code p0}
{% for i in range(value - 1) %} * @param <T{{ i + 1 }}> the result type of step {@code p{{ i + 1 }}}
{% endfor %} * @return a new program with the result of the last step
*/
public static <S, E, {% for i in range(value) %}T{{ i }}{% if i < value - 1 %}, {% endif %}{% endfor %}> Program<S, E, T{{ value - 1}}> pipe(
Program<S, E, T0> p0,
{% for i in range(value - 1) %} Function<? super T{{ i }}, ? extends Program<S, E, T{{ i + 1 }}>> p{{ i + 1 }}{% if i < value - 2 %},{% endif %}
{% endfor %}) {
return p0{% for i in range(value - 1) %}.flatMap(p{{ i + 1}}){% endfor %};
}
""")
program_chain_template = environment.from_string("""
/**
* Executes the given program and transforms its result by applying the given functions
* in order, each one receiving the result of the previous one.
* <p>
* Equivalent to chaining {@code map} calls. If the program fails, the functions are not applied
* and the resulting program fails with that error.
*
* @param p0 the program to execute first
{% for i in range(value - 1) %} * @param p{{ i + 1 }} the function that transforms the result of the previous step
{% endfor %} * @param <S> the type of the state
* @param <E> the type of the error
* @param <T0> the result type of {@code p0}
{% for i in range(value - 1) %} * @param <T{{ i + 1 }}> the result type of step {@code p{{ i + 1 }}}
{% endfor %} * @return a new program with the result of the last step
*/
public static <S, E, {% for i in range(value) %}T{{ i }}{% if i < value - 1 %}, {% endif %}{% endfor %}> Program<S, E, T{{ value - 1}}> chain(
Program<S, E, T0> p0,
{% for i in range(value - 1) %} Function<? super T{{ i }}, ? extends T{{ i + 1 }}> p{{ i + 1 }}{% if i < value - 2 %},{% endif %}
{% endfor %}) {
return p0{% for i in range(value - 1) %}.map(p{{ i + 1}}){% endfor %};
}
""")
for i in range(2, 10):
with open(f"src/main/java/com/github/tonivade/diesel/function/Finisher{i}.java", 'w') as file:
file.write(finisher_template.render(value=i))
print(">>>> result zip")
for i in range(2, 10):
print(result_zip_template.render(value=i))
print(">>>> Combine zip")
for i in range(2, 10):
print(program_zip_template.render(value=i))
print(">>>> Concurrent parzip")
for i in range(2, 10):
print(program_parzip_template.render(value=i))
print(">>>> Concurrent parzip default executor")
for i in range(2, 10):
print(program_parzip_default_template.render(value=i))
print(">>>> Combine pipe")
for i in range(2, 10):
print(program_pipe_template.render(value=i))
print(">>>> Combine chain")
for i in range(2, 10):
print(program_chain_template.render(value=i))