haskell / vector

An efficient implementation of Int-indexed arrays (both mutable and immutable), with a powerful loop optimisation framework .
Other
367 stars 139 forks source link

Adding `intersperse` to the API #421

Open tchoutri opened 3 years ago

tchoutri commented 3 years ago

I would like to suggest the addition of intersperse :: a -> Vector a -> Vector a to the API.

I have a prototype that I reuse in my projects, that reads:

intercalateVec :: Text -> Vector Text -> Vector Text                
intercalateVec sep vector =                                         
  if V.null vector                                                  
  then vector                                                       
  else V.tail $ V.concatMap (\word -> V.fromList [sep, word]) vector

But I would be interested to know if a better implementation is possible.

lehins commented 3 years ago

I pretty sure it should be possible to get an efficient version implemented of this at the stream level. In fact, here is a drop in implementation for it from text:

https://github.com/haskell/text/blob/fdc938628efe9ecf86e7d11128c4c7cc66d1071a/src/Data/Text/Internal/Fusion/Common.hs#L457-L469

We should really unify streaming functions from text with vector into a single package. Have no clue how much work that would be though.

Shimuuar commented 10 months ago

I mark this issue "help-wanted". Contributions are welcome