On Sat, 15 May 2021 at 16:02, Gilles Sadowski <[email protected]> wrote:

> Hi Alex.
>
> Would the proposal be any different if with Java 8+ features?
> [IOW, is it still useful (in any sense) to stick with Java 6?]
>

The Sampler<T> interface can be replaced with Supplier<T>. But then all the
sampling methods would have to be renamed to 'get()' as opposed to
'sample()' which we currently have in the codebase.

The interface BiFunction<T, U, R> can be used to specify the factory to
build the discrete sampler:

BiFunction<double[], UniformRandomProvider, DiscreteSampler>

I do not think any other Java 8 features would help reduce the new amount
of public API.

With Java 8 you could build a composite with streams. We would provide
access to the pair used to stored the sampler and the weights:

static class WeightedSampler<S> {
            /** The weight. */
            private final double weight;
            /** The sampler. */
            private final S sampler;
            // etc...
}

You can then use streams to build a composite sampler from a source of
objects:

List<Triangle> triangles = ...;
UniformRandomProvider rng = ...;
ObjectSampler<double[]> surface = triangles.stream().map(t -> {
    double area = t.getArea();
    double[] a = t.getVertex(0);
    double[] b = t.getVertex(1);
    double[] c = t.getVertex(2);
    return WeightedSample.of(area, TriangleSampler.of(a, b, c, rng));
}).collect(CompositeCollectors.toObjectSampler(rng));
double[] point = surface.sample();

The CompositeCollectors would amass the WeightedSamplers and construct the
composite. The class would still require 6 ways of calling it so that the 6
composite variants can be created.

This is just an idea. I do not think Java 8 is really a benefit and can be
added later.

Alex

Reply via email to