background-image: url(../img/fp-tower/website-background.svg) class: center, middle, white .title[Val vs Def functions] --- class: medium-code .forty-seven-left[ # Val function (Lambda)
```scala val replicate: (Int, String) => String = (n: Int, text: String) => ... ```
```scala replicate(3, "Hello ") // res1: String = "Hello Hello Hello " ``` ] .forty-seven-right[ # Def function (Method)
```scala def replicate(n: Int, text: String): String = ... ```
```scala replicate(3, "Hello ") // res3: String = "Hello Hello Hello " ``` ] --- # Val function (Lambda or anonymous function)
```scala (n: Int, text: String) => List.fill(n)(text).mkString ``` --- # Val functions are ordinary objects
```scala (n: Int, text: String) => List.fill(n)(text).mkString ```
```scala 3 "Hello World!" User("John Doe", 27) ``` --- # Val functions are ordinary objects
```scala val replicate = (n: Int, text: String) => List.fill(n)(text).mkString ```
```scala val counter = 3 val message = "Hello World!" val john = User("John Doe", 27) ``` --
```scala val repeat = replicate ``` --- # Val functions are ordinary objects
```scala val numbers = List(1,2,3) // numbers: List[Int] = List(1, 2, 3) val functions = List((x: Int) => x + 1, (x: Int) => x - 1, (x: Int) => x * 2) // functions: List[Int => Int] = List(
,
,
) ``` --
```scala functions(0)(10) // res12: Int = 11 functions(2)(10) // res13: Int = 20 ``` --- class: medium-code # Val function desugared
```scala val replicate: (Int, String) => String = (n: Int, text: String) => List.fill(n)(text).mkString ``` --- class: medium-code # Val function desugared
```scala val replicate: (Int, String) => String = (n: Int, text: String) => List.fill(n)(text).mkString ```
```scala val replicate: Function2[Int, String, String] = (n: Int, text: String) => List.fill(n)(text).mkString ``` -- .forty-seven-left[ ```scala val bigFunction: (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int ) => String = _ => ??? // error: function values may not have more // than 22 parameters, but 23 given ``` ] --- class: medium-code # Val function desugared
```scala val replicate: (Int, String) => String = (n: Int, text: String) => List.fill(n)(text).mkString ```
```scala val replicate: Function2[Int, String, String] = new Function2[Int, String, String] { def apply(n: Int, text: String): String = List.fill(n)(text).mkString } ``` --- class: medium-code # SAM (Single Abstract Method)
```scala trait Printer { def print(message: String): Unit } ``` .forty-seven-left[ ## SAM syntax ```scala val console: Printer = (message: String) => println(message) ``` ] .forty-seven-right[ ## Standard syntax ```scala val console: Printer = new Printer { def print(message: String): Unit = println(message) } ``` ] --- class: medium-code # SAM (Single Abstract Method) .forty-seven-left[
```scala trait Printer { def print(message: String): Unit } ``` ] .forty-seven-right[ ## Standard syntax ```scala val console: Printer = new Printer { def print(message: String): Unit = println(message) } ``` ## SAM syntax ```scala val console: Printer = (message: String) => println(message) ``` ] --- class: medium-code # Val function desugared .forty-seven-left[ ## SAM syntax ```scala val replicate: (Int, String) => String = (n: Int, text: String) => List.fill(n)(text).mkString ``` ] .forty-seven-right[ ## Standard syntax ```scala val replicate: (Int, String) => String = new Function2[Int, String, String] { def apply(n: Int, text: String): String = List.fill(n)(text).mkString } ``` ] --
.forty-seven-left[ ```scala replicate(3, "Hello ") // res24: String = "Hello Hello Hello " replicate.apply(3, "Hello ") // res25: String = "Hello Hello Hello " ``` ] --- class: medium-code # Apply syntax
```scala trait Printer { def apply(message: String): Unit } val console: Printer = new Printer { def apply(message: String) = println(message) } ```
```scala console("Hello World!") // Hello World! ``` --- # Def function (Method)
```scala import java.time.LocalDate def createDate(year: Int, month: Int, dayOfMonth: Int): LocalDate = ... ```
```scala createDate(2020, 1, 5) // res28: LocalDate = 2020-01-05 ``` --- # Arguments
```scala import java.time.LocalDate def createDate(year: Int, month: Int, dayOfMonth: Int): LocalDate = ... ```
```scala val createDateVal: (Int, Int, Int) => LocalDate = (year, month, dayOfMonth) => ... ``` --- class: center
## IDE
## Scaladoc
--- # Named arguments
```scala import java.time.LocalDate def createDate(year: Int, month: Int, dayOfMonth: Int): LocalDate = ... ```
```scala createDate(2020, 1, 5) // res30: LocalDate = 2020-01-05 ``` ```scala createDate(dayOfMonth = 5, month = 1, year = 2020) // res31: LocalDate = 2020-01-05 ``` --- # Def functions are not data
```scala def createDate(year: Int, month: Int, dayOfMonth: Int): LocalDate = ... ```
```scala List(createDate) // error: missing argument list for method createDate in class MdocApp16 // Unapplied methods are only converted to functions when a function type is expected. // You can make this conversion explicit by writing `createDate _` or `createDate(_,_,_)` instead of `createDate`. // List(createDate) // ^^^^^^^^^^ ``` --- # Def functions are not data
```scala def createDate(year: Int, month: Int, dayOfMonth: Int): LocalDate = ... ```
```scala List(createDate _) // res33: List[(Int, Int, Int) => LocalDate] = List(
) ``` --
```scala val createDateVal = createDate _ // createDateVal: (Int, Int, Int) => LocalDate =
``` --- # Def functions are not data
```scala def createDate(year: Int, month: Int, dayOfMonth: Int): LocalDate = ... ```
```scala List(createDate): List[(Int, Int, Int) => LocalDate] ```
```scala val createDateVal: (Int, Int, Int) => LocalDate = createDate ``` --- class: white background-image: url(../img/fp-tower/website-background.svg)
# .white[Summary]
.large[ * Val functions are ordinary
objects * Use def functions for API * Easy to convert def to val ]