Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for minimum length vectors #30

Merged
merged 1 commit into from
Nov 22, 2023

Conversation

albertprz
Copy link
Contributor

Rationale

The current Vect API provided in Data.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 existing Vect implementation to have some static guarantees for dynamically sized arrays and possibly to enable one unifed API to many operations performed on either an Array or a NonEmptyArray 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 a Vect would preserve the minimum length information of the resulting MinLenVect being able to safely index or extract as many elements from the resulting MinLenVect up to the Vect length. Also inspired by the Haskell minlen package.

New MinLenVect API

Included Data.FastVect.MinLenVect module for minimum length indexed vectors.MinLenVect newtype replicating Vect. The API is the same as for Vect with the exception of:

  • Exclusion of the adjust and adjustM functions as it does not make much sense to have those for minimum length indexed vectors IMO
  • fromArray successfully generates a MinLenVect in case the array length is greater or equal than the supplied type level minimum length index.
  • Inclusion of fromNonEmptyArray
  • Inclusion of fromUnsizedArray and fromUnsizedNonEmptyArray , that only take the array as an argument and return a MinLenVect 0 elem and a MinLenVect 1 elem in each case.
  • Inclusion of toVect that attempts to convert to a standard length indexed Vect if the array length is equal to the type level minimum length index and fromVect that just converts a Vect to a MinLenVect

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 existing Vect API in Data.FastVect.FastVect

@sigma-andex sigma-andex merged commit 2147045 into sigma-andex:main Nov 22, 2023
@sigma-andex
Copy link
Owner

Beautiful! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants