Skip to content

Instantly share code, notes, and snippets.

View bijancn's full-sized avatar
🚢
Shipping

Bijan Chokoufe Nejad bijancn

🚢
Shipping
View GitHub Profile
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
}
}
val r1 = (new java.lang.StringBuilder("Hello")).append(" World").toString
// 'Hello World'
val r2 = (new java.lang.StringBuilder("Hello")).append(" World").toString
// 'Hello World'
val x = new java.lang.StringBuilder("Hello")
val r1 = x.append(" World").toString
// 'Hello World'
val r2 = x.append(" World").toString
// 'Hello World World'
var x = 42
x = x + 1
val x = new java.lang.StringBuilder("Hello")
val r1 = x.append(" World").toString
val r2 = x.append(" World").toString
val x = "Hello"
val r1 = x + " World"
val r2 = x + " World"
def countSpaces(word: List[Char]): Int =
word.count(_ == ' ')
def countSpaces(word: List[Char]): Int =
word.foldLeft(0)((count: Int, char: Char) =>
if (char == ' ') {
count + 1
} else {
count
}
)
trait Foldable[F[_]] {
def foldLeft[A, B](fa: F[A], b: B)(f: (B, A) => B): B
}
def addThree(list: List[Int]): List[Int] = {
val f = (item: Int) => item + 3
list.map(f)
}