Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created June 30, 2011 05:21
Show Gist options
  • Save xuwei-k/1055681 to your computer and use it in GitHub Desktop.
Save xuwei-k/1055681 to your computer and use it in GitHub Desktop.
Ruby Enumerable.grep を Scala で
implicit def toGrepCallable(list:List[String]) = new {
def grep[A](regex:String)(func:String => A = identity _):List[A] =
list.collect{
case s if regex.r.findFirstIn(s).isDefined =>
func(s)
}
}
scala> List("aaa","123").grep("\\d"){_.toInt * 2}
res0: List[Int] = List(246)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment