Ocean Moist
Ocean Moist

Reputation: 17

Best way to combine multiple Flux<T> into Flux<T[]>

currently, I am doing it with multiple .zipWith() calls.

    frontLeft.getState()
        .zipWith(frontRight.getState(), (x, y) -> new SwerveModuleState[] {x, y})
        .zipWith(backLeft.getState(), (x, y) -> new SwerveModuleState[] {x[0], x[1], y})
        .zipWith(backRight.getState(), (x, y) -> new SwerveModuleState[] {x[0], x[1], x[2], y})
        .subscribe(
            states -> {
              estimator.update(
                  new Rotation2d(gyro.getYaw()), states); // new Rotation2d requires that the second arg is of type SwerveModuleState[]
            });

where frontLeft.getState(), frontRight.getState(), etc, is of type SwerveModuleState. I was doing it with Flux.zip(), but I found that states would be of type Object[]

EDIT:

related github issue: https://github.com/reactor/reactor-core/issues/1851

Upvotes: 0

Views: 238

Answers (2)

Patrick Hooijer
Patrick Hooijer

Reputation: 741

This is an API limitation at present since the generic type of the Publishers isn't captured, so you have to work with an Object[] when using Flux.zip, for example by copying into a typed array:

Flux.zip((Object[] array) -> Arrays.copyOf(array, array.length, String[].class),
                frontLeft.getState(), frontRight.getState(), backLeft.getState(), backRight.getState())
        .subscribe(states -> estimator.update(new Rotation2d(gyro.getYaw()), states));

Upvotes: 1

polarbear007
polarbear007

Reputation: 1

what about these static methods?

Flux zip(Iterable> sources, final Function<? super Object[], ? extends O> combinator)

Flux zip(final Function<? super Object[], ? extends O> combinator, Publisher<? extends I>... sources)

Upvotes: 0

Related Questions