如果我有 Traversable
例如, xs
,我该如何将其转换为 Vector
?
如果我有 Traversable
例如, xs
,我该如何将其转换为 Vector
?
所有 Traversable
实例也是 Foldable
,所以你可以写类似的东西
toVector :: Foldable t => t a -> Vector a
toVector = Vector.fromList . Foldable.toList
{-# INLINE toVector #-}
这可能会成为一个中间列表,如果这不会被融合掉。内联应该有助于使融合更有可能。
所有 Traversable
实例也是 Foldable
,所以你可以写类似的东西
toVector :: Foldable t => t a -> Vector a
toVector = Vector.fromList . Foldable.toList
{-# INLINE toVector #-}
这可能会成为一个中间列表,如果这不会被融合掉。内联应该有助于使融合更有可能。