Skip to content

Instantly share code, notes, and snippets.

@benjamin-hodgson
Created May 25, 2018 14:10
Show Gist options
  • Save benjamin-hodgson/a28d8894e155509bdfc763b1403253f4 to your computer and use it in GitHub Desktop.
Save benjamin-hodgson/a28d8894e155509bdfc763b1403253f4 to your computer and use it in GitHub Desktop.
import Data.Word (Word8)
rleBytes :: [Word8] -> [Word8]
rleBytes = concatMap (\(n, x) -> [n, x]) . break8 . rle
rle :: Eq a => [a] -> [(Int, a)]
rle = map len . runs
where
len (x, xs) = (length xs + 1, x)
runs [] = []
runs (x:xs) = (x, takeWhile (== x) xs) : runs (dropWhile (== x) xs)
break8 :: [(Int, a)] -> [(Word8, a)]
break8 = concatMap break
where
break (n, x)
| n > 255 = (255, x) : break (n-255, x)
| otherwise = [(fromIntegral n, x)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment