Created
January 27, 2013 17:46
-
-
Save sofoklis/4649401 to your computer and use it in GitHub Desktop.
firstclassfunctions.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simplest way to create a function | |
def f1(value: Int) = 4 * value //> f1: (value: Int)Int | |
// And call | |
f1(4) //> res0: Int = 16 | |
// and save it in a val | |
val valuef1 = f1 _ //> valuef1 : Int => Int = <function1> | |
//and call it from the val | |
valuef1(40) //> res1: Int = 160 | |
// Another way to create a function | |
val valueAnoFun = (x: Int) => 5 * x //> valueAnoFun : Int => Int = <function1> | |
// call the function | |
valueAnoFun(43) //> res2: Int = 215 | |
// passing our function to the list map function as a parameter | |
List(1,2,3,4) map valueAnoFun //> res3: List[Int] = List(5, 10, 15, 20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment