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

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

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

List.empty介绍

[英]Returns the single instance of Nil. Convenience method for Nil.instance() .

Note: this method intentionally returns type List and not Nil. This comes handy when folding. If you explicitly need type Nil use Nil#instance().
[中]返回Nil的单个实例。零的简便方法。实例()。
注意:此方法有意返回类型列表,而不是Nil。这在折叠时很方便。如果明确需要Nil类型,请使用Nil#instance()。

代码示例

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

/**
 * Alias for {@link List#empty()}
 *
 * @param <T> Component type of element.
 * @return A singleton instance of empty {@link List}
 */
public static <T> Seq<T> Seq() {
  return List.empty();
}

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

/**
 * Alias for {@link List#empty()}
 *
 * @param <T> Component type of element.
 * @return A singleton instance of empty {@link List}
 */
public static <T> List<T> List() {
  return List.empty();
}

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

@Override
public Seq<?> toSeq() {
  return List.empty();
}

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

/**
 * Returns a new Node containing the given value and having no children.
 *
 * @param value A value
 * @param <T>   Value type
 * @return A new Node instance.
 */
static <T> Node<T> of(T value) {
  return new Node<>(value, io.vavr.collection.List.empty());
}

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

/**
 * Converts this to a {@link List}.
 *
 * @return A new {@link List}.
 */
default List<T> toList() {
  return ValueModule.toTraversable(this, List.empty(), List::of, List::ofAll);
}

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

/**
 * Returns a List containing {@code n} times the given {@code element}
 *
 * @param <T>     Component type of the List
 * @param n       The number of elements in the List
 * @param element The element
 * @return A List of size {@code n}, where each element is the given {@code element}.
 */
static <T> List<T> fill(int n, T element) {
  return Collections.fillObject(n, element, empty(), List::of);
}

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

@Override
default Iterator<List<T>> crossProduct(int power) {
  return Collections.crossProduct(empty(), this, power);
}

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

/**
 * Returns a List containing {@code n} values of a given Function {@code f}
 * over a range of integer values from 0 to {@code n - 1}.
 *
 * @param <T> Component type of the List
 * @param n   The number of elements in the List
 * @param f   The Function computing element values
 * @return A List consisting of elements {@code f(0),f(1), ..., f(n - 1)}
 * @throws NullPointerException if {@code f} is null
 */
static <T> List<T> tabulate(int n, Function<? super Integer, ? extends T> f) {
  Objects.requireNonNull(f, "f is null");
  return Collections.tabulate(n, f, empty(), List::of);
}

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

@Override
default <U> List<U> map(Function<? super T, ? extends U> mapper) {
  Objects.requireNonNull(mapper, "mapper is null");
  List<U> list = empty();
  for (T t : this) {
    list = list.prepend(mapper.apply(t));
  }
  return list.reverse();
}

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

@Override
default <U> List<U> flatMap(Function<? super T, ? extends Iterable<? extends U>> mapper) {
  Objects.requireNonNull(mapper, "mapper is null");
  List<U> list = empty();
  for (T t : this) {
    for (U u : mapper.apply(t)) {
      list = list.prepend(u);
    }
  }
  return list.reverse();
}

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

/**
 * Creates a Queue that contains the elements of the given {@link java.util.stream.Stream}.
 *
 * @param javaStream A {@link java.util.stream.Stream}
 * @param <T>        Component type of the Stream.
 * @return A Queue containing the given elements in the same order.
 */
public static <T> Queue<T> ofAll(java.util.stream.Stream<? extends T> javaStream) {
  Objects.requireNonNull(javaStream, "javaStream is null");
  return new Queue<>(io.vavr.collection.List.ofAll(javaStream), io.vavr.collection.List.empty());
}

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

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

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

@Override
public io.vavr.collection.List<T> toList() {
  io.vavr.collection.List<T> results = io.vavr.collection.List.empty();
  for (PriorityQueue<T> queue = this; !queue.isEmpty(); ) {
    final Tuple2<T, PriorityQueue<T>> dequeue = queue.dequeue();
    results = results.prepend(dequeue._1);
    queue = dequeue._2;
  }
  return results.reverse();
}

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

@Override
default Stream<T> dropRight(int n) {
  if (n <= 0) {
    return this;
  } else {
    return DropRight.apply(take(n).toList(), List.empty(), drop(n));
  }
}

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

@Override
default List<T> dropRight(int n) {
  if (n <= 0) {
    return this;
  }
  if (n >= length()) {
    return empty();
  }
  return ofAll(iterator().dropRight(n));
}

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

@Override
default List<T> takeRight(int n) {
  if (n <= 0) {
    return empty();
  }
  if (n >= length()) {
    return this;
  }
  return reverse().take(n).reverse();
}

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

static <T> Iterator<T> reverseIterator(Iterable<T> iterable) {
  if (iterable instanceof java.util.List) {
    return reverseListIterator((java.util.List<T>) iterable);
  } else if (iterable instanceof Seq) {
    return ((Seq<T>) iterable).reverseIterator();
  } else {
    return List.<T>empty().pushAll(iterable).iterator();
  }
}

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

@Override
default Tuple2<List<T>, List<T>> splitAtInclusive(Predicate<? super T> predicate) {
  if (isEmpty()) {
    return Tuple.of(empty(), empty());
  } else {
    final Tuple2<List<T>, List<T>> t = SplitAt.splitByPredicateReversed(this, predicate);
    if (t._2.isEmpty() || t._2.tail().isEmpty()) {
      return Tuple.of(this, empty());
    } else {
      return Tuple.of(t._1.prepend(t._2.head()).reverse(), t._2.tail());
    }
  }
}

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

static <T> List<List<T>> apply(List<T> elements, int k) {
    if (k == 0) {
      return List.of(List.empty());
    } else {
      return elements.zipWithIndex().flatMap(
          t -> apply(elements.drop(t._2 + 1), (k - 1)).map(c -> c.prepend(t._1))
      );
    }
  }
}

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

static <T> Stream<T> apply(io.vavr.collection.List<T> front, io.vavr.collection.List<T> rear, Stream<T> remaining) {
    if (remaining.isEmpty()) {
      return remaining;
    } else if (front.isEmpty()) {
      return apply(rear.reverse(), io.vavr.collection.List.empty(), remaining);
    } else {
      return Stream.cons(front.head(),
          () -> apply(front.tail(), rear.prepend(remaining.head()), remaining.tail()));
    }
  }
}

相关文章