summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java37
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java52
2 files changed, 53 insertions, 36 deletions
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<E> implements Cloneable {
// Get the iterator for the other list
Iterator<T> rightIterator = rightList.toIterable().iterator();
- for (Iterator<E> leftIterator =
- wrappedList.iterator(); leftIterator.hasNext()
+ for (Iterator<E> 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<E> implements Cloneable {
* @return A new list containing the flattened results of applying the
* provided function.
*/
- public <T> FunctionalList<T>
- flatMap(Function<E, FunctionalList<T>> elementExpander) {
- FunctionalList<T> returnedList =
- new FunctionalList<>(this.wrappedList.size());
+ public <T> FunctionalList<T> flatMap(
+ Function<E, FunctionalList<T>> elementExpander) {
+ FunctionalList<T> returnedList = new FunctionalList<>(
+ this.wrappedList.size());
forEach(element -> {
- FunctionalList<T> expandedElement =
- elementExpander.apply(element);
+ FunctionalList<T> expandedElement = elementExpander
+ .apply(element);
// Add each element to the returned list
expandedElement.forEach(returnedList::add);
@@ -344,8 +344,8 @@ public class FunctionalList<E> implements Cloneable {
* @return A new list containing the mapped elements of this list.
*/
public <T> FunctionalList<T> map(Function<E, T> elementTransformer) {
- FunctionalList<T> returnedList =
- new FunctionalList<>(this.wrappedList.size());
+ FunctionalList<T> returnedList = new FunctionalList<>(
+ this.wrappedList.size());
forEach(element -> {
// Add the transformed item to the result
@@ -366,8 +366,8 @@ public class FunctionalList<E> implements Cloneable {
* @return A list containing pairs of this element and the specified
* list
*/
- public <T> FunctionalList<Pair<E, T>>
- pairWith(FunctionalList<T> rightList) {
+ public <T> FunctionalList<Pair<E, T>> pairWith(
+ FunctionalList<T> rightList) {
return combineWith(rightList, Pair<E, T>::new);
}
@@ -378,14 +378,13 @@ public class FunctionalList<E> 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<FunctionalList<E>>
- partition(int numberPerPartition) {
- FunctionalList<FunctionalList<E>> returnedList =
- new FunctionalList<>();
+ public FunctionalList<FunctionalList<E>> partition(
+ int numberPerPartition) {
+ FunctionalList<FunctionalList<E>> returnedList = new FunctionalList<>();
// The current partition being filled
- GenHolder<FunctionalList<E>> currentPartition =
- new GenHolder<>(new FunctionalList<>());
+ GenHolder<FunctionalList<E>> currentPartition = new GenHolder<>(
+ new FunctionalList<>());
this.forEach((element) -> {
if (isPartitionFull(numberPerPartition, currentPartition)) {
@@ -555,7 +554,7 @@ public class FunctionalList<E> 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<String> splitTokens =
- new FunctionalList<>(
- tokenToSplit.split(operatorRegex));
+ FunctionalList<String> splitTokens = new FunctionalList<>(
+ tokenToSplit.split(operatorRegex));
FunctionalList<String> result = new FunctionalList<>();
@@ -165,14 +164,13 @@ public class ListUtils {
/*
* List that holds our results
*/
- FunctionalList<FunctionalList<E>> returnedList =
- new FunctionalList<>();
+ FunctionalList<FunctionalList<E>> returnedList = new FunctionalList<>();
/*
* List that holds current partition
*/
- GenHolder<FunctionalList<E>> currentPartition =
- new GenHolder<>(new FunctionalList<>());
+ GenHolder<FunctionalList<E>> 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<String> deAffixTokens(
FunctionalList<String> input,
Deque<Pair<String, String>> ops) {
- GenHolder<FunctionalList<String>> returnedList =
- new GenHolder<>(input);
+ GenHolder<FunctionalList<String>> 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<String> 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<String> 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