Add support for minimum length vectors #30
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rationale
The current
Vect
API provided inData.FastVect.FastVect
can be useful in many scenarios where arrays are created statically. However, in cases when dealing with dynamically sized vectors, still partial static length information can be preserved throughout multiple transformations. This PR aims to leverage the existingVect
implementation to have some static guarantees for dynamically sized arrays and possibly to enable one unifed API to many operations performed on either anArray
or aNonEmptyArray
by offering a more lax guarantee on the type level index.One particular application of this could be that appending a dynamically sized
Array
to aVect
would preserve the minimum length information of the resultingMinLenVect
being able to safely index or extract as many elements from the resultingMinLenVect
up to theVect
length. Also inspired by the Haskell minlen package.New
MinLenVect
APIIncluded
Data.FastVect.MinLenVect
module for minimum length indexed vectors.MinLenVect
newtype replicatingVect
. The API is the same as forVect
with the exception of:adjust
andadjustM
functions as it does not make much sense to have those for minimum length indexed vectors IMOfromArray
successfully generates aMinLenVect
in case the array length is greater or equal than the supplied type level minimum length index.fromNonEmptyArray
fromUnsizedArray
andfromUnsizedNonEmptyArray
, that only take the array as an argument and return aMinLenVect 0 elem
and aMinLenVect 1 elem
in each case.toVect
that attempts to convert to a standard length indexedVect
if the array length is equal to the type level minimum length index andfromVect
that just converts aVect
to aMinLenVect
Implementation
The implementation is the same as in
Data.FastVect.FastVect
except for the few API changes in the new module, which could be a concern for maintability because of all the code duplication. I was not aware how to keep both APIs completely separate without exposing implementation details in neither of them and at the same time avoid all the duplication. I guess one option could be to extract the common API from both modules into a common one, but that would be more intrusive in terms of modifying the existingVect
API inData.FastVect.FastVect