Skip to content

Instantly share code, notes, and snippets.

@berdario
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berdario/273edcdc1c09bea55be4 to your computer and use it in GitHub Desktop.
Save berdario/273edcdc1c09bea55be4 to your computer and use it in GitHub Desktop.
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