本文整理了Java中fj.data.List.foldLeft()
方法的一些代码示例,展示了List.foldLeft()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。List.foldLeft()
方法的具体详情如下:
包路径:fj.data.List
类名称:List
方法名:foldLeft
[英]Provides a first-class version of foldLeft.
[中]提供foldLeft的一流版本。
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Returns the product of a list of doubles.
*
* @param doubles A list of doubles to multiply together.
* @return The product of the doubles in the list.
*/
public static double product(final List<Double> doubles) {
return doubles.foldLeft((x, y) -> x * y, 1.0);
}
代码示例来源: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
/**
* Sums a list of doubles.
*
* @param doubles A list of doubles to sum.
* @return The sum of the doubless in the list.
*/
public static double sum(final List<Double> doubles) {
return doubles.foldLeft((x, y) -> x + y, 0.0);
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* The length of this list.
*
* @return The length of this list.
*/
public final int length() {
return foldLeft((i, a) -> i + 1, 0);
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Performs a left-fold reduction across this list. This function runs in constant space.
*
* @param f The function to apply on each element of the list.
* @param b The beginning value to start the application from.
* @return The final result after the left-fold reduction.
*/
public final <B> B foldLeft(final F<B, F<A, B>> f, final B b) {
return foldLeft(uncurryF2(f), b);
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Provides a first-class version of foldLeft.
*
* @return The left fold function for lists.
*/
public static <A, B> F<F<B, F<A, B>>, F<B, F<List<A>, B>>> foldLeft() {
return curry((f, b, as) -> as.foldLeft(f, b));
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Sums the given values with left-fold.
*
* @param as The values to sum.
* @return The sum of the given values.
*/
public A sumLeft(final List<A> as) {
return as.foldLeft(def::append, def.empty());
}
代码示例来源:origin: no.arktekk.unix/unix-sysv-pkg
public String toString()
{
String eol = System.getProperty( "line.separator" );
return toList().foldLeft( joiner.f( eol ), "" ).trim() + eol;
}
代码示例来源:origin: com.stratio.mojo.unix/unix-sysv-pkg
public String toString()
{
return toList().foldLeft( joiner.f( EOL ), "" ).trim() + EOL;
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Performs a right-fold reduction across this list. This function uses O(length) 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 The final result after the right-fold reduction.
*/
public final <B> B foldRight(final F2<A, B, B> f, final B b) {
return reverse().foldLeft(flip(f), b);
}
代码示例来源: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
/**
* Adds nodes using the list of products with priority k and value a. This operation takes O(list.length()).
*/
public PriorityQueue<K, A> enqueue(List<P2<K, A>> list) {
return list.foldLeft((pq, p) -> pq.enqueue(p._1(), p._2()), this);
}
代码示例来源:origin: org.functionaljava/functionaljava
/**
* Adds the product of key-value (k, v) pairs to the trie.
*/
public HashArrayMappedTrie<K, V> set(final List<P2<K, V>> list) {
return list.foldLeft(h -> p -> h.set(p._1(), p._2()), this);
}
代码示例来源: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: it.unibz.inf.ontop/ontop-model
/**
* Folds a list of boolean atoms into one AND(AND(...)) boolean atom.
*/
public static 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 TERM_FACTORY.getFunctionAND(previousAtom, currentAtom);
}
}, firstBooleanAtom);
}
代码示例来源: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
/**
* 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
/**
* 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
/**
* Map each element of a structure to an action, evaluate these actions from left to right
* and collect the results.
*/
public static <S, A, B> State<S, List<B>> traverse(List<A> list, F<A, State<S, B>> f) {
return list
.foldLeft(
(acc, a) -> acc.flatMap(bs -> f.f(a).map(b -> cons(b, bs))),
State.<S, List<B>>constant(List.nil()))
.map(bs -> bs.reverse());
}
代码示例来源:origin: novarto-oss/sane-dbc
@Test
public void foldLeft()
{
FoldLeftSelectOp<Integer> fold = new FoldLeftSelectOp<>("SELECT * FROM MySqlTest_IDS", NO_BINDER,
(x, rs) -> x + rs.getInt(1), 0);
Integer result = DB.submit(fold);
assertThat(result, is(0));
DB.submit(insertKeysOp(asList("a", "b", "c", "d")));
Integer expected = DB.submit(new SelectOp.FjList<>("SELECT ID FROM MySqlTest_IDS", NO_BINDER, x -> x.getInt(1)))
.foldLeft((x, y) -> x + y, 0);
result = DB.submit(fold);
assertThat(result, is(expected));
}
内容来源于网络,如有侵权,请联系作者删除!