summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/funcutils/FuncUtils.java
diff options
context:
space:
mode:
authorBenjamin J. Culkin <bjculkin@mix.wvu.edu>2018-10-16 06:11:39 -0300
committerBenjamin J. Culkin <bjculkin@mix.wvu.edu>2018-10-16 06:11:39 -0300
commitd2be5b73d7a5653ad5c8273c17284346baa6f1c7 (patch)
tree9d3c6adb193f53588bd5d004fdf80c0381685351 /base/src/main/java/bjc/utils/funcutils/FuncUtils.java
parent0308029629a12711b849ea7765639b9b1f9e03d2 (diff)
parentd1d01769e7c55f7f62dc01cadf420d5f63424584 (diff)
Merge branch 'master' of github.com:bculkin2442/bjc-utils2
Diffstat (limited to 'base/src/main/java/bjc/utils/funcutils/FuncUtils.java')
-rw-r--r--base/src/main/java/bjc/utils/funcutils/FuncUtils.java43
1 files changed, 32 insertions, 11 deletions
diff --git a/base/src/main/java/bjc/utils/funcutils/FuncUtils.java b/base/src/main/java/bjc/utils/funcutils/FuncUtils.java
index 2e55a3d..ff9fefb 100644
--- a/base/src/main/java/bjc/utils/funcutils/FuncUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/FuncUtils.java
@@ -1,6 +1,7 @@
package bjc.utils.funcutils;
import java.util.function.BiFunction;
+import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.UnaryOperator;
@@ -16,16 +17,16 @@ public class FuncUtils {
* function.
*
* @param <A>
- * The initial type of the function.
+ * The initial type of the function.
*
* @param <B>
- * The intermediate type of the function.
+ * The intermediate type of the function.
*
* @param <C>
- * The terminal type of the function.
+ * The terminal type of the function.
*
* @param func
- * The function to transform.
+ * The function to transform.
*
* @return The function transformed into a unary function returning a
* function.
@@ -40,13 +41,13 @@ public class FuncUtils {
* Do the specified action the specified number of times.
*
* @param nTimes
- * The number of times to do the action.
+ * The number of times to do the action.
*
* @param cons
- * The action to perform.
+ * The action to perform.
*/
public static void doTimes(final int nTimes, final Consumer<Integer> cons) {
- for(int i = 0; i < nTimes; i++) {
+ for (int i = 0; i < nTimes; i++) {
cons.accept(i);
}
}
@@ -55,15 +56,35 @@ public class FuncUtils {
* Return an operator that executes until it converges.
*
* @param op
- * The operator to execute.
+ * The operator to execute.
*
* @param maxTries
- * The maximum amount of times to apply the function in an
- * attempt to cause it to converge.
+ * The maximum amount of times to apply the function in
+ * an attempt to cause it to converge.
*
* @return The requested operator.
*/
public static <T> UnaryOperator<T> converge(final UnaryOperator<T> op, final int maxTries) {
+ return converge(op, (nw, old) -> nw.equals(old), maxTries);
+ }
+
+ /**
+ * Return an operator that executes until it converges.
+ *
+ * @param op
+ * The operator to execute.
+ * @param converged
+ * The predicate to execute to check if the function has
+ * converged.
+ *
+ * @param maxTries
+ * The maximum amount of times to apply the function in
+ * an attempt to cause it to converge.
+ *
+ * @return The requested operator.
+ */
+ public static <T> UnaryOperator<T> converge(final UnaryOperator<T> op, final BiPredicate<T, T> converged,
+ final int maxTries) {
return (val) -> {
T newVal = op.apply(val);
T oldVal;
@@ -75,7 +96,7 @@ public class FuncUtils {
newVal = op.apply(newVal);
tries += 1;
- } while(!newVal.equals(oldVal) && tries < maxTries);
+ } while (!converged.test(newVal, oldVal) && tries < maxTries);
return newVal;
};