module Data.Splittable
( Split(..)
, Combine(..)
)
where
import Prelude hiding (splitAt)
import qualified Prelude as P
import qualified Data.ByteString as BS
import qualified Data.Vector.Generic as VG
class Split source where
splitAt :: Int
-> source
-> (source, source)
size :: source -> Int
splitIn :: Int
-> source
-> [source]
splitIn n l | n < 1 = error "Can't split in less than one chunk!"
| n > (size l) = splitIn (size l) l
| otherwise =
let
partSize = (size l) `div` n
splitIn1 acc 1 rest = acc ++ [rest]
splitIn1 acc m rest = splitIn1 (acc ++ [v1]) (m 1) v2
where
(v1, v2) = splitAt partSize rest
in
splitIn1 [] n l
class Combine result where
combine :: [result] -> result
instance Split [s] where
splitAt = P.splitAt
size = P.length
instance Combine [s] where
combine = concat
instance (VG.Vector v e) => Split (v e) where
splitAt = VG.splitAt
size = VG.length
instance (VG.Vector v e) => Combine (v e) where
combine = VG.concat
instance Split BS.ByteString where
splitAt = BS.splitAt
size = BS.length