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 capitals = Map("France" -> "Paris", "Japan" -> "Tokyo") | |
| capitals get "France" | |
| capitals get "North Pole" | |
| def show(x: Option[String]) = x match { | |
| case Some(x) => x | |
| case None => "?" | |
| } | |
| show(capitals get "Japan") |
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 capitals = Map("France" -> "Paris", "Japan" -> "Tokyo") | |
| capitals get "France" | |
| capitals get "North Pole" | |
| def show(x: Option[String]) = x match { | |
| case Some(x) => x | |
| case None => "?" | |
| } | |
| show(capitals get "Japan") |
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
| abstract class Expr | |
| case class Var(name: String) extends Expr | |
| case class Number(num: Double) extends Expr | |
| case class UnOp(operator: String, arg: Expr) extends Expr | |
| case class BinOp(operator: String, left: Expr, right: Expr) extends Expr | |
| val v = Var("x") | |
| val op = BinOp("+", Number(1), v) | |
| v.name |
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
| import scala.collection.mutable.ArrayBuffer | |
| //Abstract class IntQueue | |
| abstract class IntQueue { | |
| def get(): Int | |
| def put(x: Int) | |
| } | |
| // A BasicIntQueue implemented with an ArrayBuffer | |
| class BasicIntQueue extends IntQueue { |
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 curriedSum(x: Int)(y: int) = x + y | |
| curriedSum(1)(2) // 3 | |
| def first(x: Int) = (y: Int) => x + y | |
| val second = first(1) | |
| second(2) // 3 | |
| // use placeholder notation to use curriedSum in a partially applied function |
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 sum(a: Int, b: Int, c: Int): Int = a + b + c | |
| val a = sum _ | |
| a(1,2,3) | |
| val b = sum(1, _: Int, 3) | |
| b(2) | |
| b(5) | |
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
| /** | |
| * place an asterisk after the type of the parameter | |
| */ | |
| def echo(args: String*) = | |
| for (arg <- args) printn(arg) | |
| echo("hello", "world") | |
| val arr = Array("What's", "up", "doc?") | |
| echo(arr: _*) |
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
| class Rational(n: Int, d: Int) { | |
| require(d != 0) | |
| private val g = gcd(n.abs, d.abs) | |
| val number = n / g | |
| val denom = d / g | |
| def this(n: Int) = this(n, 1) // auxiliary constructor | |
| def + (that: Rational): Rational = |
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 factorial(n: Int): Int = { | |
| if (n==0) 1 else n * factorial(n-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
| def factorial(n: Int) = { | |
| def factorial1(n: Int, multi: Int): Int = { | |
| if (n == 0) multi | |
| else factorial1(n-1, multi*n) | |
| } | |
| factorial1(n, 1) | |
| } | |