Search in sources :

Example 11 with ReactiveSeq

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

the class IdenticalToStream method measureThroughput.

@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 5)
@Measurement(iterations = 5)
@Fork(1)
public List<Entry<Integer, List<String>>> measureThroughput() {
    // Function to compute the score of a given word
    IntUnaryOperator scoreOfALetter = letter -> letterScores[letter - 'a'];
    // score of the same letters in a word
    ToIntFunction<Entry<Integer, Long>> letterScore = entry -> letterScores[entry.getKey() - 'a'] * Integer.min(entry.getValue().intValue(), scrabbleAvailableLetters[entry.getKey() - 'a']);
    // Histogram of the letters in a given word
    Function<String, Map<Integer, Long>> histoOfLetters = word -> ReactiveSeq.fromCharSequence(word).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    // number of blanks for a given letter
    ToLongFunction<Entry<Integer, Long>> blank = entry -> Long.max(0L, entry.getValue() - scrabbleAvailableLetters[entry.getKey() - 'a']);
    // number of blanks for a given word
    Function<String, Long> nBlanks = word -> reactiveSeq(histoOfLetters.apply(word).entrySet()).mapToLong(blank).sum();
    // can a word be written with 2 blanks?
    Predicate<String> checkBlanks = word -> nBlanks.apply(word) <= 2;
    // score taking blanks into account
    Function<String, Integer> score2 = word -> reactiveSeq(histoOfLetters.apply(word).entrySet()).mapToInt(letterScore).sum();
    // Placing the word on the board
    // Building the streams of first and last letters
    Function<String, IntStream> first3 = word -> word.chars().limit(3);
    Function<String, IntStream> last3 = word -> word.chars().skip(Integer.max(0, word.length() - 4));
    // Stream to be maxed
    Function<String, IntStream> toBeMaxed = word -> ReactiveSeq.of(first3.apply(word), last3.apply(word)).flatMapToInt(Function.identity());
    // Bonus for double letter
    ToIntFunction<String> bonusForDoubleLetter = word -> toBeMaxed.apply(word).map(scoreOfALetter).max().orElse(0);
    // score of the word put on the board
    Function<String, Integer> score3 = word -> (score2.apply(word) + bonusForDoubleLetter.applyAsInt(word)) + (score2.apply(word) + bonusForDoubleLetter.applyAsInt(word)) + (word.length() == 7 ? 50 : 0);
    Function<Function<String, Integer>, ReactiveSeq<Map<Integer, List<String>>>> buildHistoOnScore = score -> ReactiveSeq.of(reactiveSeq(shakespeareWords).filter(scrabbleWords::contains).filter(// filter out the words that needs more than 2 blanks
    checkBlanks).collect(Collectors.groupingBy(score, () -> new TreeMap<Integer, List<String>>(Comparator.reverseOrder()), Collectors.toList())));
    // best key / value pairs
    List<Entry<Integer, List<String>>> finalList = buildHistoOnScore.apply(score3).map(e -> reactiveSeq(e.entrySet()).limit(3).collect(Collectors.toList())).findAny().get();
    return finalList;
}
Also used : IntStream(java.util.stream.IntStream) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Measurement(org.openjdk.jmh.annotations.Measurement) IntUnaryOperator(java.util.function.IntUnaryOperator) Mode(org.openjdk.jmh.annotations.Mode) Predicate(java.util.function.Predicate) ToIntFunction(java.util.function.ToIntFunction) ReactiveSeq.reactiveSeq(cyclops.reactive.ReactiveSeq.reactiveSeq) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Warmup(org.openjdk.jmh.annotations.Warmup) Benchmark(org.openjdk.jmh.annotations.Benchmark) TimeUnit(java.util.concurrent.TimeUnit) ReactiveSeq(cyclops.reactive.ReactiveSeq) List(java.util.List) TreeMap(java.util.TreeMap) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit) Map(java.util.Map) Entry(java.util.Map.Entry) Fork(org.openjdk.jmh.annotations.Fork) Comparator(java.util.Comparator) ToLongFunction(java.util.function.ToLongFunction) IntUnaryOperator(java.util.function.IntUnaryOperator) ReactiveSeq(cyclops.reactive.ReactiveSeq) ToIntFunction(java.util.function.ToIntFunction) Function(java.util.function.Function) ToLongFunction(java.util.function.ToLongFunction) Entry(java.util.Map.Entry) List(java.util.List) TreeMap(java.util.TreeMap) Map(java.util.Map) IntStream(java.util.stream.IntStream) Measurement(org.openjdk.jmh.annotations.Measurement) Warmup(org.openjdk.jmh.annotations.Warmup) Fork(org.openjdk.jmh.annotations.Fork) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Benchmark(org.openjdk.jmh.annotations.Benchmark) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit)

Example 12 with ReactiveSeq

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

the class AsyncZippingTest 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) ObservableReactiveSeq(cyclops.reactive.ObservableReactiveSeq) Test(org.junit.Test)

Example 13 with ReactiveSeq

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

the class AsyncZippingTest 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) ObservableReactiveSeq(cyclops.reactive.ObservableReactiveSeq) Test(org.junit.Test)

Example 14 with ReactiveSeq

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

the class AsyncZippingTest 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) ObservableReactiveSeq(cyclops.reactive.ObservableReactiveSeq) Test(org.junit.Test)

Example 15 with ReactiveSeq

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

the class AsyncZippingTest method testUnzip4.

@Test
public void testUnzip4() {
    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().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)));
    assertTrue(u1._4().toList().containsAll(asList('z', 'y', 'x')));
}
Also used : Tuple4(cyclops.data.tuple.Tuple4) ReactiveSeq(cyclops.reactive.ReactiveSeq) ObservableReactiveSeq(cyclops.reactive.ObservableReactiveSeq) 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