Data.Vector.Unboxed is not an instance of Foldable. What is the best way to write a function which works on instances of Foldable as well as on unboxed vectors? For example this version of sum works with lists and boxed vectors, but not unboxed:
sum :: (Foldable t, Num a) => t a -> a
sum = Foldable.foldl' (+) 0
This sounds like something mono-traversable could do for you. F.e. it already contains a sum variant:
osum :: (MonoFoldable mono, Num (Element mono)) => mono -> Element mono
osum = ofoldl' (+) 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With