From d06aa6bd1ba6240558a12c63beccafea3bd95ffe Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Fri, 1 Apr 2016 10:46:07 -0400 Subject: Minor fixes to things --- .../java/bjc/utils/funcdata/FunctionalList.java | 37 ++++++++------- .../main/java/bjc/utils/funcutils/ListUtils.java | 52 +++++++++++++++------- 2 files changed, 53 insertions(+), 36 deletions(-) (limited to 'BJC-Utils2/src/main/java') 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 9652469..a260d78 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -181,8 +181,8 @@ public class FunctionalList implements Cloneable { // Get the iterator for the other list Iterator rightIterator = rightList.toIterable().iterator(); - for (Iterator leftIterator = - wrappedList.iterator(); leftIterator.hasNext() + for (Iterator leftIterator = wrappedList + .iterator(); leftIterator.hasNext() && rightIterator.hasNext();) { // Add the transformed items to the result list E leftVal = leftIterator.next(); @@ -227,14 +227,14 @@ public class FunctionalList implements Cloneable { * @return A new list containing the flattened results of applying the * provided function. */ - public FunctionalList - flatMap(Function> elementExpander) { - FunctionalList returnedList = - new FunctionalList<>(this.wrappedList.size()); + public FunctionalList flatMap( + Function> elementExpander) { + FunctionalList returnedList = new FunctionalList<>( + this.wrappedList.size()); forEach(element -> { - FunctionalList expandedElement = - elementExpander.apply(element); + FunctionalList expandedElement = elementExpander + .apply(element); // Add each element to the returned list expandedElement.forEach(returnedList::add); @@ -344,8 +344,8 @@ public class FunctionalList implements Cloneable { * @return A new list containing the mapped elements of this list. */ public FunctionalList map(Function elementTransformer) { - FunctionalList returnedList = - new FunctionalList<>(this.wrappedList.size()); + FunctionalList returnedList = new FunctionalList<>( + this.wrappedList.size()); forEach(element -> { // Add the transformed item to the result @@ -366,8 +366,8 @@ public class FunctionalList implements Cloneable { * @return A list containing pairs of this element and the specified * list */ - public FunctionalList> - pairWith(FunctionalList rightList) { + public FunctionalList> pairWith( + FunctionalList rightList) { return combineWith(rightList, Pair::new); } @@ -378,14 +378,13 @@ public class FunctionalList implements Cloneable { * The size of elements to put into each one of the sublists * @return A list partitioned into partitions of size nPerPart */ - public FunctionalList> - partition(int numberPerPartition) { - FunctionalList> returnedList = - new FunctionalList<>(); + public FunctionalList> partition( + int numberPerPartition) { + FunctionalList> returnedList = new FunctionalList<>(); // The current partition being filled - GenHolder> currentPartition = - new GenHolder<>(new FunctionalList<>()); + GenHolder> currentPartition = new GenHolder<>( + new FunctionalList<>()); this.forEach((element) -> { if (isPartitionFull(numberPerPartition, currentPartition)) { @@ -555,7 +554,7 @@ public class FunctionalList implements Cloneable { // Remove trailing space and comma sb.deleteCharAt(sb.length() - 1); - sb.deleteCharAt(sb.length() - 2); + //sb.deleteCharAt(sb.length() - 2); sb.append(")"); diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java index 3963e69..810bc29 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -60,9 +60,8 @@ public class ListUtils { operatorRegex)) { return new FunctionalList<>(tokenToSplit); } else { - FunctionalList splitTokens = - new FunctionalList<>( - tokenToSplit.split(operatorRegex)); + FunctionalList splitTokens = new FunctionalList<>( + tokenToSplit.split(operatorRegex)); FunctionalList result = new FunctionalList<>(); @@ -165,14 +164,13 @@ public class ListUtils { /* * List that holds our results */ - FunctionalList> returnedList = - new FunctionalList<>(); + FunctionalList> returnedList = new FunctionalList<>(); /* * List that holds current partition */ - GenHolder> currentPartition = - new GenHolder<>(new FunctionalList<>()); + GenHolder> currentPartition = new GenHolder<>( + new FunctionalList<>()); /* * List that holds elements rejected during current pass */ @@ -186,10 +184,8 @@ public class ListUtils { /* * Run up to a certain number of passes */ - for (int numberOfIterations = - 0; numberOfIterations < MAX_NTRIESPART - && !rejectedElements - .isEmpty(); numberOfIterations++) { + for (int numberOfIterations = 0; numberOfIterations < MAX_NTRIESPART + && !rejectedElements.isEmpty(); numberOfIterations++) { input.forEach(new GroupPartIteration<>(returnedList, currentPartition, rejectedElements, numberInCurrentPartition, numberPerPartition, @@ -253,8 +249,8 @@ public class ListUtils { public static FunctionalList deAffixTokens( FunctionalList input, Deque> ops) { - GenHolder> returnedList = - new GenHolder<>(input); + GenHolder> returnedList = new GenHolder<>( + input); ops.forEach((op) -> returnedList .transform((oldRet) -> oldRet.flatMap((tok) -> { @@ -273,8 +269,30 @@ public class ListUtils { * @return The collapsed string of tokens */ public static String collapseTokens(FunctionalList input) { - return input.reduceAux("", - (currentString, state) -> state + currentString, - (strang) -> strang); + return collapseTokens(input, ""); } -} + + /** + * Collapse a string of tokens into a single string, adding the desired + * seperator after each token + * + * @param input + * The list of tokens to collapse + * @param sep + * The seperator to use for seperating tokens + * @return The collapsed string of tokens + */ + public static String collapseTokens(FunctionalList input, + String sep) { + if (input.getSize() <= 1) { + return input.first(); + } else { + return input.reduceAux("", + (currentString, state) -> state + currentString + sep, + (strang) -> { + return strang.substring(0, + strang.length() - sep.length()); + }); + } + } +} \ No newline at end of file -- cgit v1.2.3