Search in sources :

Example 61 with ReactiveSeq

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

the class MiscTest method pVectorX.

@Test
public void pVectorX() {
    ReactiveSeq<String> seq = Spouts.from(VectorX.of(1, 2, 3, 4).plus(5).map(i -> "connect toNested Akka, RxJava and more with reactiveBuffer-streams" + i));
    PersistentSetX<String> setX = seq.to(s -> new LazyReact().fromStream(s)).map(data -> "fan out across threads with futureStreams" + data).to(ReactiveConvertableSequence::converter).persistentSetX();
}
Also used : ListX(cyclops.reactive.collections.mutable.ListX) Arrays(java.util.Arrays) ReactiveConvertableSequence(com.oath.cyclops.ReactiveConvertableSequence) Spouts(cyclops.reactive.Spouts) LazyReact(cyclops.futurestream.LazyReact) VectorX(cyclops.reactive.collections.immutable.VectorX) Futures(cyclops.companion.Futures) Test(org.junit.Test) Future(cyclops.control.Future) Supplier(java.util.function.Supplier) Assert.assertThat(org.junit.Assert.assertThat) ReactiveSeq(cyclops.reactive.ReactiveSeq) Flux(reactor.core.publisher.Flux) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Matchers.equalTo(org.hamcrest.Matchers.equalTo) SimpleReact(cyclops.futurestream.SimpleReact) Optional(java.util.Optional) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) PersistentSetX(cyclops.reactive.collections.immutable.PersistentSetX) LazyReact(cyclops.futurestream.LazyReact) Test(org.junit.Test)

Example 62 with ReactiveSeq

use of cyclops.reactive.ReactiveSeq 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 63 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 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)

Example 64 with ReactiveSeq

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

the class SimpleReactStream method splitAt.

/**
 * Split a stream at a given position. (Operates on futures)
 * <pre>
 * {@code
 * // tuple((1, 2, 3), (4, 5, 6))
 *  EagerFutureStream.of(1, 2, 3, 4, 5,6).splitAt(3)
 *
 *  }</pre>
 * @return
 */
default Tuple2<SimpleReactStream<U>, SimpleReactStream<U>> splitAt(final int position) {
    final Stream stream = getLastActive().stream();
    final Tuple2<ReactiveSeq<CompletableFuture<U>>, ReactiveSeq<CompletableFuture<U>>> split = ReactiveSeq.fromStream((Stream<CompletableFuture<U>>) stream).splitAt(position);
    return Tuple.tuple(fromListCompletableFuture(split._1().collect(Collectors.toList())), fromListCompletableFuture(split._2().collect(Collectors.toList())));
}
Also used : FutureStream(cyclops.futurestream.FutureStream) Stream(java.util.stream.Stream) ReactiveSeq(cyclops.reactive.ReactiveSeq)

Example 65 with ReactiveSeq

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

the class LazySeqAgronaTest method shouldZipFiniteWithInfiniteSeq.

@Test
public void shouldZipFiniteWithInfiniteSeq() throws Exception {
    ThreadPools.setUseCommon(false);
    final ReactiveSeq<Integer> units = new LazyReact(ThreadPools.getCommonFreeThread()).iterate(1, n -> n + 1).limit(5);
    // <-- MEMORY LEAK! - no auto-closing yet, so writes infinetely to it's async queue
    final FutureStream<Integer> hundreds = new LazyReact(ThreadPools.getCommonFreeThread()).iterate(100, n -> n + 100);
    final ReactiveSeq<String> zipped = units.zip(hundreds, (n, p) -> n + ": " + p);
    assertThat(zipped.limit(5).join(), equalTo(of("1: 100", "2: 200", "3: 300", "4: 400", "5: 500").join()));
    ThreadPools.setUseCommon(true);
}
Also used : Tuple2(cyclops.data.tuple.Tuple2) QueueFactories(com.oath.cyclops.async.QueueFactories) LazyReact(cyclops.futurestream.LazyReact) Matchers.not(org.hamcrest.Matchers.not) FutureStream(cyclops.futurestream.FutureStream) Matchers.hasItems(org.hamcrest.Matchers.hasItems) Supplier(java.util.function.Supplier) Assert.assertThat(org.junit.Assert.assertThat) Tuple.tuple(cyclops.data.tuple.Tuple.tuple) Arrays.asList(java.util.Arrays.asList) Matchers.lessThan(org.hamcrest.Matchers.lessThan) BaseSeqTest(cyclops.futurestream.react.base.BaseSeqTest) Iterator(java.util.Iterator) Collection(java.util.Collection) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) ReactiveSeq(cyclops.reactive.ReactiveSeq) List(java.util.List) Stream(java.util.stream.Stream) Queue(com.oath.cyclops.async.adapters.Queue) Ignore(org.junit.Ignore) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) ForkJoinPool(java.util.concurrent.ForkJoinPool) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ThreadPools(com.oath.cyclops.react.ThreadPools) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Signal(com.oath.cyclops.async.adapters.Signal) LazyReact(cyclops.futurestream.LazyReact) BaseSeqTest(cyclops.futurestream.react.base.BaseSeqTest) 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