net.sourceforge.cilib.util.functions.Utils Maven / Gradle / Ivy
/** __ __
* _____ _/ /_/ /_ Computational Intelligence Library (CIlib)
* / ___/ / / / __ \ (c) CIRG @ UP
* / /__/ / / / /_/ / http://cilib.net
* \___/_/_/_/_.___/
*/
package net.sourceforge.cilib.util.functions;
import com.google.common.base.Predicate;
import fj.Equal;
import fj.F;
import fj.F2;
import fj.Function;
import fj.Ord;
import fj.Ordering;
import fj.P2;
import fj.data.List;
public final class Utils {
private Utils() {}
public static > F> iterableList() {
return new F>() {
@Override
public List f(I a) {
return List.iterableList(a);
}
};
}
public static F, T>> index() {
return new F, T>>() {
@Override
public F, T> f(final Integer a) {
return new F, T>() {
@Override
public T f(List b) {
return b.index(a);
}
};
}
};
}
public static F precision(final int p) {
return new F() {
@Override
public Double f(Double a) {
return Math.round(Math.pow(10, p) * a) / Math.pow(10, p);
}
};
}
public static F2, List, List> pairwise(final F> f) {
return new F2, List, List>() {
@Override
public List f(List a, List b) {
return a.zip(b).map(new F, A>() {
@Override
public A f(P2 a) {
return f.f(a._1()).f(a._2());
}
});
}
};
}
public static F predicate(final Predicate pred) {
return new F() {
@Override
public Boolean f(T a) {
return pred.apply(a);
}
};
}
public static Ord equalOrd() {
return Ord.ord(Function.curry(new F2() {
@Override
public Ordering f(T a, T b) {
return Ordering.EQ;
}
}));
}
public static Equal alwaysEqual() {
return Equal.equal(Function.curry(new F2() {
@Override
public Boolean f(T a, T b) {
return true;
}
}));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy