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

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

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

List.head介绍

暂无

代码示例

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

/**
 * Returns the head element without modifying the List.
 *
 * @return the first element
 * @throws java.util.NoSuchElementException if this List is empty
 */
default T peek() {
  if (isEmpty()) {
    throw new NoSuchElementException("peek of empty list");
  }
  return head();
}

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

/**
 * Performs an action on the head element of this {@code List}.
 *
 * @param action A {@code Consumer}
 * @return this {@code List}
 */
@Override
default List<T> peek(Consumer<? super T> action) {
  Objects.requireNonNull(action, "action is null");
  if (!isEmpty()) {
    action.accept(head());
  }
  return this;
}

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

@Override
default List<T> dropWhile(Predicate<? super T> predicate) {
  Objects.requireNonNull(predicate, "predicate is null");
  List<T> list = this;
  while (!list.isEmpty() && predicate.test(list.head())) {
    list = list.tail();
  }
  return list;
}

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

@Override
default int indexOf(T element, int from) {
  int index = 0;
  for (List<T> list = this; !list.isEmpty(); list = list.tail(), index++) {
    if (index >= from && Objects.equals(list.head(), element)) {
      return index;
    }
  }
  return -1;
}

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

@Override
public T head() {
  if (isEmpty()) {
    throw new NoSuchElementException("head of empty " + stringPrefix());
  } else {
    return front.head();
  }
}

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

@Override
default int lastIndexOf(T element, int end) {
  int result = -1, index = 0;
  for (List<T> list = this; index <= end && !list.isEmpty(); list = list.tail(), index++) {
    if (Objects.equals(list.head(), element)) {
      result = index;
    }
  }
  return result;
}

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

/**
 * Write an object to a serialization stream.
 *
 * @param s An object serialization stream.
 * @throws java.io.IOException If an error occurs writing to the stream.
 */
private void writeObject(ObjectOutputStream s) throws IOException {
  s.defaultWriteObject();
  s.writeInt(list.length());
  for (List<T> l = list; !l.isEmpty(); l = l.tail()) {
    s.writeObject(l.head());
  }
}

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

static <T> Tuple2<List<T>, List<T>> splitByPredicateReversed(List<T> source, Predicate<? super T> predicate) {
    Objects.requireNonNull(predicate, "predicate is null");
    List<T> init = Nil.instance();
    List<T> tail = source;
    while (!tail.isEmpty() && !predicate.test(tail.head())) {
      init = init.prepend(tail.head());
      tail = tail.tail();
    }
    return Tuple.of(init, tail);
  }
}

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

@Override
public T last() {
  return rear.isEmpty() ? front.last() : rear.head();
}

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

private void drawAux(String indent, StringBuilder builder) {
  builder.append(value);
  for (io.vavr.collection.List<Node<T>> it = children; !it.isEmpty(); it = it.tail()) {
    final boolean isLast = it.tail().isEmpty();
    builder.append('\n')
        .append(indent)
        .append(isLast ? "└──" : "├──");
    it.head().drawAux(indent + (isLast ? "   " : "│  "), builder);
  }
}

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

/**
 * Removes the head element from this List.
 *
 * @return a tuple containing the head element and the remaining elements of this List
 * @throws java.util.NoSuchElementException if this List is empty
 */
default Tuple2<T, List<T>> pop2() {
  if (isEmpty()) {
    throw new NoSuchElementException("pop2 of empty list");
  }
  return Tuple.of(head(), tail());
}

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

@Override
default List<T> takeWhile(Predicate<? super T> predicate) {
  Objects.requireNonNull(predicate, "predicate is null");
  List<T> result = Nil.instance();
  for (List<T> list = this; !list.isEmpty() && predicate.test(list.head()); list = list.tail()) {
    result = result.prepend(list.head());
  }
  return result.length() == length() ? this : result.reverse();
}

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

/**
 * Returns the head element without modifying the List.
 *
 * @return {@code None} if this List is empty, otherwise a {@code Some} containing the head element
 */
default Option<T> peekOption() {
  return isEmpty() ? Option.none() : Option.some(head());
}

代码示例来源: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

/**
 * Removes the head element from this List.
 *
 * @return {@code None} if this List is empty, otherwise {@code Some} {@code Tuple} containing the head element and the remaining elements of this List
 */
default Option<Tuple2<T, List<T>>> pop2Option() {
  return isEmpty() ? Option.none() : Option.some(Tuple.of(head(), pop()));
}

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

@Override
default List<T> take(int n) {
  if (n <= 0) {
    return empty();
  }
  if (n >= length()) {
    return this;
  }
  List<T> result = Nil.instance();
  List<T> list = this;
  for (int i = 0; i < n; i++, list = list.tail()) {
    result = result.prepend(list.head());
  }
  return result.reverse();
}

代码示例来源: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()));
    }
  }
}

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

@Override
default Tuple2<List<T>, List<T>> splitAt(int n) {
  if (isEmpty()) {
    return Tuple.of(empty(), empty());
  } else {
    List<T> init = Nil.instance();
    List<T> tail = this;
    while (n > 0 && !tail.isEmpty()) {
      init = init.prepend(tail.head());
      tail = tail.tail();
      n--;
    }
    return Tuple.of(init.reverse(), tail);
  }
}

代码示例来源: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 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());
    }
  }
}

相关文章