1 {-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
2 module StringBuffer where
8 instance Buffer String where
11 line n b = safeIndex n (lines b)
12 replaceLine n l b = unlines . uncurry replaceLine' . splitAt n . lines $ b
13 where replaceLine' pre [] = pre
14 replaceLine' pre (_:ls) = pre ++ l:ls
15 numLines = length . lines
16 value = length . words
18 safeIndex :: Int -> [a] -> Maybe a
19 safeIndex n _ | n < 0 = Nothing
20 safeIndex _ [] = Nothing
21 safeIndex 0 (x:_) = Just x
22 safeIndex n (_:xs) = safeIndex (n-1) xs