Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Not short cutting 'count' Stream collector #955

@jenetics

Description

@jenetics

The Stream.count() terminal Stream operation might shortcut the evaluation of the Stream, which might not be desirable in certain conditions.

@Override
protected int crossover(final MSeq<G> that, final MSeq<G> other) {
    final int length = min(that.length(), other.length());
    return (int)indexes(RandomRegistry.random(), length, _swapProbability)
        .peek(i -> that.swap(i, other))
        .count();
}

A Collector<T, ?, Long> collector should avoid this kind of problem.

@Override
protected int crossover(final MSeq<G> that, final MSeq<G> other) {
    final int length = min(that.length(), other.length());
    return indexes(RandomRegistry.random(), length, _swapProbability)
        .collect(count(i -> that.swap(i, other)))
        .intValue();
}

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions