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()));
}
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()));
}
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;
}
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));
}));
}
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()));
}
Aggregations