diff --git a/quickcheck/generator.mbt b/quickcheck/generator.mbt index 715b40c92..01b11b49c 100644 --- a/quickcheck/generator.mbt +++ b/quickcheck/generator.mbt @@ -125,7 +125,10 @@ pub fn[T, U, V] Generator::zip_with( other : Generator[U], combine : (T, U) -> V, ) -> Generator[V] { - self.flat_map(first => other.map(second => combine(first, second))) + Generator((size, state) => { + let other_state = state.split() + combine(self.run(size, state), other.run(size, other_state)) + }) } ///| @@ -136,7 +139,15 @@ pub fn[T, U, V, W] Generator::zip_with3( third : Generator[V], combine : (T, U, V) -> W, ) -> Generator[W] { - self.flat_map(a => second.flat_map(b => third.map(c => combine(a, b, c)))) + Generator((size, state) => { + let second_state = state.split() + let third_state = second_state.split() + combine( + self.run(size, state), + second.run(size, second_state), + third.run(size, third_state), + ) + }) } ///| diff --git a/quickcheck/generator_bench_test.mbt b/quickcheck/generator_bench_test.mbt new file mode 100644 index 000000000..1f92d9296 --- /dev/null +++ b/quickcheck/generator_bench_test.mbt @@ -0,0 +1,29 @@ +// Copyright 2026 International Digital Economy Academy +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +///| +test "bench Generator::zip_with n=1000000" (it : @bench.T) { + let generator = @quickcheck.int_range(0, 1000).zip_with( + @quickcheck.int_range(0, 1000), + (a, b) => a + b, + ) + it.bench(fn() { + let state = @splitmix.new() + let mut sum = 0 + for _ in 0..<1000000 { + sum += generator.run(100, state) + } + it.keep(sum) + }) +}