Created
March 9, 2016 17:49
-
-
Save hussachai/f8dcec002f58c384b369 to your computer and use it in GitHub Desktop.
This file contains 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.util.Try | |
object FirstWeek extends App { | |
lazy val lazy1 = 1 //evaluate once when needed | |
def lazy2 = 2 //evaluate when called | |
val eager = 3 //evaluate immediately | |
object Dog { | |
def name = { | |
println("Bulldog") | |
"BullDog" | |
} | |
} | |
//call by value: evaluate the function arguments before calling the function | |
def callByValue(x: Double) = x * x | |
def callByValue(s: String, run: Boolean) = if(run) println(s) | |
callByValue(s"Hello ${Dog.name}", false) | |
//call by name: evaluate the function first, and then evaluates the arguments | |
def callByName(x: => Double) = x * x | |
def callByName(s: => String, run: Boolean) = if(run) println(s) | |
callByName(s"Hello ${Dog.name}", false) | |
//Higher order functions | |
//These are functions that take a function as a parameter or return function | |
def sum(f: (Int, Int) => Int = (a, b) => a + b, x: Int, y: Int) = f(x, y) | |
println(sum(x = 1, y = 2)) //3 | |
def sum() = (a: Int, b: Int) => a + b | |
println(sum()(3, 4)) | |
//Currying | |
//Break down a function that takes multiple arguments into a series of functions that take part of the argments. | |
def add(a: Int, b: Int) = a + b | |
def addCurrying1(a: Int)(b: Int): Int = a + b | |
def addCurrying2(a: Int): Int => Int = (b: Int) => a + b | |
println(addCurrying1(5)(6)) | |
val paf: Int => Int = addCurrying1(5) _ //partially applied function | |
println(addCurrying2(5)(6)) | |
//Class | |
class MyClass(x: Int, y: Int) { //defines a new type MyClass with default constructor | |
require(y > 0, "y must be positive") //precondition, triggering an IllegalArgumentException if not met | |
def this (x: Int) = { //auxiliary constructor | |
this(x, 0) | |
} | |
val sum = x + y //compute only once | |
def circle = x*x + y*y //public method/function | |
protected def mult = x * y | |
override def toString = s"($x, $y)" | |
} | |
trait Unidentified { | |
def exists = true | |
} | |
trait Alive { | |
def isAlive = true | |
} | |
trait Animal extends Alive { | |
def say() | |
} | |
class Cow extends Animal { | |
def say() = println("Mooooo") | |
} | |
trait Alien extends Alive with Unidentified | |
class ET extends Alien | |
//Type Parameters | |
def myF1[T <: Animal](arg: T) = arg.say() //T must derive from Animal | |
myF1(new Cow()) | |
// myF1(new ET()) | |
def myF2[T >: Animal](arg: T) = println(arg) //T must be a super type of Animal | |
def myF3[T >: Animal <: Alive](arg: T) = println(arg.isAlive) | |
myF3(new ET()) | |
myF3(new Cow()) | |
// myF3(new MyClass()) | |
//homework1: sum 1 to n using recursion | |
//homework2: list files recursively | |
//Next week: More about Type, Monad, Monoid, Handling Exception, Collection | |
val list1 = List("1", "2", "A") | |
val list2 = list1.map(e => Try(e.toInt)) | |
println(list2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment