Last active
August 29, 2015 14:16
-
-
Save berdario/273edcdc1c09bea55be4 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
package object wordwrap{ | |
def wrapSingleLine(column: Int)(words: List[String]): Iterable[String] = words match { | |
case List() => List(); | |
case (hd :: tl) => { | |
tl.scanLeft(hd)({_ + " " + _}).span({_.length <= column}) match { | |
case (List(), _) => hd.grouped(column).toIterable ++ wrapSingleLine(column)(tl) | |
case (f, s) => { | |
val f2 = f.map({_.split(" ").last}) | |
f2 ++ wrapSingleLine(column)(s.map{_.drop(f2.last.length)}); | |
} | |
} | |
} | |
} | |
def wrap(t: String, column: Int) = | |
"\\n".r.split(t) | |
.map{_.split(" ").toList} | |
.toList | |
.map(wrapSingleLine(column)) | |
.map{_.mkString("\n")} | |
.mkString("\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment