From fefd6eb2917b9a0856c247353545cc13876b6eda Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Tue, 1 Dec 2020 20:19:34 -0500 Subject: An assortment of changes/new things --- .../java/bjc/utils/funcutils/IteratorUtils.java | 102 ++++++++++----------- 1 file changed, 50 insertions(+), 52 deletions(-) (limited to 'base/src/main/java/bjc/utils/funcutils/IteratorUtils.java') diff --git a/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java b/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java index 8d51996..ea67295 100644 --- a/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java @@ -3,7 +3,7 @@ package bjc.utils.funcutils; import java.util.*; import java.util.function.*; -import bjc.data.ArrayIterator; +import bjc.data.*; /** * Utility methods for dealing with iterators. @@ -12,58 +12,10 @@ import bjc.data.ArrayIterator; * */ public class IteratorUtils { - /** - * A chain iterator. This is essentially flatMap in iterator form. - * - * @author bjculkin - * - * @param - * The type of the input values. - * - * @param - * The type of the output values. - */ - public static class ChainIterator implements Iterator { - private Iterator mainItr; - private Function> trans; - - private Iterator curItr; - - /** - * Create a new chain iterator. - * - * @param mainItr - * The main iterator for input. - * - * @param trans - * The transformation to use to produce the outputs. - */ - public ChainIterator(Iterator mainItr, Function> trans) { - this.mainItr = mainItr; - this.trans = trans; - } - - @Override - public boolean hasNext() { - if (curItr != null) { - return curItr.hasNext() ? true : mainItr.hasNext(); - } - - return mainItr.hasNext(); - } - - @Override - public T2 next() { - if (curItr == null || !curItr.hasNext()) { - curItr = trans.apply(mainItr.next()); - } - - return curItr.next(); - } - } - /** * Convert an iterator to an iterable. + * + * @param The type being iterated over. * * @param itr * The iterator to convert. @@ -77,6 +29,8 @@ public class IteratorUtils { /** * Convert an iterable to an iterator. * + * @param The type being iterated over. + * * @param itr * The iterable to convert. * @@ -89,18 +43,23 @@ public class IteratorUtils { /** * Convert an array to an iterator. * + * @param The type being iterated over. + * * @param parms * The array to iterate over. * * @return An iterator over the provided array. */ @SafeVarargs - public static Iterator AI(E... parms) { + public static Iterator I(E... parms) { return new ArrayIterator<>(parms); } /** * Create a chain iterator. + * + * @param The initial type being iterated over. + * @param The resulting type being iterated over. * * @param itrA * The iterator for input values. @@ -114,4 +73,43 @@ public class IteratorUtils { Function> itrB) { return new ChainIterator<>(itrA, itrB); } + + /** + * Perform a left-fold over an iterator. + * + * @param The type of elements in the iterator. + * @param The result from the fold. + * + * @param itr The items to iterate over. + * @param zero The initial element for the fold. + * @param folder The function that does the folding. + * + * @return The result of folding over the iterator. + */ + public static ResultType foldLeft( + Iterable itr, + ResultType zero, + BiFunction folder) + { + ResultType state = zero; + for (ElementType elem : itr) { + state = folder.apply(elem, state); + } + return state; + } + + /** + * Creates an 'entangled' pair of a consumer and an iterator. + * + * @param The type of value involved. + * + * @return A pair consisting of a consumer of values, and an iterator that + * yields the consumed values. + */ + public static + IPair, Iterator> entangle() + { + Queue backer = new ArrayDeque<>(); + return IPair.pair(backer::add, new QueueBackedIterator<>(backer)); + } } -- cgit v1.2.3