Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View gist:524460
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import Data.Char (isSpace)
 
trim :: String -> String
trim = trimAndReverse . trimAndReverse
where trimAndReverse = reverse . dropWhile isSpace
 
reverseBreak :: (a -> Bool) -> [a] -> ([a], [a])
reverseBreak f xs = (reverse before, reverse after)
where (after, before) = break f $ reverse xs
 
wrapLine :: Int -> String -> [String]
wrapLine maxLen line
| length line <= maxLen = [line]
| any isSpace beforeMax = beforeSpace : (wrapLine maxLen $ afterSpace ++ afterMax)
| otherwise = beforeMax : wrapLine maxLen afterMax
where (beforeMax, afterMax) = splitAt maxLen line
(beforeSpace, afterSpace) = reverseBreak isSpace beforeMax
 
main :: IO ()
main = interact $ unlines . concatMap (map trim . wrapLine maxLen) . lines
where maxLen = 72
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.