Skip to content

Instantly share code, notes, and snippets.

@michaelfeathers
Created July 12, 2016 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelfeathers/d16c388e01a88a0dd2df4b086a882bdc to your computer and use it in GitHub Desktop.
Save michaelfeathers/d16c388e01a88a0dd2df4b086a882bdc to your computer and use it in GitHub Desktop.
-- code from https://www.infoq.com/presentations/Type-Functional-Design with bug related to
-- assumption of trailing spaces after words
import Data.List;
lineBreak :: String -> String
lineBreak = joinedLines . wordJoinedLines . brokenLines . words
brokenLines :: [String] -> [[String]]
brokenLines [] = []
brokenLines wordList = brokenLine : brokenLines remainingWords
where (brokenLine, remainingWords) = splitAt (brokenLineWordCount wordList) wordList
brokenLineWordCount :: [String] -> Int
brokenLineWordCount = length . takeWhile (< 13) . scanl1 (+) . map wordLength
wordLength :: String -> Int
wordLength = succ . length
wordJoinedLines :: [[String]] -> [String]
wordJoinedLines = map (intercalate " ")
joinedLines :: [String] -> String
joinedLines = intercalate "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment