As a Statistics background, I know I keep coming back to R because I can do this

a = c(1,2,3,4,5,6,7,8,9,10)
sum(a[a%%2==0])

Until I was once amazed by the simplicity of Lambda function in Scala and the List comprehension in Python:

in Scala

val a = List(1,2,3,4,5,6,7,8,9,10)
a.filter(_ % 2 == 0).sum

in Python

a = [1,2,3,4,5,6,7,8,9,10]
sum(filter(lambda x: x % 2, a))
sum(x for x in a if x % 2)

And, today I found another thing in Scala that is much cooler than in R. With the help of “some” and “option” in Scala, it can automatically ignore the strings. So you can do

val a = List("1","2","3","4","5","6","7","8","9","10","hello")
def f(x: String): Option[Int] = {
try {
    if (Integer.parseInt(x.trim) % 2 == 0) Some(Integer.parseInt(x.trim)) else None
  } catch {
    case e: NumberFormatException => None
  }
}
a.flatMap(f(_)).sum

compare to R

a = c("1","2","3","4","5","6","7","8","9","10","hello")
v = as.numeric(a[!is.na(as.numeric(a))])
sum(v[v%%2==0])

Published

17 April 2014

Tags


blog comments powered by Disqus