Created
July 12, 2016 18:20
-
-
Save michaelfeathers/d16c388e01a88a0dd2df4b086a882bdc 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
-- 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