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 intersperse #20

Closed
wants to merge 1 commit into from
Closed

Add intersperse #20

wants to merge 1 commit into from

Conversation

flip111
Copy link
Contributor

@flip111 flip111 commented Nov 12, 2023

No description provided.

@@ -2646,3 +2649,17 @@ iscanr f b = NonEmptyVector . V.iscanr f b . _neVec
iscanr' :: (Int -> a -> b -> b) -> b -> NonEmptyVector a -> NonEmptyVector b
iscanr' f b = NonEmptyVector . V.iscanr' f b . _neVec
{-# INLINE iscanr' #-}

-- | \(\mathcal{O}(n)\). The 'intersperse' function takes an element and a NonEmptyVector
Copy link
Owner

@emilypi emilypi Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you copypasted something like the implementation for intersperse from Data.List.NonEmpty, which isn't a very good implementation for something like vector or nonempty-vector for several reasons:

  1. This function is not O(n): cons for Vector is O(n), and vector consing for n-1-many elements is actually O(n^2). This makes the implementation rather unsatisfactory, and in fact, worse than the just converting to and from a list for very large vectors.

  2. There is an issue out to resolve the suboptimal vector cons-based implementation (posted here: Adding intersperse to the API haskell/vector#421), which discusses the potential streams-based implementation that I would welcome in this package when the time comes for that work to be done in vector. Otherwise, we'd be importing a very suboptimal implementation into this package, breaking from vector's API. Presumably such a streams-based implementation could amortize away some of the array resizing needed under the hood so we don't have to take the quadratic hit so hard.

  3. I think there's benefit in matching the vector API, since it makes this package a near drop-in replacement for vector, minus some fiddling and pattern matching to enforce the nonempty invariants in one's code. We'd be deviating from that until point 2 is implemented.

So i have to reject this on those grounds.

Copy link
Owner

@emilypi emilypi Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least, in its current incarnation. If you want to help the vector package move the work along for intersperse, I'm happy to keep this open and accept it with a deference to the vector implementation!

@flip111
Copy link
Contributor Author

flip111 commented Nov 13, 2023

Reasons sound well founded. I will close this. A new PR can be created later on the bases of a vector intersperse.

@flip111 flip111 closed this Nov 13, 2023
@emilypi
Copy link
Owner

emilypi commented Nov 13, 2023

Thanks @flip111! I hope you take on the vector work if you're up for it :)

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