From 9d9677e0a34d412fed209996fde4ad0ae6acd463 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 10 May 2016 17:37:31 -0400 Subject: Added ability to bimap over pairs and pair like things --- .../src/main/java/bjc/utils/funcdata/IList.java | 53 ++++------------------ .../java/bjc/utils/funcdata/theory/Bifunctor.java | 41 +++++++++++++++++ 2 files changed, 49 insertions(+), 45 deletions(-) (limited to 'BJC-Utils2/src/main/java') 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 c45551e..b6363e7 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java @@ -10,12 +10,7 @@ import java.util.function.Predicate; import bjc.utils.data.IPair; /** - * A wrapper over another list that provides eager functional operations - * over it. Differs from a stream in every way except for the fact that - * they both provide functional operations. - * - * NOTE: The indications of complexity for methods assume that the backing - * list has performance on the order of an array + * A wrapper over another list that provides functional operations over it. * * @author ben * @@ -27,8 +22,6 @@ public interface IList { /** * Add an item to this list * - * Takes O(1) time. - * * @param item * The item to add to this list. * @return Whether the item was added to the list succesfully. @@ -39,10 +32,6 @@ public interface IList { * Check if all of the elements of this list match the specified * predicate. * - * Takes O(f * p(n)) time on average, where f is defined as the average - * number of elements in a list until the predicate returns false, and - * p is the average running time of the predicate - * * @param matchPredicate * The predicate to use for checking. * @return Whether all of the elements of the list match the specified @@ -53,10 +42,6 @@ public interface IList { /** * Check if any of the elements in this list match the specified list. * - * Takes O(f * p(n)) time on average, where f is defined as the average - * number of elements in a list until the predicate returns true, and p - * is the average running time of the predicate - * * @param matchPredicate * The predicate to use for checking. * @return Whether any element in the list matches the provided @@ -72,10 +57,6 @@ public interface IList { * NOTE: The returned list will have the length of the shorter of this * list and the combined one. * - * Takes O(q * c(q)), where q is defined as the length of the shorter - * of this list and the provided one, and c is the running time of the - * combiner. - * * @param * The type of the second list * @param @@ -94,8 +75,6 @@ public interface IList { /** * Check if the list contains the specified item * - * Takes O(n) time, assuming object compare in constant time. - * * @param item * The item to see if it is contained * @return Whether or not the specified item is in the list @@ -105,8 +84,6 @@ public interface IList { /** * Get the first element in the list * - * Takes O(1) time - * * @return The first element in this list. */ ContainedType first(); @@ -115,9 +92,6 @@ public interface IList { * Apply a function to each member of the list, then flatten the * results. Does not change the underlying list. * - * Takes O(n * m) time, where m is the average number of elements in - * the returned list. - * * @param * The type of the flattened list * @@ -132,9 +106,6 @@ public interface IList { /** * Apply a given action for each member of the list * - * Takes O(n * f(n)) time, where n is the length of the list, and f is - * the running time of the action. - * * @param action * The action to apply to each member of the list. */ @@ -143,9 +114,6 @@ public interface IList { /** * Apply a given function to each element in the list and its index. * - * Takes O(n * f(n)) time, where n is the length of the list, and f is - * the running time of the action. - * * @param indexedAction * The function to apply to each element in the list and its * index. @@ -155,8 +123,6 @@ public interface IList { /** * Retrieve a value in the list by its index. * - * Takes O(1) time. - * * @param index * The index to retrieve a value from. * @return The value at the specified index in the list. @@ -166,14 +132,12 @@ public interface IList { /** * Retrieve a list containing all elements matching a predicate * - * Takes O(n) time, where n is the number of elements in the list - * * @param matchPredicate * 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 @@ -200,8 +164,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 @@ -214,8 +178,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 @@ -224,8 +188,7 @@ public interface IList { * The size of elements to put into each one of the sublists * @return A list partitioned into partitions of size nPerPart */ - IList> partition( - int numberPerPartition); + IList> partition(int numberPerPartition); /** * Prepend an item to the list 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 3bea135..e8c74fc 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 @@ -50,6 +50,47 @@ public interface Bifunctor { 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; + } + /** * Get the value contained on the left of this bifunctor * -- cgit v1.2.3