简体   繁体   English

功能通用于Foldable和Data.Vector.Unboxed

[英]Function generic over Foldable and Data.Vector.Unboxed

Data.Vector.Unboxed is not an instance of Foldable . Data.Vector.Unboxed不是Foldable的实例。 What is the best way to write a function which works on instances of Foldable as well as on unboxed vectors? 编写适用于Foldable实例和未装箱矢量的函数的最佳方法是什么? For example this version of sum works with lists and boxed vectors, but not unboxed: 例如,此版本的sum与列表和盒装向量一起使用,但不是取消装箱:

sum :: (Foldable t, Num a) => t a -> a
sum = Foldable.foldl' (+) 0

This sounds like something mono-traversable could do for you. 这听起来像是单行程可以为你做的事情。 Fe it already contains a sum variant: Fe它已经包含一个sum变量:

osum :: (MonoFoldable mono, Num (Element mono)) => mono -> Element mono
osum = ofoldl' (+) 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM