Search in sources :

Example 26 with Tuple2

use of cyclops.data.tuple.Tuple2 in project cyclops by aol.

the class AbstractIterableXTest method pairWiseZipIncremental.

@Test
public void pairWiseZipIncremental() {
    AtomicBoolean data = new AtomicBoolean(false);
    AtomicReference<Vector<Tuple2<Integer, String>>> values = new AtomicReference<>(Vector.empty());
    AtomicBoolean complete = new AtomicBoolean(false);
    AtomicReference<Throwable> error = new AtomicReference<Throwable>(null);
    Subscription sub = of(1).zip(of("test")).zip(of("test2")).map(t -> Tuple.tuple(t._1()._1(), t._1()._2() + t._2())).forEach(0, n -> {
        data.set(true);
        values.updateAndGet(v -> v.plus(n));
    }, e -> {
        error.set(e);
    }, () -> {
        complete.set(true);
    });
    assertFalse(data.get());
    assertFalse(complete.get());
    assertNull(error.get());
    assertThat(values.get(), Matchers.equalTo(Vector.empty()));
    sub.request(10l);
    assertTrue(data.get());
    assertTrue(complete.get());
    assertNull(error.get());
    assertThat(values.get(), Matchers.equalTo(Vector.of(Tuple.tuple(1, "testtest2"))));
}
Also used : StepVerifier(reactor.test.StepVerifier) HashMap(cyclops.data.HashMap) Trampoline(cyclops.control.Trampoline) Monoids(cyclops.companion.Monoids) UnaryOperator(java.util.function.UnaryOperator) MockitoAnnotations(org.mockito.MockitoAnnotations) Arrays.asList(java.util.Arrays.asList) Semigroups(cyclops.companion.Semigroups) cyclops.data(cyclops.data) ReactiveSeq.fromIntStream(cyclops.reactive.ReactiveSeq.fromIntStream) Monoid(cyclops.function.Monoid) SimpleTimer(com.oath.cyclops.util.SimpleTimer) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Serializable(java.io.Serializable) ReactiveSeq(cyclops.reactive.ReactiveSeq) Stream(java.util.stream.Stream) IterableX(com.oath.cyclops.types.traversable.IterableX) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) Try(cyclops.control.Try) TreeSet(cyclops.data.TreeSet) Tuple4(cyclops.data.tuple.Tuple4) IntStream(java.util.stream.IntStream) Tuple3(cyclops.data.tuple.Tuple3) Tuple2(cyclops.data.tuple.Tuple2) java.util(java.util) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Vector(cyclops.data.Vector) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Tuple(cyclops.data.tuple.Tuple) Tuple.tuple(cyclops.data.tuple.Tuple.tuple) IsEqual(org.hamcrest.core.IsEqual) Option(cyclops.control.Option) Comparator.comparing(java.util.Comparator.comparing) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Before(org.junit.Before) Maybe(cyclops.control.Maybe) LongStream(java.util.stream.LongStream) Spouts(cyclops.reactive.Spouts) Streams(cyclops.companion.Streams) Executor(java.util.concurrent.Executor) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) Ignore(org.junit.Ignore) Subscription(org.reactivestreams.Subscription) Assert(org.junit.Assert) Reducers(cyclops.companion.Reducers) Streamable(cyclops.companion.Streamable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(org.reactivestreams.Subscription) Vector(cyclops.data.Vector) Test(org.junit.Test)

Example 27 with Tuple2

use of cyclops.data.tuple.Tuple2 in project cyclops by aol.

the class ReactiveZippingRSTest method testUnzipWithLimits.

@Test
public void testUnzipWithLimits() {
    Supplier<ReactiveSeq<Tuple2<Integer, String>>> s = () -> of(new Tuple2(1, "a"), new Tuple2(2, "b"), new Tuple2(3, "c"));
    Tuple2<ReactiveSeq<Integer>, ReactiveSeq<String>> u1 = ReactiveSeq.unzip(s.get());
    assertTrue(u1._1().limit(2).to(Streamable::fromStream).toList().containsAll(Arrays.asList(1, 2)));
    assertTrue(u1._2().toList().containsAll(asList("a", "b", "c")));
}
Also used : Tuple2(cyclops.data.tuple.Tuple2) ReactiveSeq(cyclops.reactive.ReactiveSeq) FluxReactiveSeq(cyclops.reactive.FluxReactiveSeq) Test(org.junit.Test)

Example 28 with Tuple2

use of cyclops.data.tuple.Tuple2 in project cyclops by aol.

the class ReactiveZippingRSTest method testUnzip.

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

Example 29 with Tuple2

use of cyclops.data.tuple.Tuple2 in project cyclops by aol.

the class SimpleReactStream method zipWithIndex.

/**
 * Zip this Stream with an index, but Zip based on the underlying tasks, not completed results.
 *
 * e.g.
 * two function that return method name, but take varying lengths of time.
 * <pre>
 * <code>
 * EagerFutureStream.react(()-&gt;takesALotOfTime(),()-&gt;veryQuick()).zipWithIndex();
 *
 *  [["takesALotOfTime",0],["veryQuick",1]]
 *
 *  Where as with standard zipWithIndex you would getValue a new Stream ordered by completion
 *
 *  [["veryQuick",0],["takesALotOfTime",1]]
 * </code>
 * </pre>
 */
default SimpleReactStream<Tuple2<U, Long>> zipWithIndex() {
    final ReactiveSeq seq = ReactiveSeq.fromIterator(getLastActive().stream().iterator()).zipWithIndex();
    final ReactiveSeq<Tuple2<CompletableFuture<U>, Long>> withType = seq;
    final SimpleReactStream futureStream = fromStreamOfFutures((Stream) withType.map(t -> t._1().thenApply(v -> Tuple.tuple(t._1().join(), t._2()))));
    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 30 with Tuple2

use of cyclops.data.tuple.Tuple2 in project cyclops by aol.

the class SimpleReactStream method zip.

/**
 * Zip two Streams, zipping against the underlying futures of this stream
 *
 * @param other
 * @return
 */
default <R> SimpleReactStream<Tuple2<U, R>> zip(final Stream<R> other) {
    final ReactiveSeq seq = ReactiveSeq.fromStream(getLastActive().stream()).zip(ReactiveSeq.fromStream(other));
    final ReactiveSeq<Tuple2<CompletableFuture<U>, R>> withType = seq;
    final SimpleReactStream futureStream = fromStreamOfFutures((Stream) withType.map(t -> t._1().thenApply(v -> Tuple.tuple(t._1().join(), t._2()))));
    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)

Aggregations

Tuple2 (cyclops.data.tuple.Tuple2)43 Test (org.junit.Test)35 ReactiveSeq (cyclops.reactive.ReactiveSeq)23 Stream (java.util.stream.Stream)17 FutureStream (cyclops.futurestream.FutureStream)16 Tuple (cyclops.data.tuple.Tuple)10 BaseSeqTest (cyclops.futurestream.react.base.BaseSeqTest)9 Collectors (java.util.stream.Collectors)7 FlowableReactiveSeq (cyclops.reactive.FlowableReactiveSeq)6 FluxReactiveSeq (cyclops.reactive.FluxReactiveSeq)6 java.util (java.util)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Queue (com.oath.cyclops.async.adapters.Queue)5 Vector (cyclops.data.Vector)5 Tuple3 (cyclops.data.tuple.Tuple3)5 LazyReact (cyclops.futurestream.LazyReact)5 Streams (cyclops.companion.Streams)4 Executor (java.util.concurrent.Executor)4 Function (java.util.function.Function)4