From dca8e9f586fd595a7995f07788318fb92b8cce79 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 28 Jul 2016 16:44:36 -0400 Subject: Format/Cleanup pass --- .../main/java/bjc/utils/funcdata/ExtendedMap.java | 3 +- .../java/bjc/utils/funcdata/FunctionalList.java | 15 ++-- .../utils/funcdata/FunctionalStringTokenizer.java | 3 +- .../src/main/java/bjc/utils/funcdata/IList.java | 78 +++++++++---------- .../src/main/java/bjc/utils/funcdata/IMap.java | 3 +- .../bjc/utils/funcdata/TransformedValueMap.java | 4 +- .../src/main/java/bjc/utils/funcdata/Tree.java | 15 ++-- .../bjc/utils/funcdata/bst/BinarySearchTree.java | 4 +- .../java/bjc/utils/funcdata/theory/Bifunctor.java | 88 ++++++++++------------ 9 files changed, 99 insertions(+), 114 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata') 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 - implements IMap { +class ExtendedMap implements IMap { private IMap delegate; private IMap 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 implements Cloneable, IList { * IFunctionalList, java.util.function.BiFunction) */ @Override - public IList combineWith( - IList rightList, + public IList combineWith(IList rightList, BiFunction itemCombiner) { if (rightList == null) { throw new NullPointerException( @@ -227,8 +226,7 @@ public class FunctionalList implements Cloneable, IList { * Function) */ @Override - public IList flatMap( - Function> elementExpander) { + public IList flatMap(Function> elementExpander) { if (elementExpander == null) { throw new NullPointerException("Expander must not be null"); } @@ -237,8 +235,7 @@ public class FunctionalList implements Cloneable, IList { this.wrappedList.size()); forEach(element -> { - IList expandedElement = elementExpander - .apply(element); + IList expandedElement = elementExpander.apply(element); if (expandedElement == null) { throw new NullPointerException( @@ -396,8 +393,7 @@ public class FunctionalList implements Cloneable, IList { * IFunctionalList) */ @Override - public IList> pairWith( - IList rightList) { + public IList> pairWith(IList rightList) { return combineWith(rightList, Pair::new); } @@ -407,8 +403,7 @@ public class FunctionalList implements Cloneable, IList { * @see bjc.utils.funcdata.IFunctionalList#partition(int) */ @Override - public IList> partition( - int numberPerPartition) { + public IList> 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 IList toList( - Function tokenTransformer) { + public IList toList(Function 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 @@ -49,6 +49,29 @@ public interface IList { */ boolean anyMatch(Predicate matchPredicate); + /** + * Reduce the contents of this list using a collector + * + * @param + * The intermediate accumulation type + * @param + * The final, reduced type + * @param collector + * The collector to use for reduction + * @return The reduced list + */ + public default ReducedType collect( + Collector collector) { + BiConsumer 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. @@ -136,8 +159,8 @@ public interface IList { * The predicate to match by * @return A list containing all elements that match the predicate */ - IList - getMatching(Predicate matchPredicate); + IList getMatching( + Predicate matchPredicate); /** * Retrieve the size of the wrapped list @@ -164,8 +187,8 @@ public interface IList { * The function to apply to each element in the list * @return A new list containing the mapped elements of this list. */ - IList - map(Function elementTransformer); + IList map( + Function elementTransformer); /** * Zip two lists into a list of pairs @@ -178,8 +201,8 @@ public interface IList { * @return A list containing pairs of this element and the specified * list */ - IList> - pairWith(IList rightList); + IList> pairWith( + IList rightList); /** * Partition this list into a list of sublists @@ -198,16 +221,6 @@ public interface IList { */ 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 rnd); - /** * Select a random item from the list, using a default random number * generator @@ -218,6 +231,16 @@ public interface IList { return randItem((num) -> (int) (Math.random() * num)); } + /** + * 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 rnd); + /** * Reduce this list to a single value, using a accumulative approach. * @@ -310,27 +333,4 @@ public interface IList { * @return An iterable view onto the list */ public Iterable toIterable(); - - /** - * Reduce the contents of this list using a collector - * - * @param - * The intermediate accumulation type - * @param - * The final, reduced type - * @param collector - * The collector to use for reduction - * @return The reduced list - */ - public default ReducedType collect( - Collector collector) { - BiConsumer 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 { * The function to use to transform values * @return The map where each value will be transformed after lookup */ - IMap mapValues( - Function transformer); + IMap mapValues(Function 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 implements IMap { - private IMap mapToTransform; - private Function transformer; + private IMap mapToTransform; + private Function transformer; public TransformedValueMap(IMap destMap, Function 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 */ public class Tree implements ITree { - private ContainedType data; + private ContainedType data; private IList> 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 implements ITree { Function, NewType> nodeTransformer = nodeCollapser .apply(data); - IList collapsedChildren = children - .map((child) -> { - return child.collapse(leafTransform, nodeCollapser, - (subTreeVal) -> subTreeVal); - }); + IList 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 { * The distance from the pivot * @return Whether the adjusted pivot is with the list */ - private boolean adjustedPivotInBounds(IList elements, - int pivot, int pivotAdjustment) { + private boolean adjustedPivotInBounds(IList 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 @@ -13,6 +13,43 @@ import java.util.function.Function; * */ public interface Bifunctor { + /** + * Lift a pair of functions to a single function that maps over both + * parts of a pair + * + * @param + * The old left type of the pair + * @param + * The old right type of the pair + * @param + * The new left type of the pair + * @param + * 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 Function, Bifunctor> bimap( + Function leftFunc, + Function rightFunc) { + Function, Bifunctor> bimappedFunc = ( + argPair) -> { + Function, Bifunctor> leftMapper = argPair + . fmapLeft(leftFunc); + + Bifunctor leftMappedFunctor = leftMapper + .apply(argPair); + Function, Bifunctor> rightMapper = leftMappedFunctor + . fmapRight(rightFunc); + + return rightMapper.apply(leftMappedFunctor); + }; + + return bimappedFunc; + } + /** * Lift a function to operate over the left part of this pair * @@ -27,9 +64,8 @@ public interface Bifunctor { * pair * @return The function lifted to work over the left side of bifunctors */ - public - Function, Bifunctor> - fmapLeft(Function func); + public Function, Bifunctor> fmapLeft( + Function func); /** * Lift a function to operate over the right part of this pair @@ -46,50 +82,8 @@ public interface Bifunctor { * @return The function lifted to work over the right side of * bifunctors */ - public - Function, Bifunctor> - fmapRight(Function func); - - /** - * Lift a pair of functions to a single function that maps over both - * parts of a pair - * - * @param - * The old left type of the pair - * @param - * The old right type of the pair - * @param - * The new left type of the pair - * @param - * 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 - Function, Bifunctor> - bimap(Function leftFunc, - Function rightFunc) { - Function, Bifunctor> bimappedFunc = - (argPair) -> { - Function, Bifunctor> leftMapper = - argPair. fmapLeft( - leftFunc); - - Bifunctor leftMappedFunctor = - leftMapper.apply(argPair); - Function, Bifunctor> rightMapper = - leftMappedFunctor - . fmapRight( - rightFunc); - - return rightMapper.apply(leftMappedFunctor); - }; - - return bimappedFunc; - } + public Function, Bifunctor> fmapRight( + Function func); /** * Get the value contained on the left of this bifunctor -- cgit v1.2.3