Inspired by Scala being in the Scripting Language Bowl, I wanted to take a look at Scala as a scripting language.
I’ve got this little Groovy code snippet:
(System.in.text =~ /\b((\p{Lu}\p{Ll}+)+)\b/).each { println "I found: ${it[0]}" }
When run, it looks like this:
$ echo " Foo bAr Baz robert Fischer alicia Weller " | groovy ./CapitalInput.groovy I found: Foo I found: Baz I found: Fischer I found: Weller
What’s a nice Scala equivalent? Here’s the best I could come up with:
import scala.io.Source val text = Source.fromInputStream(System.in).mkString("") """\b((\p{Lu}\p{Ll}+)+)\b""".r.findAllIn(text).foreach { found:String => println("I found: " + found) }
That’s actually not too bad, but does anyone have anything better? Any obvious mistakes I’m making?
Related posts: