If I have a Traversable
instance, xs
, how do I convert it into a Vector
?
Asked
Active
Viewed 428 times
10
1 Answers
11
All Traversable
instances are also Foldable
, so you can write something like
toVector :: Foldable t => t a -> Vector a
toVector = Vector.fromList . Foldable.toList
{-# INLINE toVector #-}
This might make an intermediate list though, if that doesn't get fused away. The inlining should help make fusion more likely.

David Young
- 10,713
- 2
- 33
- 47
-
I just had a shot and it seems like the larger obstacle is that the `Vector` consumers, fundamentally, can only fuse with data types that can be efficiently turned into the library's `Stream` type. So I don't think you can fuse `Vector` consumers with an arbitrary `Foldable`s for reasons similar to why you can't implement `uncons :: [a] -> Maybe (a, [a])` efficiently with just `foldr`. (Try writing `toVector` in terms of `Foldable.foldr` and `Stream.unfoldr`, without effectively reimplementing `Foldable.toList`—I don't think it can be done...) So your solution may well be optimal. – Luis Casillas Apr 26 '15 at 01:25