Created
May 18, 2013 16:57
-
-
Save ignasi35/5605115 to your computer and use it in GitHub Desktop.
Scala solution to the Word Wrap Kata performed during 2013-05-18 at @softonic 's #katayuno
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
object WordWrap { | |
/** | |
* Returns the <code>input</code> text split into lines of length | |
* smaller or equal to <code>maxLength</code>. The newline | |
* character is not consistent with the platform, it's always a | |
* CR character. This input processing also replaces | |
* consecutive blank spaces with a single blank space. | |
* | |
* @param input the text we want to process. | |
* @param the maximum length of a line | |
*/ | |
def wrap(input : String, maxLength : Int) = { | |
input.split (" ").foldLeft ("")( | |
(acc, in) => | |
if (in equals "") acc else if ((acc.length - acc.lastIndexOf('\n') + in.length()) < maxLength) { acc + " " + in } | |
else { acc + '\n' + in }).trim | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes it can be done. :D