Esempio n. 1
0
  public static void main(String[] args) throws IOException {
    Path path = Paths.get("../alice.txt");
    String contents = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);

    Stream<String> words = Stream.of(contents.split("[\\P{L}]+"));
    show("words", words);
    Stream<String> song = Stream.of("gently", "down", "the", "stream");
    show("song", song);
    Stream<String> silence = Stream.empty();
    silence = Stream.<String>empty(); // Explicit type specification
    show("silence", silence);

    Stream<String> echos = Stream.generate(() -> "Echo");
    show("echos", echos);

    Stream<Double> randoms = Stream.generate(Math::random);
    show("randoms", randoms);

    Stream<BigInteger> integers = Stream.iterate(BigInteger.ONE, n -> n.add(BigInteger.ONE));
    show("integers", integers);

    Stream<String> wordsAnotherWay = Pattern.compile("[\\P{L}]+").splitAsStream(contents);
    show("wordsAnotherWay", wordsAnotherWay);

    try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {
      show("lines", lines);
    }
  }
Esempio n. 2
0
 @SuppressWarnings("unchecked")
 @Override
 default Tuple2<Seq<T>, Seq<T>> span(Predicate<? super T> predicate) {
   Objects.requireNonNull(predicate, "predicate is null");
   if (isEmpty()) {
     return Tuple.of(Stream.empty(), Stream.empty());
   } else {
     return (Tuple2<Seq<T>, Seq<T>>) traverse().span(predicate);
   }
 }
Esempio n. 3
0
 @Override
 default Seq<T> takeRight(int n) {
   if (isEmpty()) {
     return Stream.empty();
   } else {
     return traverse().takeRight(n);
   }
 }
Esempio n. 4
0
 @Override
 default Seq<T> dropRight(int n) {
   if (n >= length()) {
     return Stream.empty();
   } else {
     return traverse().dropRight(n);
   }
 }
Esempio n. 5
0
 @Override
 default Seq<T> filter(Predicate<? super T> predicate) {
   Objects.requireNonNull(predicate, "predicate is null");
   if (isEmpty()) {
     return Stream.empty();
   } else {
     return traverse().filter(predicate);
   }
 }
Esempio n. 6
0
 @Override
 default <U> Seq<T> distinctBy(Function<? super T, ? extends U> keyExtractor) {
   Objects.requireNonNull(keyExtractor, "keyExtractor is null");
   if (isEmpty()) {
     return Stream.empty();
   } else {
     return traverse().distinctBy(keyExtractor);
   }
 }
Esempio n. 7
0
 @Override
 default Seq<T> distinctBy(Comparator<? super T> comparator) {
   Objects.requireNonNull(comparator, "comparator is null");
   if (isEmpty()) {
     return Stream.empty();
   } else {
     return traverse().distinctBy(comparator);
   }
 }
Esempio n. 8
0
 static <T> Stream<T> levelOrder(Tree<T> tree) {
   Stream<T> result = Stream.empty();
   final java.util.Queue<Tree<T>> queue = new java.util.LinkedList<>();
   queue.add(tree);
   while (!queue.isEmpty()) {
     final Tree<T> next = queue.remove();
     result = result.prepend(next.getValue());
     queue.addAll(next.getChildren().toJavaList());
   }
   return result.reverse();
 }
Esempio n. 9
0
 static <T> Stream<T> inOrder(Tree<T> tree) {
   if (tree.isLeaf()) {
     return Stream.of(tree.getValue());
   } else {
     final List<Node<T>> children = tree.getChildren();
     return children
         .tail()
         .foldLeft(Stream.<T>empty(), (acc, child) -> acc.appendAll(inOrder(child)))
         .prepend(tree.getValue())
         .prependAll(inOrder(children.head()));
   }
 }
Esempio n. 10
0
 /**
  * Traverses the Tree in a specific order.
  *
  * @param order the tree traversal order
  * @return A List containing all elements of this tree, which is List if this tree is empty.
  * @throws java.lang.NullPointerException if order is null
  */
 default Seq<T> traverse(Order order) {
   Objects.requireNonNull(order, "order is null");
   if (isEmpty()) {
     return Stream.empty();
   } else {
     switch (order) {
       case PRE_ORDER:
         return Traversal.preOrder(this);
       case IN_ORDER:
         return Traversal.inOrder(this);
       case POST_ORDER:
         return Traversal.postOrder(this);
       case LEVEL_ORDER:
         return Traversal.levelOrder(this);
       default:
         throw new IllegalStateException("Unknown order: " + order.name());
     }
   }
 }
Esempio n. 11
0
 static <T> Stream<T> postOrder(Tree<T> tree) {
   return tree.getChildren()
       .foldLeft(Stream.<T>empty(), (acc, child) -> acc.appendAll(postOrder(child)))
       .append(tree.getValue());
 }