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
| object Cafe { | |
| //@JavaPeople: We don't need semicolons or the return keyword | |
| def buyCoffee(creditCard: CreditCard): Coffee = { | |
| val cup = new Coffee() // new coffee is created | |
| creditCard.charge(cup.price) // side effecting call to outside | |
| cup // cup is returned | |
| } | |
| } |
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
| val r1 = (new java.lang.StringBuilder("Hello")).append(" World").toString | |
| // 'Hello World' | |
| val r2 = (new java.lang.StringBuilder("Hello")).append(" World").toString | |
| // 'Hello World' |
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
| val x = new java.lang.StringBuilder("Hello") | |
| val r1 = x.append(" World").toString | |
| // 'Hello World' | |
| val r2 = x.append(" World").toString | |
| // 'Hello World World' |
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
| var x = 42 | |
| x = x + 1 |
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
| val x = new java.lang.StringBuilder("Hello") | |
| val r1 = x.append(" World").toString | |
| val r2 = x.append(" World").toString |
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
| val x = "Hello" | |
| val r1 = x + " World" | |
| val r2 = x + " World" |
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
| def countSpaces(word: List[Char]): Int = | |
| word.count(_ == ' ') |
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
| def countSpaces(word: List[Char]): Int = | |
| word.foldLeft(0)((count: Int, char: Char) => | |
| if (char == ' ') { | |
| count + 1 | |
| } else { | |
| count | |
| } | |
| ) |
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
| trait Foldable[F[_]] { | |
| def foldLeft[A, B](fa: F[A], b: B)(f: (B, A) => B): B | |
| } |
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
| def addThree(list: List[Int]): List[Int] = { | |
| val f = (item: Int) => item + 3 | |
| list.map(f) | |
| } |