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