Search in sources :

Example 96 with ReactiveSeq

use of cyclops.reactive.ReactiveSeq in project cyclops by aol.

the class ReactiveZippingTest method testUnzip3.

@Test
public void testUnzip3() {
    Supplier<ReactiveSeq<Tuple3<Integer, String, Long>>> s = () -> of(new Tuple3(1, "a", 2l), new Tuple3(2, "b", 3l), new Tuple3(3, "c", 4l));
    Tuple3<ReactiveSeq<Integer>, ReactiveSeq<String>, ReactiveSeq<Long>> u1 = ReactiveSeq.unzip3(s.get());
    assertTrue(u1._1().toList().containsAll(Arrays.asList(1, 2, 3)));
    assertTrue(u1._2().toList().containsAll(asList("a", "b", "c")));
    assertTrue(u1._3().toList().containsAll(asList(2l, 3l, 4l)));
}
Also used : Tuple3(cyclops.data.tuple.Tuple3) ReactiveSeq(cyclops.reactive.ReactiveSeq) Test(org.junit.Test)

Example 97 with ReactiveSeq

use of cyclops.reactive.ReactiveSeq in project cyclops by aol.

the class SimpleReactStream method zip.

/**
 * Zip two Streams, zipping against the underlying futures of both Streams
 * Placeholders (Futures) will be populated immediately in the new zipped Stream and results
 * will be populated asyncrhonously
 *
 * @param other  Another FutureStream to zip Futures with
 * @return New Sequence of CompletableFutures
 */
default <R> SimpleReactStream<Tuple2<U, R>> zip(final SimpleReactStream<R> other) {
    final ReactiveSeq seq = ReactiveSeq.fromStream(getLastActive().stream()).zip(ReactiveSeq.fromStream(other.getLastActive().stream()));
    final ReactiveSeq<Tuple2<CompletableFuture<U>, CompletableFuture<R>>> withType = seq;
    final SimpleReactStream futureStream = fromStreamOfFutures((Stream) withType.map(t -> CompletableFuture.allOf(t._1(), t._2()).thenApply(v -> Tuple.tuple(t._1().join(), t._2().join()))));
    return futureStream;
}
Also used : ListX(cyclops.reactive.collections.mutable.ListX) Blocker(com.oath.cyclops.react.collectors.lazy.Blocker) Tuple2(cyclops.data.tuple.Tuple2) java.util(java.util) ReactiveConvertableSequence(com.oath.cyclops.ReactiveConvertableSequence) LazyReact(cyclops.futurestream.LazyReact) FutureStream(cyclops.futurestream.FutureStream) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Tuple(cyclops.data.tuple.Tuple) FilteredExecutionPathException(com.oath.cyclops.internal.react.exceptions.FilteredExecutionPathException) SimpleReactFailedStageException(com.oath.cyclops.react.SimpleReactFailedStageException) StreamSupport(java.util.stream.StreamSupport) Collector(java.util.stream.Collector) EagerStreamWrapper(com.oath.cyclops.internal.react.stream.EagerStreamWrapper) ThrowsSoftened(com.oath.cyclops.util.ThrowsSoftened) Continueable(com.oath.cyclops.react.async.subscription.Continueable) Streams(cyclops.companion.Streams) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) ReactiveSeq(cyclops.reactive.ReactiveSeq) StageWithResults(com.oath.cyclops.react.StageWithResults) Queue(com.oath.cyclops.async.adapters.Queue) Stream(java.util.stream.Stream) Status(com.oath.cyclops.react.Status) SimpleReact(cyclops.futurestream.SimpleReact) QueueFactory(com.oath.cyclops.async.adapters.QueueFactory) Tuple2(cyclops.data.tuple.Tuple2) ReactiveSeq(cyclops.reactive.ReactiveSeq)

Example 98 with ReactiveSeq

use of cyclops.reactive.ReactiveSeq in project cyclops by aol.

the class MergeLatestTest method mergeLatestEmpty.

@Test
public void mergeLatestEmpty() {
    AtomicReference<Vector<Integer>> data = new AtomicReference(Vector.empty());
    AtomicBoolean complete = new AtomicBoolean(false);
    AtomicReference<Throwable> error = new AtomicReference<Throwable>(null);
    Spouts.<Integer>mergeLatest(Spouts.<ReactiveSeq<Integer>>of()).forEach(z -> {
        assertFalse(complete.get());
        data.updateAndGet(s -> s.plus(z));
    }, e -> {
        error.set(e);
    }, () -> {
        complete.set(true);
    });
    Assert.assertThat(complete.get(), equalTo(true));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Vector(cyclops.data.Vector) ReactiveSeq(cyclops.reactive.ReactiveSeq) Test(org.junit.Test)

Example 99 with ReactiveSeq

use of cyclops.reactive.ReactiveSeq in project cyclops by aol.

the class MergeLatestTest method mergeEmpty.

@Test
public void mergeEmpty() {
    AtomicReference<Vector<Integer>> data = new AtomicReference(Vector.empty());
    AtomicBoolean complete = new AtomicBoolean(false);
    AtomicReference<Throwable> error = new AtomicReference<Throwable>(null);
    Spouts.<Integer>merge(Spouts.<ReactiveSeq<Integer>>of()).forEach(z -> {
        assertFalse(complete.get());
        data.updateAndGet(s -> s.plus(z));
    }, e -> {
        error.set(e);
    }, () -> {
        complete.set(true);
    });
    Assert.assertThat(complete.get(), equalTo(true));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Vector(cyclops.data.Vector) ReactiveSeq(cyclops.reactive.ReactiveSeq) Test(org.junit.Test)

Example 100 with ReactiveSeq

use of cyclops.reactive.ReactiveSeq in project cyclops by aol.

the class ZippingTest method testUnzip4WithLimits.

@Test
public void testUnzip4WithLimits() {
    Supplier<ReactiveSeq<Tuple4<Integer, String, Long, Character>>> s = () -> of(new Tuple4(1, "a", 2l, 'z'), new Tuple4(2, "b", 3l, 'y'), new Tuple4(3, "c", 4l, 'x'));
    Tuple4<ReactiveSeq<Integer>, ReactiveSeq<String>, ReactiveSeq<Long>, ReactiveSeq<Character>> u1 = ReactiveSeq.unzip4(s.get());
    assertTrue(u1._1().limit(1).toList().containsAll(Arrays.asList(1)));
    assertTrue(u1._2().limit(2).toList().containsAll(asList("a", "b")));
    assertTrue(u1._3().limit(3).toList().containsAll(asList(2l, 3l, 4l)));
    assertTrue(u1._4().limit(4).toList().containsAll(asList('z', 'y', 'x')));
}
Also used : Tuple4(cyclops.data.tuple.Tuple4) ReactiveSeq(cyclops.reactive.ReactiveSeq) Test(org.junit.Test)

Aggregations

ReactiveSeq (cyclops.reactive.ReactiveSeq)101 Test (org.junit.Test)91 Tuple3 (cyclops.data.tuple.Tuple3)33 Tuple4 (cyclops.data.tuple.Tuple4)33 Tuple2 (cyclops.data.tuple.Tuple2)31 FlowableReactiveSeq (cyclops.reactive.FlowableReactiveSeq)19 FluxReactiveSeq (cyclops.reactive.FluxReactiveSeq)18 Stream (java.util.stream.Stream)17 Collectors (java.util.stream.Collectors)16 FutureStream (cyclops.futurestream.FutureStream)12 QueueFactories (com.oath.cyclops.async.QueueFactories)11 Queue (com.oath.cyclops.async.adapters.Queue)11 LazyReact (cyclops.futurestream.LazyReact)11 Supplier (java.util.function.Supplier)10 Signal (com.oath.cyclops.async.adapters.Signal)9 List (java.util.List)9 Spouts (cyclops.reactive.Spouts)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Matchers.equalTo (org.hamcrest.Matchers.equalTo)8 Assert.assertThat (org.junit.Assert.assertThat)8