Skip to content

Instantly share code, notes, and snippets.

@itang
Created July 15, 2011 07:56
Show Gist options
  • Save itang/1084277 to your computer and use it in GitHub Desktop.
Save itang/1084277 to your computer and use it in GitHub Desktop.
Pattern Matching Anonymous Functions
List(("a",1),("b",2)).map(case (x,y) => x + y)
List(("a",1),("b",2)).map{case (x,y) => x + y}
val func0: (Int, Int)=>Int = case (x, y)
val func1: (Int, Int)=>Int = { case (x, y) => x + y }
func1(1, 2)
func1(1->2)
val func2: ((Int, Int))=>Int = { case (x, y) => x + y}
func2(1, 2)
func2(1 -> 2)
List(1,2,3) map func2
val func3: (Int, Int, Int)=>Int = { case (x, y, z) => x + y + z}
func3(1, 2, 2)
val func4: Pair[Int, Int] => Int = { case (x, y) => x + y }
func4(1, 2)
func4(1 -> 2)
val func5: PartialFunction[(Int,Int), Int] = { case (x, y) => x + y }
func5(1, 2)
func5(1 -> 2)
func5.isDefinedAt(100,10000)
def test(list:List[(Int,Int)])(f:(Int,Int)=>Int) = list.map(it=> f(it._1,it._2))
test(List((1,2),(3,4))){
case (x,y) => x + y
}
import scala.actors._
import Actor._
val a = actor{
react{
case msg => println(msg)
}
}
a ! "hello"
def goodNames(words:List[String])(f: String => Boolean):Int = {
words.count(f)
}
val f : PartialFunction[String,Boolean] = { case "itang" => true }
goodNames(List("itang","tom")){
f orElse {
case "tom" => true
case _ => false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment