io.vavr.collection.List.foldLeft()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(7.2k)|赞(0)|评价(0)|浏览(149)

本文整理了Java中io.vavr.collection.List.foldLeft()方法的一些代码示例,展示了List.foldLeft()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。List.foldLeft()方法的具体详情如下:
包路径:io.vavr.collection.List
类名称:List
方法名:foldLeft

List.foldLeft介绍

暂无

代码示例

代码示例来源:origin: vavr-io/vavr

/**
 * Constructs a rose tree branch.
 *
 * @param value    A value.
 * @param children A non-empty list of children.
 * @throws NullPointerException     if children is null
 * @throws IllegalArgumentException if children is empty
 */
public Node(T value, io.vavr.collection.List<Node<T>> children) {
  Objects.requireNonNull(children, "children is null");
  this.value = value;
  this.children = children;
  this.size = children.foldLeft(1, (acc, child) -> acc + child.size);
}

代码示例来源:origin: vavr-io/vavr

/**
 * A combinator that checks if <strong>all</strong> of the given {@code predicates} are satisfied.
 * <p>
 * By definition {@code allOf} is satisfied if the given {@code predicates} are empty.
 *
 * <pre>{@code
 * Predicate<Integer> isGreaterThanOne = i -> i > 1;
 * Predicate<Integer> isGreaterThanTwo = i -> i > 2;
 * allOf().test(0);                                   // true
 * allOf(isGreaterThanOne, isGreaterThanTwo).test(3); // true
 * allOf(isGreaterThanOne, isGreaterThanTwo).test(2); // false
 * }</pre>
 *
 * @param predicates An array of predicates
 * @param <T>        closure over tested object types
 * @return A new {@code Predicate}
 * @throws NullPointerException if {@code predicates} is null
 */
@SuppressWarnings({ "unchecked", "varargs" })
@SafeVarargs
public static <T> Predicate<T> allOf(Predicate<T>... predicates) {
  Objects.requireNonNull(predicates, "predicates is null");
  return t -> List.of(predicates).foldLeft(true, (bool, pred) -> bool && pred.test(t));
}

代码示例来源:origin: vavr-io/vavr

@Override
default List<T> reverse() {
  return (length() <= 1) ? this : foldLeft(empty(), List::prepend);
}

代码示例来源:origin: vavr-io/vavr

/**
 * Enqueues the given elements. A queue has FIFO order, i.e. the first of the given elements is
 * the first which will be retrieved.
 *
 * @param elements An Iterable of elements, may be empty
 * @return a new {@code Queue} instance, containing the new elements
 * @throws NullPointerException if elements is null
 */
@SuppressWarnings("unchecked")
public Queue<T> enqueueAll(Iterable<? extends T> elements) {
  Objects.requireNonNull(elements, "elements is null");
  if (isEmpty() && elements instanceof Queue) {
    return (Queue<T>) elements;
  } else {
    return io.vavr.collection.List.ofAll(elements).foldLeft(this, Queue::enqueue);
  }
}

代码示例来源:origin: vavr-io/vavr

@Override
default List<T> prependAll(Iterable<? extends T> elements) {
  Objects.requireNonNull(elements, "elements is null");
  return isEmpty() ? ofAll(elements) : ofAll(elements).reverse().foldLeft(this, List::prepend);
}

代码示例来源:origin: vavr-io/vavr

/**
 * Counts the number of branches of this tree. The empty tree and a leaf have no branches.
 *
 * @return The number of branches of this tree.
 */
default int branchCount() {
  if (isEmpty() || isLeaf()) {
    return 0;
  } else {
    return getChildren().foldLeft(1, (count, child) -> count + child.branchCount());
  }
}

代码示例来源:origin: vavr-io/vavr

static <T> Stream<Node<T>> traversePreOrder(Node<T> node) {
  return node.getChildren().foldLeft(Stream.of(node),
      (acc, child) -> acc.appendAll(traversePreOrder(child)));
}

代码示例来源:origin: vavr-io/vavr

/**
 * Counts the number of leaves of this tree. The empty tree has no leaves.
 *
 * @return The number of leaves of this tree.
 */
default int leafCount() {
  if (isEmpty()) {
    return 0;
  } else if (isLeaf()) {
    return 1;
  } else {
    return getChildren().foldLeft(0, (count, child) -> count + child.leafCount());
  }
}

代码示例来源:origin: vavr-io/vavr

static <T> Stream<Node<T>> traversePostOrder(Node<T> node) {
  return node
      .getChildren()
      .foldLeft(Stream.<Node<T>> empty(), (acc, child) -> acc.appendAll(traversePostOrder(child)))
      .append(node);
}

代码示例来源:origin: vavr-io/vavr

@Override
default List<T> removeFirst(Predicate<T> predicate) {
  Objects.requireNonNull(predicate, "predicate is null");
  List<T> init = empty();
  List<T> tail = this;
  while (!tail.isEmpty() && !predicate.test(tail.head())) {
    init = init.prepend(tail.head());
    tail = tail.tail();
  }
  if (tail.isEmpty()) {
    return this;
  } else {
    return init.foldLeft(tail.tail(), List::prepend);
  }
}

代码示例来源:origin: vavr-io/vavr

@Override
default List<T> filter(Predicate<? super T> predicate) {
  Objects.requireNonNull(predicate, "predicate is null");
  if (isEmpty()) {
    return this;
  } else {
    final List<T> filtered = foldLeft(empty(), (xs, x) -> predicate.test(x) ? xs.prepend(x) : xs);
    if (filtered.isEmpty()) {
      return empty();
    } else if (filtered.length() == length()) {
      return this;
    } else {
      return filtered.reverse();
    }
  }
}

代码示例来源:origin: vavr-io/vavr

static <T> Stream<Node<T>> traverseInOrder(Node<T> node) {
  if (node.isLeaf()) {
    return Stream.of(node);
  } else {
    final io.vavr.collection.List<Node<T>> children = node.getChildren();
    return children
        .tail()
        .foldLeft(Stream.<Node<T>> empty(), (acc, child) -> acc.appendAll(traverseInOrder(child)))
        .prepend(node)
        .prependAll(traverseInOrder(children.head()));
  }
}

代码示例来源:origin: vavr-io/vavr

@Override
default List<List<T>> permutations() {
  if (isEmpty()) {
    return Nil.instance();
  } else {
    final List<T> tail = tail();
    if (tail.isEmpty()) {
      return of(this);
    } else {
      final List<List<T>> zero = Nil.instance();
      return distinct().foldLeft(zero, (xs, x) -> {
        final Function<List<T>, List<T>> prepend = l -> l.prepend(x);
        return xs.appendAll(remove(x).permutations().map(prepend));
      });
    }
  }
}

代码示例来源:origin: com.pragmaticobjects.oo.atom/atom-basis

@Override
  public final boolean matches(T target) {
    return matchers.foldLeft(false, (v, m) -> v || m.matches(target));
  }
}

代码示例来源:origin: com.pragmaticobjects.oo.atom/atom-basis

@Override
  public final Builder<?> transitionResult(Builder<?> source, TypeDescription typeDescription) {
    return transitions.<Builder<?>>foldLeft(
      source,
      (state, transition) -> transition.transitionResult(state, typeDescription)
    );
  }
}

代码示例来源:origin: io.vavr/vavr

@Override
default List<T> prependAll(Iterable<? extends T> elements) {
  Objects.requireNonNull(elements, "elements is null");
  return isEmpty() ? ofAll(elements) : ofAll(elements).reverse().foldLeft(this, List::prepend);
}

代码示例来源:origin: io.vavr/vavr

/**
 * Counts the number of branches of this tree. The empty tree and a leaf have no branches.
 *
 * @return The number of branches of this tree.
 */
default int branchCount() {
  if (isEmpty() || isLeaf()) {
    return 0;
  } else {
    return getChildren().foldLeft(1, (count, child) -> count + child.branchCount());
  }
}

代码示例来源:origin: io.vavr/vavr

static <T> Stream<Node<T>> traversePreOrder(Node<T> node) {
  return node.getChildren().foldLeft(Stream.of(node),
      (acc, child) -> acc.appendAll(traversePreOrder(child)));
}

代码示例来源:origin: io.vavr/vavr

static <T> Stream<Node<T>> traversePostOrder(Node<T> node) {
  return node
      .getChildren()
      .foldLeft(Stream.<Node<T>> empty(), (acc, child) -> acc.appendAll(traversePostOrder(child)))
      .append(node);
}

代码示例来源:origin: io.vavr/vavr

static <T> Stream<Node<T>> traverseInOrder(Node<T> node) {
  if (node.isLeaf()) {
    return Stream.of(node);
  } else {
    final io.vavr.collection.List<Node<T>> children = node.getChildren();
    return children
        .tail()
        .foldLeft(Stream.<Node<T>> empty(), (acc, child) -> acc.appendAll(traverseInOrder(child)))
        .prepend(node)
        .prependAll(traverseInOrder(children.head()));
  }
}

相关文章