Random snippet for tag parsing - scala-kurz.org
bject NonEmpty {
def unapply(s:String): Option[String] = {
if (s == null) return None
val t = s.trim
if (0 == t.length) return None
return Some(s)
}
}
object Parsely {
def buraqParseNumber(in: String): (Long, Int) = in match {
case NonEmpty(trimmed) =>
val (sign, startpos) = trimmed.charAt(0) match {
case '-' => (-1L, 1)
case '+' => (+1L, 1)
case _ => (+1L, 0)
}
def finished(pos: Int) = { pos == tin.length || !tin.charAt(pos).isDigit }
def process(acc: Long, pos: Int): (Long, Int) =
if (finished(pos))
(acc * sign, pos)
else {
val nacc = (acc * 10L) + (trimmed.charAt(pos).toLong - '0'.toLong)
process(nacc, pos + 1)
}
return process(0L, startpos)
case _ =>
return (0L,0)
}
}
spinning <a href="http://erikengbrecht.blogspot.com/2007/05/number-parsing-in-functional-style.html">Erik'code</a>, who spun <a href="http://blog.lostlake.org/index.php?/archives/60-Thinking-functional-Scala-imperative-vs.-functional-code.html">David's code</a>. use nested function, but also uses pattern matching.