Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function generic over Foldable and Data.Vector.Unboxed

Tags:

haskell

vector

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
like image 316
Grzegorz Chrupała Avatar asked Mar 26 '14 09:03

Grzegorz Chrupała


1 Answers

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
like image 145
Sjoerd Visscher Avatar answered Sep 21 '22 13:09

Sjoerd Visscher