Skip to content

Instantly share code, notes, and snippets.

@RoryDungan

RoryDungan/split.hs

Created Aug 8, 2018
Embed
What would you like to do?
Split the given string up based on a max length, breaking at spaces
splitBy :: (a -> Bool) -> [a] -> [[a]]
splitBy p = map reverse . reverse . splitByInner [[]] p
splitByInner :: [[a]] -> (a -> Bool) -> [a] -> [[a]]
splitByInner acc _ [] = acc
splitByInner (current:rest) predicate (x:xs) =
if predicate x then
splitByInner ([]:current:rest) predicate xs
else
splitByInner ((x:current):rest) predicate xs
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
divideIntoGroups :: Foldable t => Int -> [t a] -> [[t a]]
divideIntoGroups n = map reverse . reverse . divideIntoGroupsInner [[]] n
divideIntoGroupsInner :: Foldable t => [[t a]] -> Int -> [t a] -> [[t a]]
divideIntoGroupsInner acc _ [] = acc
divideIntoGroupsInner (current:rest) n (x:xs) =
if newLength > n then
divideIntoGroupsInner ([x]:current:rest) n xs
else
divideIntoGroupsInner ((x:current):rest) n xs
where newLength = sum (map length current) + length x + length current
joinWith :: a -> [[a]] -> [a]
joinWith _ [] = []
joinWith _ (x:[]) = x
joinWith c (x:xs) = x ++ c:(joinWith c xs)
splitByWords :: Int -> String -> [String]
splitByWords maxChars =
map (joinWith separator)
. divideIntoGroups maxChars
. splitBy (==separator)
where separator = ' '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment