Search in sources :

Example 1 with Tuple2

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

the class TakeSkipSliceTest method testZipWithFutures.

@Test
public void testZipWithFutures() {
    SimpleReactStream stream = of("a", "b");
    List<Tuple2<Integer, String>> result = of(1, 2).zip(stream).block();
    assertThat(result.size(), is(asList(tuple(1, "a"), tuple(2, "b")).size()));
}
Also used : Tuple2(cyclops.data.tuple.Tuple2) BaseSimpleReactStream(com.oath.cyclops.types.futurestream.BaseSimpleReactStream) SimpleReactStream(com.oath.cyclops.types.futurestream.SimpleReactStream) Test(org.junit.Test)

Example 2 with Tuple2

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

the class TakeSkipSliceTest method testZipWithFuturesStream.

@Test
public void testZipWithFuturesStream() {
    Stream stream = Stream.of("a", "b");
    List<Tuple2<Integer, String>> result = of(1, 2).zip(stream).block();
    assertThat(result.size(), is(asList(tuple(1, "a"), tuple(2, "b")).size()));
}
Also used : Tuple2(cyclops.data.tuple.Tuple2) IntStream(java.util.stream.IntStream) BaseSimpleReactStream(com.oath.cyclops.types.futurestream.BaseSimpleReactStream) Stream(java.util.stream.Stream) SimpleReactStream(com.oath.cyclops.types.futurestream.SimpleReactStream) Test(org.junit.Test)

Example 3 with Tuple2

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

the class ZippingLatestOperator method subscribe.

@Override
public StreamSubscription subscribe(Consumer<? super R> onNext, Consumer<? super Throwable> onError, Runnable onComplete) {
    StreamSubscription[] leftSub = { null };
    StreamSubscription[] rightSub = { null };
    // left & right compelte can be merged into single integer
    AtomicBoolean leftComplete = new AtomicBoolean(false);
    AtomicBoolean rightComplete = new AtomicBoolean(false);
    AtomicReference<Tuple2<T1, T2>> nextValue = new AtomicReference<>(Tuple.tuple((T1) UNSET, (T2) UNSET));
    AtomicBoolean completing = new AtomicBoolean(false);
    AtomicInteger completed = new AtomicInteger(0);
    AtomicInteger index = new AtomicInteger(0);
    ManyToOneConcurrentArrayQueue<R> data = new ManyToOneConcurrentArrayQueue<R>(1024);
    List<StreamSubscription> subs = new ArrayList<>(2);
    StreamSubscription sub = new StreamSubscription() {

        LongConsumer work = n -> {
            while (requested.get() > 0) {
                if (completed.get() == 2 && data.size() == 0) {
                    onComplete.run();
                    return;
                }
                long sent = 0;
                long reqCycle = requested.get();
                for (long k = 0; k < reqCycle; k++) {
                    if (!isOpen)
                        return;
                    int toUse = index.incrementAndGet() - 1;
                    if (toUse + 1 >= subs.size()) {
                        index.set(0);
                    }
                    if (subs.get(toUse).isOpen) {
                        subs.get(toUse).request(1l);
                    } else
                        k--;
                    R fromQ = nilsafeOut(data.poll());
                    if (fromQ != null) {
                        onNext.accept(fromQ);
                        requested.decrementAndGet();
                        sent++;
                    }
                    if (completed.get() == subs.size() && data.size() == 0) {
                        onComplete.run();
                        return;
                    }
                }
                while (sent < reqCycle && isOpen && !(completed.get() == subs.size() && data.size() == 0)) {
                    R fromQ = nilsafeOut(data.poll());
                    if (fromQ != null) {
                        onNext.accept(fromQ);
                        requested.decrementAndGet();
                        sent++;
                    }
                }
                if (completed.get() == subs.size() && data.size() == 0) {
                    onComplete.run();
                    return;
                }
            }
        };

        @Override
        public void request(long n) {
            if (n <= 0) {
                onError.accept(new IllegalArgumentException("3.9 While the Subscription is not cancelled, Subscription.request(long n) MUST throw a java.lang.IllegalArgumentException if the argument is <= 0."));
                return;
            }
            this.singleActiveRequest(n, work);
        }

        @Override
        public void cancel() {
            if (leftSub[0] != null)
                leftSub[0].cancel();
            if (rightSub[0] != null)
                rightSub[0].cancel();
            super.cancel();
        }
    };
    leftSub[0] = left.subscribe(e -> {
        if (!sub.isOpen)
            return;
        try {
            boolean set = false;
            while (!set) {
                Tuple2<T1, T2> local = nextValue.get();
                Tuple2<T1, T2> updated = local.map1(__ -> (T1) e);
                set = nextValue.compareAndSet(local, updated);
                if (set) {
                    if (updated._2() != UNSET) {
                        while (!data.offer((R) nilsafeIn(applyFn(updated)))) {
                        }
                    }
                }
            }
        } catch (Throwable t) {
            onError.accept(t);
        }
    }, e -> {
        onError.accept(e);
        leftSub[0].request(1l);
    }, () -> {
        completed.incrementAndGet();
    });
    rightSub[0] = right.subscribe(e -> {
        if (!sub.isOpen)
            return;
        try {
            boolean set = false;
            while (!set) {
                Tuple2<T1, T2> local = nextValue.get();
                Tuple2<T1, T2> updated = local.map2(__ -> (T2) e);
                set = nextValue.compareAndSet(local, updated);
                if (set) {
                    if (updated._1() != UNSET) {
                        while (!data.offer((R) nilsafeIn(applyFn(updated)))) {
                        }
                    }
                }
            }
        } catch (Throwable t) {
            onError.accept(t);
        }
    }, e -> {
        onError.accept(e);
        rightSub[0].request(1l);
    }, () -> {
        completed.incrementAndGet();
    });
    subs.add(leftSub[0]);
    subs.add(rightSub[0]);
    return sub;
}
Also used : ManyToOneConcurrentArrayQueue(org.agrona.concurrent.ManyToOneConcurrentArrayQueue) Tuple2(cyclops.data.tuple.Tuple2) BiFunction(java.util.function.BiFunction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) LongConsumer(java.util.function.LongConsumer) Consumer(java.util.function.Consumer) List(java.util.List) Queue(com.oath.cyclops.async.adapters.Queue) Tuple(cyclops.data.tuple.Tuple) ManyToOneConcurrentArrayQueue(org.agrona.concurrent.ManyToOneConcurrentArrayQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllArgsConstructor(lombok.AllArgsConstructor) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongConsumer(java.util.function.LongConsumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Tuple2(cyclops.data.tuple.Tuple2)

Example 4 with Tuple2

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

the class Folds method withPercentiles.

default Seq<Tuple2<T, BigDecimal>> withPercentiles() {
    Seq<T> list = stream().seq();
    int precision = new Double(Math.log10(list.size())).intValue();
    return list.zipWithIndex().map(t -> t.map2(idx -> {
        double d = (idx / new Double(list.size()));
        return new BigDecimal((d * 100), new MathContext(precision));
    }));
}
Also used : PrintStream(java.io.PrintStream) PrintWriter(java.io.PrintWriter) Maybe(cyclops.control.Maybe) Tuple3(cyclops.data.tuple.Tuple3) java.util(java.util) Tuple2(cyclops.data.tuple.Tuple2) cyclops.data(cyclops.data) Spouts(cyclops.reactive.Spouts) HashMap(cyclops.data.HashMap) MathContext(java.math.MathContext) Monoid(cyclops.function.Monoid) HashSet(cyclops.data.HashSet) Vector(cyclops.data.Vector) Collectors(java.util.stream.Collectors) Seq(cyclops.data.Seq) BigDecimal(java.math.BigDecimal) Reducer(cyclops.function.Reducer) ReactiveSeq(cyclops.reactive.ReactiveSeq) Tuple(cyclops.data.tuple.Tuple) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Option(cyclops.control.Option) java.util.function(java.util.function) Collector(java.util.stream.Collector) TreeSet(cyclops.data.TreeSet) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext)

Example 5 with Tuple2

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

the class LazySeqAgronaTest method testZipWithFuturesCoreStream.

@Test
public void testZipWithFuturesCoreStream() {
    Stream stream = Stream.of("a", "b");
    FutureStream<Tuple2<Integer, String>> seq = of(1, 2).actOnFutures().zip(stream);
    // .map(tuple -> Tuple.tuple(tuple.v1.join(),tuple.v2)).collect(CyclopsCollectors.toList());
    List<Tuple2<Integer, String>> result = seq.block();
    assertThat(result.size(), is(asList(tuple(1, "a"), tuple(2, "b")).size()));
}
Also used : Tuple2(cyclops.data.tuple.Tuple2) FutureStream(cyclops.futurestream.FutureStream) Stream(java.util.stream.Stream) BaseSeqTest(cyclops.futurestream.react.base.BaseSeqTest) Test(org.junit.Test)

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