fj.data.List.head()方法的使用及代码示例

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

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

List.head介绍

[英]The first element of the linked list or fails for the empty list.
[中]链表的第一个元素或空列表的第一个元素失败。

代码示例

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Returns the last element of this list. Undefined for the empty list.
 *
 * @return The last element of this list or throws an error if this list is empty.
 */
public final A last() {
 A a = head();
 for (List<A> xs = tail(); xs.isNotEmpty(); xs = xs.tail())
  a = xs.head();
 return a;
}

代码示例来源:origin: org.functionaljava/functionaljava

public final Object[] toArrayObject() {
 final int length = length();
 final Object[] a = new Object[length];
 List<A> x = this;
 for (int i = 0; i < length; i++) {
  a[i] = x.head();
  x = x.tail();
 }
 return a;
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Returns the head of this list if there is one or the given argument if this list is empty.
 *
 * @param a The argument to return if this list is empty.
 * @return The head of this list if there is one or the given argument if this list is empty.
 */
public final A orHead(final F0<A> a) {
 return isEmpty() ? a.f() : head();
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Performs a left-fold reduction across this list. This function runs in constant space.
 */
public final A foldLeft1(final F2<A, A, A> f) {
 A x = head;
 for (List<A> xs = tail; !xs.isEmpty(); xs = xs.tail()) {
  x = f.f(x, xs.head());
 }
 return x;
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Returns the head of the list, if any.  Equivalent to {@link #toOption()} .
 *
 * @return The optional head of the list.
 */
public final Option<A> headOption() {
 return isEmpty() ? Option.none() : some(head());
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Performs a side-effect for each element of this list.
 *
 * @param f The side-effect to perform for the given element.
 */
public final void foreachDoEffect(final Effect1<A> f) {
 for (List<A> xs = this; xs.isNotEmpty(); xs = xs.tail()) {
  f.f(xs.head());
 }
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Appends the given list to this buffer.
 *
 * @param as The list to append to this buffer.
 * @return This buffer.
 */
public Buffer<A> append(final List<A> as) {
 for (List<A> xs = as; xs.isNotEmpty(); xs = xs.tail())
  snoc(xs.head());
 return this;
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Removes duplicates according to the given equality. Warning: O(n^2).
 *
 * @param eq Equality over the elements.
 * @return A list without duplicates.
 */
public final List<A> nub(final Equal<A> eq) {
 return isEmpty() ? this : cons(head(), tail().filter(a -> !eq.eq(a, head())).nub(eq));
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Returns whether or not all elements in the list are equal according to the given equality test.
 *
 * @param eq The equality test.
 * @return Whether or not all elements in the list are equal according to the given equality test.
 */
public final boolean allEqual(final Equal<A> eq) {
 return isEmpty() || tail().isEmpty() || eq.eq(head(), tail().head()) && tail().allEqual(eq);
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Returns an either projection of this list; the given argument in <code>Left</code> if empty, or
 * the first element in <code>Right</code>.
 *
 * @param x The value to return in left if this list is empty.
 * @return An either projection of this list.
 */
public final <X> Either<X, A> toEither(final F0<X> x) {
 return isEmpty() ? Either.left(x.f()) : Either.right(head());
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Intersperses the given argument between each element of this non empty list.
 *
 * @param a The separator to intersperse in this non empty list.
 * @return A non empty list with the given separator interspersed.
 */
public NonEmptyList<A> intersperse(final A a) {
 final List<A> list = toList().intersperse(a);
 return nel(list.head(), list.tail());
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Reverse this non empty list in constant stack space.
 *
 * @return A new non empty list with the elements in reverse order.
 */
public NonEmptyList<A> reverse() {
 final List<A> list = toList().reverse();
 return nel(list.head(), list.tail());
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Sorts this non empty list using the given order over elements using a <em>merge sort</em> algorithm.
 *
 * @param o The order over the elements of this non empty list.
 * @return A sorted non empty list according to the given order.
 */
public NonEmptyList<A> sort(final Ord<A> o) {
 final List<A> list = toList().sort(o);
 return nel(list.head(), list.tail());
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Zips this non empty list with the given non empty list to produce a list of pairs. If this list and the given list
 * have different lengths, then the longer list is normalised so this function never fails.
 *
 * @param bs The non empty list to zip this non empty list with.
 * @return A new non empty list with a length the same as the shortest of this list and the given list.
 */
public <B> NonEmptyList<P2<A, B>> zip(final NonEmptyList<B> bs) {
 final List<P2<A, B>> list = toList().zip(bs.toList());
 return nel(list.head(), list.tail());
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Returns a potential non-empty list from the given list. A non-value is returned if the given list is empty.
 *
 * @param as The list to construct a potential non-empty list with.
 * @return A potential non-empty list from the given list.
 */
public static <A> Option<NonEmptyList<A>> fromList(final List<A> as) {
 return as.isEmpty() ?
     Option.none() :
     some(nel(as.head(), as.tail()));
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Returns a stream projection of this list.
 *
 * @return A stream projection of this list.
 */
public final Stream<A> toStream() {
 return isEmpty() ? Stream.nil() : Stream.cons(head(), () -> tail().toStream());
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Transforms a non empty list of pairs into a non empty list of first components and
 * a non empty list of second components.
 *
 * @param xs The non empty list of pairs to transform.
 * @return A non empty list of first components and a non empty list of second components.
 */
public static <A, B> P2<NonEmptyList<A>, NonEmptyList<B>> unzip(final NonEmptyList<P2<A, B>> xs) {
 final P2<List<A>, List<B>> p = List.unzip(xs.toList());
 return P.p(nel(p._1().head(), p._1().tail()), nel(p._2().head(), p._2().tail()));
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Performs a right-fold reduction across this list in O(1) stack space.
 * @param f The function to apply on each element of the list.
 * @param b The beginning value to start the application from.
 * @return A Trampoline containing the final result after the right-fold reduction.
 */
public final <B> Trampoline<B> foldRightC(final F2<A, B, B> f, final B b) {
 return Trampoline.suspend(() -> isEmpty() ? Trampoline.pure(b) : tail().foldRightC(f, b).map(F2Functions.f(f, head())));
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Returns the list of initial segments of this list, shortest first.
 *
 * @return The list of initial segments of this list, shortest first.
 */
public final List<List<A>> inits() {
 List<List<A>> s = single(List.nil());
 if (isNotEmpty())
  s = s.append(tail().inits().map(List.<A>cons().f(head())));
 return s;
}

代码示例来源:origin: org.functionaljava/functionaljava

/**
 * Sequences through the right side of the either monad with a list of values.
 *
 * @param a The list of values to sequence with the either monad.
 * @return A sequenced value.
 */
public static <B, X> Either<X, List<B>> sequenceRight(final List<Either<X, B>> a) {
 return a.isEmpty() ?
     right(List.nil()) :
     a.head().right().bind(bb -> sequenceRight(a.tail()).right().map(cons_(bb)));
}

相关文章