Skip to content

Instantly share code, notes, and snippets.

@jbrechtel
Created January 22, 2011 16:35
Show Gist options
  • Save jbrechtel/791226 to your computer and use it in GitHub Desktop.
Save jbrechtel/791226 to your computer and use it in GitHub Desktop.
lambdas (anonymous functions) in scala
val nums = List(10,21,23,35,40)
//signature of filter: def filter (p: (A) ⇒ Boolean) : Seq[A]
nums.filter(_ > 25) //List(35, 40)
nums.filter((x: Int) => (x % 5) == 0) //List(10,35,40)
def even(x: Int) = (x % 2) == 0
nums.filter(even) //List(10,40)
nums.map(_ * 10) //List(100,210,230,350,400)
nums.mkString("-") // 10-21-23-35-40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment