Created
January 7, 2013 18:09
-
-
Save anonymous/4477082 to your computer and use it in GitHub Desktop.
haskell version of code from http://blog.8thlight.com/uncle-bob/2013/01/07/FPBE3-Do-the-rules-change.html
This file contains hidden or 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
makeUpTo n = mlu n [] | |
where | |
mlu n pre [] = [pre] | |
mlu n [] (w:ws) = mlu n w ws | |
mlu n pre (w:ws) | length pre + 1 + length w <= n = mlu n (pre ++ ' ' : w) ws | |
| otherwise = pre : mlu n [] (w:ws) | |
t2 n i = putStrLn | |
$ unlines | |
$ makeUpTo n | |
$ [ frag | w <- words i, frag <- splitIntoSize n w ] | |
-- basically a library function, and useful for other kinds of list | |
splitIntoSize :: Int -> [a] -> [[a]] | |
splitIntoSize n = map (take n) . takeWhile (not . null) . iterate (drop n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment