fj.data.List类的使用及代码示例

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

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

List介绍

[英]Provides an in-memory, immutable, singly linked list.
[中]提供内存中不可变的单链接列表。

代码示例

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

/**
 * If list contains a failure, returns a failure of the reduction of
 * all the failures using the semigroup, otherwise returns the successful list.
 */
public static <E, A> Validation<E, List<A>> sequence(final Semigroup<E> s, final List<Validation<E, A>> list) {
 if (list.exists(Validation::isFail)) {
  return Validation.fail(list.filter(Validation::isFail).map(v -> v.fail()).foldLeft1((F2<E, E, E>) s::sum));
 } else {
  return success(list.foldLeft((List<A> acc, Validation<E, A> v) -> acc.cons(v.success()), List.nil()).reverse());
 }
}

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

/**
 * Returns the list of final segments of this list, longest first.
 *
 * @return The list of final segments of this list, longest first.
 */
public final List<List<A>> tails() {
 return isEmpty() ? single(List.nil()) : cons(this, tail().tails());
}

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

/**
 * Reverse this list in constant stack space.
 *
 * @return A new list that is the reverse of this one.
 */
public final List<A> reverse() {
 return foldLeft((as, a) -> cons(a, as), nil());
}

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

/**
 * 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

/**
 * If the list contains a failure, returns a Validation of the list of
 * fails in the list, otherwise returns a successful Validation with
 * the list of successful values.  Does not accumulate the failures into a
 * single failure using a semigroup.
 */
 public static <A, E> Validation<List<E>, List<A>> sequenceNonCumulative(List<Validation<E, A>> list) {
  if (list.exists(Validation::isFail)) {
   F2<List<E>, Validation<E, A>, List<E>> f = (acc, v) -> acc.cons(v.fail());
   return fail(list.filter(Validation::isFail).foldLeft(f, List.nil()).reverse());
  } else {
   F2<List<A>, Validation<E, A>, List<A>> f = (acc, v) -> acc.cons(v.success());
   return success(list.filter(Validation::isSuccess).foldLeft(f, List.nil()).reverse());
  }
 }

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

/**
 * Turns a List of Callables into a single Callable of a List.
 *
 * @param as The list of callables to transform.
 * @return A single callable for the given List.
 */
public static <A> Callable<List<A>> sequence(final List<Callable<A>> as) {
 return as.foldRight(Callables.liftM2(List.cons()), callable(List.nil()));
}

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

public final <B> List<List<B>> traverseList(final F<A, List<B>> f) {
 return foldRight(
   (a, acc) -> f.f(a).bind(b -> acc.map(bs -> bs.cons(b))),
   single(List.nil()));
}

代码示例来源:origin: no.arktekk.unix/unix-sysv-pkg

public List<String> toList()
{
  F<List<String>, String> folder = List.<String, String>foldLeft().f( joiner.f( " " ) ).f( "" );
  F<List<String>, String> stringF = FunctionF.compose( curry( concat, "CLASSES=" ), trim, folder );
  List<Option<String>> list = List.<Option<String>>single( some( "ARCH=" + arch ) ).
    cons( some( "CATEGORY=" + category ) ).
    cons( some( "NAME=" + name ) ).
    cons( some( "PKG=" + pkg ) ).
    cons( some( "VERSION=" + version ) ).
    cons( pstamp.map( curry( concat, "PSTAMP=" ) ) ).
    cons( desc.map( curry( concat, "DESC=" ) ) ).
    cons( email.map( curry( concat, "EMAIL=" ) ) ).
    cons( iif( List.<String>isNotEmpty_(), classes ).map( stringF ) );
  return Option.somes( list ).reverse();
}

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

/**
 * Evaluate each action in the sequence from left to right, and collect the results.
 */
public static <S, A> State<S, List<A>> sequence(List<State<S, A>> list) {
 return list
   .foldLeft(
     (acc, ts) -> acc.flatMap(as -> ts.map(a -> cons(a, as))),
     State.<S, List<A>>constant(List.nil()))
   .map(as -> as.reverse());
}

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

代码示例来源:origin: ontop/ontop

/**
 * Folds a list of boolean atoms into one AND(AND(...)) boolean atom.
 */
public Expression foldBooleanConditions(List<Function> booleanAtoms) {
  if (booleanAtoms.length() == 0)
    return TRUE_EQ;
  Expression firstBooleanAtom = convertOrCastIntoBooleanAtom( booleanAtoms.head());
  return booleanAtoms.tail().foldLeft(new F2<Expression, Function, Expression>() {
    @Override
    public Expression f(Expression previousAtom, Function currentAtom) {
      return termFactory.getFunctionAND(previousAtom, currentAtom);
    }
  }, firstBooleanAtom);
}

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

/**
 * Takes the first 2 elements of the list and applies the function to them,
 * then applies the function to the result and the third element and so on.
 *
 * @param f The function to apply on each element of the list.
 * @return The final result after the left-fold reduction.
 */
public final A foldLeft1(final F2<A, A, A> f) {
 if (isEmpty())
  throw error("Undefined: foldLeft1 on empty list");
 return tail().foldLeft(f, head());
}

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

/**
  * Returns a list representation of this set in reverse order.
  *
  * @return a list representation of this set in reverse order.
  */
 public final List<A> toListReverse() {
   return foldMapRight(List.cons(List.nil()), Monoid.listMonoid());
 }

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

/**
 * 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

/**
 * 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

/**
 * Projects an immutable collection of this buffer.
 *
 * @return An immutable collection of this buffer.
 */
public Collection<A> toCollection() {
 return start.toCollection();
}

相关文章