Skip to content

Instantly share code, notes, and snippets.

@hossshy
Created January 15, 2016 07:15
Show Gist options
  • Save hossshy/0fbcc089d440d02d73c8 to your computer and use it in GitHub Desktop.
Save hossshy/0fbcc089d440d02d73c8 to your computer and use it in GitHub Desktop.
package chapter4
/**
* Created by hoshi on 1/15/16.
*/
object MainObj {
val areaOfRect: (Int, Int) => Int = (width: Int, height: Int) => {
width * height
}
// Same
val areaOfRect2: Function2[Int, Int, Int] = (width: Int, height: Int) => {
width * height
}
// Same
val areaOfRect3: (Int, Int) => Int = new Function2[Int, Int, Int] {
def apply(width: Int, height: Int): Int = {
width * height
}
}
def operation(functionparam : (Int, Int) => Int) {
println(functionparam(4,4))
}
def main(args: Array[String]) {
println(areaOfRect(5, 3))
println(areaOfRect2(5, 3))
println(areaOfRect3(5, 3))
var doubler = (i: Int) => { i * 2 }
println(doubler(5))
val add = (x:Int, y:Int) => { x + y }
val sub = (x:Int, y:Int) => { x - y }
operation(add)
operation(sub)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment