Skip to content

Instantly share code, notes, and snippets.

@berdario

berdario/wordwrap.scala

Last active Aug 29, 2015
Embed
What would you like to do?
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