user1002430
user1002430

Reputation:

How to convert a Haskell Traversable into a Vector?

If I have a Traversable instance, xs, how do I convert it into a Vector?

Upvotes: 10

Views: 454

Answers (1)

David Young
David Young

Reputation: 10783

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.

Upvotes: 11

Related Questions