wordwrap :: Int64 -> Text.Lazy.Text -> Text.Lazy.Text
wordwrap maxwidth = Text.Lazy.unlines . wordwrap' . Text.Lazy.words
  where
    wordwrap' [] = []
    wordwrap' ws = sentence : wordwrap' restwords
      where
        zipped = zip (concats ws) ws
        (sentences, rest) = span (\(s, _) -> Text.Lazy.length s <= maxwidth) zipped
        sentence = last (map fst sentences)
        restwords = map snd rest
wordwrap :: Int64 -> Text.Lazy.Text -> Text.Lazy.Text
wordwrap maxwidth = Text.Lazy.unlines . wordwrap' . Text.Lazy.words
  where
    wordwrap' [] = []
    wordwrap' ws = sentence : wordwrap' restwords
      where
        zipped = zip (concats ws) ws
        (sentences, rest) = span (\(s, _) -> Text.Lazy.length s <= maxwidth) zipped
        sentence = last (map fst sentences)
        restwords = map snd rest
wordwrap maxwidth = Text.Lazy.unlines . wordwrap' . Text.Lazy.words
  where
    wordwrap' [] = []
    wordwrap' ws = sentence : wordwrap' restwords
      where
        zipped = zip (concats ws) ws
        (sentences, rest) = span (\(s, _) -> Text.Lazy.length s <= maxwidth) zipped
        sentence = last (map fst sentences)
        restwords = map snd rest