Created
May 25, 2018 14:10
-
-
Save benjamin-hodgson/a28d8894e155509bdfc763b1403253f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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