diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata')
9 files changed, 99 insertions, 114 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java index 378a4a0..211f964 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java @@ -6,8 +6,7 @@ import java.util.function.Function; import bjc.utils.funcutils.ListUtils; -class ExtendedMap<KeyType, ValueType> - implements IMap<KeyType, ValueType> { +class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { private IMap<KeyType, ValueType> delegate; private IMap<KeyType, ValueType> store; diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java index 638ad7e..590ed1d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -166,8 +166,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { * IFunctionalList, java.util.function.BiFunction) */ @Override - public <T, F> IList<F> combineWith( - IList<T> rightList, + public <T, F> IList<F> combineWith(IList<T> rightList, BiFunction<E, T, F> itemCombiner) { if (rightList == null) { throw new NullPointerException( @@ -227,8 +226,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { * Function) */ @Override - public <T> IList<T> flatMap( - Function<E, IList<T>> elementExpander) { + public <T> IList<T> flatMap(Function<E, IList<T>> elementExpander) { if (elementExpander == null) { throw new NullPointerException("Expander must not be null"); } @@ -237,8 +235,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { this.wrappedList.size()); forEach(element -> { - IList<T> expandedElement = elementExpander - .apply(element); + IList<T> expandedElement = elementExpander.apply(element); if (expandedElement == null) { throw new NullPointerException( @@ -396,8 +393,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { * IFunctionalList) */ @Override - public <T> IList<IPair<E, T>> pairWith( - IList<T> rightList) { + public <T> IList<IPair<E, T>> pairWith(IList<T> rightList) { return combineWith(rightList, Pair<E, T>::new); } @@ -407,8 +403,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { * @see bjc.utils.funcdata.IFunctionalList#partition(int) */ @Override - public IList<IList<E>> partition( - int numberPerPartition) { + public IList<IList<E>> partition(int numberPerPartition) { if (numberPerPartition < 1 || numberPerPartition > wrappedList.size()) { throw new IllegalArgumentException("" + numberPerPartition diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java index 18617d1..3714bcd 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java @@ -155,8 +155,7 @@ public class FunctionalStringTokenizer { * The function to use to convert tokens. * @return A list containing all of the converted tokens. */ - public <E> IList<E> toList( - Function<String, E> tokenTransformer) { + public <E> IList<E> toList(Function<String, E> tokenTransformer) { if (tokenTransformer == null) { throw new NullPointerException("Transformer must not be null"); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java index 00ec653..e446b64 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java @@ -50,6 +50,29 @@ public interface IList<ContainedType> { boolean anyMatch(Predicate<ContainedType> matchPredicate); /** + * Reduce the contents of this list using a collector + * + * @param <StateType> + * The intermediate accumulation type + * @param <ReducedType> + * The final, reduced type + * @param collector + * The collector to use for reduction + * @return The reduced list + */ + public default <StateType, ReducedType> ReducedType collect( + Collector<ContainedType, StateType, ReducedType> collector) { + BiConsumer<StateType, ContainedType> accumulator = collector + .accumulator(); + + return reduceAux(collector.supplier().get(), (value, state) -> { + accumulator.accept(state, value); + + return state; + }, collector.finisher()); + } + + /** * Combine this list with another one into a new list and merge the * results. Works sort of like a combined zip/map over resulting pairs. * Does not change the underlying list. @@ -136,8 +159,8 @@ public interface IList<ContainedType> { * The predicate to match by * @return A list containing all elements that match the predicate */ - IList<ContainedType> - getMatching(Predicate<ContainedType> matchPredicate); + IList<ContainedType> getMatching( + Predicate<ContainedType> matchPredicate); /** * Retrieve the size of the wrapped list @@ -164,8 +187,8 @@ public interface IList<ContainedType> { * The function to apply to each element in the list * @return A new list containing the mapped elements of this list. */ - <MappedType> IList<MappedType> - map(Function<ContainedType, MappedType> elementTransformer); + <MappedType> IList<MappedType> map( + Function<ContainedType, MappedType> elementTransformer); /** * Zip two lists into a list of pairs @@ -178,8 +201,8 @@ public interface IList<ContainedType> { * @return A list containing pairs of this element and the specified * list */ - <OtherType> IList<IPair<ContainedType, OtherType>> - pairWith(IList<OtherType> rightList); + <OtherType> IList<IPair<ContainedType, OtherType>> pairWith( + IList<OtherType> rightList); /** * Partition this list into a list of sublists @@ -199,16 +222,6 @@ public interface IList<ContainedType> { void prepend(ContainedType item); /** - * Select a random item from this list, using the provided random - * number generator. - * - * @param rnd - * The random number generator to use. - * @return A random element from this list. - */ - ContainedType randItem(Function<Integer, Integer> rnd); - - /** * Select a random item from the list, using a default random number * generator * @@ -219,6 +232,16 @@ public interface IList<ContainedType> { } /** + * Select a random item from this list, using the provided random + * number generator. + * + * @param rnd + * The random number generator to use. + * @return A random element from this list. + */ + ContainedType randItem(Function<Integer, Integer> rnd); + + /** * Reduce this list to a single value, using a accumulative approach. * * @param <StateType> @@ -310,27 +333,4 @@ public interface IList<ContainedType> { * @return An iterable view onto the list */ public Iterable<ContainedType> toIterable(); - - /** - * Reduce the contents of this list using a collector - * - * @param <StateType> - * The intermediate accumulation type - * @param <ReducedType> - * The final, reduced type - * @param collector - * The collector to use for reduction - * @return The reduced list - */ - public default <StateType, ReducedType> ReducedType collect( - Collector<ContainedType, StateType, ReducedType> collector) { - BiConsumer<StateType, ContainedType> accumulator = - collector.accumulator(); - - return reduceAux(collector.supplier().get(), (value, state) -> { - accumulator.accept(state, value); - - return state; - }, collector.finisher()); - } }
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java index 243e58a..a2426b2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java @@ -95,8 +95,7 @@ public interface IMap<KeyType, ValueType> { * The function to use to transform values * @return The map where each value will be transformed after lookup */ - <V2> IMap<KeyType, V2> mapValues( - Function<ValueType, V2> transformer); + <V2> IMap<KeyType, V2> mapValues(Function<ValueType, V2> transformer); /** * Add an entry to the map diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java index cf54935..b620bd9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java @@ -18,8 +18,8 @@ import java.util.function.Function; */ final class TransformedValueMap<OldKey, OldValue, NewValue> implements IMap<OldKey, NewValue> { - private IMap<OldKey, OldValue> mapToTransform; - private Function<OldValue, NewValue> transformer; + private IMap<OldKey, OldValue> mapToTransform; + private Function<OldValue, NewValue> transformer; public TransformedValueMap(IMap<OldKey, OldValue> destMap, Function<OldValue, NewValue> transform) { diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java index 34b70d5..834c124 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java @@ -16,12 +16,12 @@ import bjc.utils.funcutils.StringUtils; * @param <ContainedType> */ public class Tree<ContainedType> implements ITree<ContainedType> { - private ContainedType data; + private ContainedType data; private IList<ITree<ContainedType>> children; - private boolean hasChildren; + private boolean hasChildren; - private int childCount = 0; + private int childCount = 0; /** * Create a new leaf node in a tree @@ -134,11 +134,10 @@ public class Tree<ContainedType> implements ITree<ContainedType> { Function<IList<NewType>, NewType> nodeTransformer = nodeCollapser .apply(data); - IList<NewType> collapsedChildren = children - .map((child) -> { - return child.collapse(leafTransform, nodeCollapser, - (subTreeVal) -> subTreeVal); - }); + IList<NewType> collapsedChildren = children.map((child) -> { + return child.collapse(leafTransform, nodeCollapser, + (subTreeVal) -> subTreeVal); + }); return nodeTransformer.apply(collapsedChildren); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java index 9cfc9a4..865f4d6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java @@ -74,8 +74,8 @@ public class BinarySearchTree<T> { * The distance from the pivot * @return Whether the adjusted pivot is with the list */ - private boolean adjustedPivotInBounds(IList<T> elements, - int pivot, int pivotAdjustment) { + private boolean adjustedPivotInBounds(IList<T> elements, int pivot, + int pivotAdjustment) { return (pivot - pivotAdjustment) >= 0 && (pivot + pivotAdjustment) < elements.getSize(); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java index e8c74fc..d3ebdac 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java @@ -14,6 +14,43 @@ import java.util.function.Function; */ public interface Bifunctor<LeftType, RightType> { /** + * Lift a pair of functions to a single function that maps over both + * parts of a pair + * + * @param <OldLeft> + * The old left type of the pair + * @param <OldRight> + * The old right type of the pair + * @param <NewLeft> + * The new left type of the pair + * @param <NewRight> + * The new right type of the pair + * @param leftFunc + * The function that maps over the left of the pair + * @param rightFunc + * The function that maps over the right of the pair + * @return A function that maps over both parts of the pair + */ + public default <OldLeft, OldRight, NewLeft, NewRight> Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, NewRight>> bimap( + Function<OldLeft, NewLeft> leftFunc, + Function<OldRight, NewRight> rightFunc) { + Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, NewRight>> bimappedFunc = ( + argPair) -> { + Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, OldRight>> leftMapper = argPair + .<OldLeft, OldRight, NewLeft> fmapLeft(leftFunc); + + Bifunctor<NewLeft, OldRight> leftMappedFunctor = leftMapper + .apply(argPair); + Function<Bifunctor<NewLeft, OldRight>, Bifunctor<NewLeft, NewRight>> rightMapper = leftMappedFunctor + .<NewLeft, OldRight, NewRight> fmapRight(rightFunc); + + return rightMapper.apply(leftMappedFunctor); + }; + + return bimappedFunc; + } + + /** * Lift a function to operate over the left part of this pair * * @param <OldLeft> @@ -27,9 +64,8 @@ public interface Bifunctor<LeftType, RightType> { * pair * @return The function lifted to work over the left side of bifunctors */ - public <OldLeft, OldRight, NewLeft> - Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, OldRight>> - fmapLeft(Function<OldLeft, NewLeft> func); + public <OldLeft, OldRight, NewLeft> Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, OldRight>> fmapLeft( + Function<OldLeft, NewLeft> func); /** * Lift a function to operate over the right part of this pair @@ -46,50 +82,8 @@ public interface Bifunctor<LeftType, RightType> { * @return The function lifted to work over the right side of * bifunctors */ - public <OldLeft, OldRight, NewRight> - Function<Bifunctor<OldLeft, OldRight>, Bifunctor<OldLeft, NewRight>> - fmapRight(Function<OldRight, NewRight> func); - - /** - * Lift a pair of functions to a single function that maps over both - * parts of a pair - * - * @param <OldLeft> - * The old left type of the pair - * @param <OldRight> - * The old right type of the pair - * @param <NewLeft> - * The new left type of the pair - * @param <NewRight> - * The new right type of the pair - * @param leftFunc - * The function that maps over the left of the pair - * @param rightFunc - * The function that maps over the right of the pair - * @return A function that maps over both parts of the pair - */ - public default <OldLeft, OldRight, NewLeft, NewRight> - Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, NewRight>> - bimap(Function<OldLeft, NewLeft> leftFunc, - Function<OldRight, NewRight> rightFunc) { - Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, NewRight>> bimappedFunc = - (argPair) -> { - Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, OldRight>> leftMapper = - argPair.<OldLeft, OldRight, NewLeft> fmapLeft( - leftFunc); - - Bifunctor<NewLeft, OldRight> leftMappedFunctor = - leftMapper.apply(argPair); - Function<Bifunctor<NewLeft, OldRight>, Bifunctor<NewLeft, NewRight>> rightMapper = - leftMappedFunctor - .<NewLeft, OldRight, NewRight> fmapRight( - rightFunc); - - return rightMapper.apply(leftMappedFunctor); - }; - - return bimappedFunc; - } + public <OldLeft, OldRight, NewRight> Function<Bifunctor<OldLeft, OldRight>, Bifunctor<OldLeft, NewRight>> fmapRight( + Function<OldRight, NewRight> func); /** * Get the value contained on the left of this bifunctor |
