Odersky Scala Week 1 
Function Types(goal to design APIs) 
Recursion inside an API 
Recursion base case + inductive step 
FoldLeft/FoldRight
Function Types 
 Denoted as Int=>Int or type a = Int=>Int, not 
just a mapping but an apply to each element 
 Abstract, they need an implementation 
 import scala.reflect.runtime.universe._ 
- Scala 2.10+; failed in 2.9.3 didnt debug 
 typeOf[a] and typeOf[a].members 
- scala> typeOf[test].members 
- res16: reflect.runtime.universe.MemberScope = Scopes(method apply, 
constructor test, method toString, method andThen, method compose, method 
$init$, method $asInstanceOf, method $isInstanceOf, method synchronized, 
method ##, method !=, method ==, method ne, method eq, method notifyAll, 
method notify, method clone, method getClass, method hashCode, method 
equals, method wait, method wait, method wait, method finalize, method 
asInstanceOf, method isInstanceOf)
Reference for slide before 
 scala> object test extends (Int=>Int){ 
 | override def apply(x:Int)={ 
 | x+10 
 | } 
 | } 
 defined object test
FunctionTypes 
 Function Type example 
- Int=>Int same as 
new Function1[Int, Int] { 
- def apply(x: Int): Int = x 
- } 
apply default method for anon class Function1, maps 
int input to int output, can add transformations to 
types 
- can override the apply function
Apply example, modify to add 10 
scala> object test extends (Int=>Int){ 
| override def apply(x:Int)={ 
| x+10 
| } 
| } 
defined object test 
scala> test(1) 
res5: Int = 11
Objects vs Classes 
Objects implement a function type signature 
scala> class test extends (Int=>Int){ 
| override def apply(x:Int)={ 
| x+10 
| } 
| } 
defined class test 
scala> val t = new test 
t: test = <function1> 
scala> test(1) 
res6: Int = 11
Factorial 
 Factorial using functional part of Scala 
scala> object factorial extends(Int=>Int){ 
def apply(x:Int):Int={ x*(if(x>1) apply(x-1) else x) 
} 
} 
 defined object factorial 
 scala> factorial(3) 
 res4: Int = 6 
 Use scala to define your API, either a singleton 
or class for others to use
Recursion 
 Base case and inductive step 
 Tail Recursion 
 Recursion uses result of first step in next step. 
- Finding max/min requires looking at each element 
- Uses reduce 
- Fold is like reduce but has initial value 
 Scala collection operations 
- Reduce, foldLeft, foldRight implement TR 
- Not always easy to understand
Factorial 
 scala> def factorial(x:Int) = (1 to x).reduce(_*_) 
 factorial: (x: Int)Int 
 scala> factorial(3) 
 res1: Int = 6
For comprehension 
 Not a for loop 
- For comprehension not a for loop as in Java, is a 
substitute for map/flatMap/filter 
- Reduce/Fold+For comprehension together
Maven + Scalacheck 
 Add Maven 
 Add Scalatest/Scalacheck
Scala Classes 
 Employee[T], Employee[+T], Employee[-T]
Scala Method type bounds

Odersky week1 notes

  • 1.
    Odersky Scala Week1 Function Types(goal to design APIs) Recursion inside an API Recursion base case + inductive step FoldLeft/FoldRight
  • 2.
    Function Types Denoted as Int=>Int or type a = Int=>Int, not just a mapping but an apply to each element  Abstract, they need an implementation  import scala.reflect.runtime.universe._ - Scala 2.10+; failed in 2.9.3 didnt debug  typeOf[a] and typeOf[a].members - scala> typeOf[test].members - res16: reflect.runtime.universe.MemberScope = Scopes(method apply, constructor test, method toString, method andThen, method compose, method $init$, method $asInstanceOf, method $isInstanceOf, method synchronized, method ##, method !=, method ==, method ne, method eq, method notifyAll, method notify, method clone, method getClass, method hashCode, method equals, method wait, method wait, method wait, method finalize, method asInstanceOf, method isInstanceOf)
  • 3.
    Reference for slidebefore  scala> object test extends (Int=>Int){  | override def apply(x:Int)={  | x+10  | }  | }  defined object test
  • 4.
    FunctionTypes  FunctionType example - Int=>Int same as new Function1[Int, Int] { - def apply(x: Int): Int = x - } apply default method for anon class Function1, maps int input to int output, can add transformations to types - can override the apply function
  • 5.
    Apply example, modifyto add 10 scala> object test extends (Int=>Int){ | override def apply(x:Int)={ | x+10 | } | } defined object test scala> test(1) res5: Int = 11
  • 6.
    Objects vs Classes Objects implement a function type signature scala> class test extends (Int=>Int){ | override def apply(x:Int)={ | x+10 | } | } defined class test scala> val t = new test t: test = <function1> scala> test(1) res6: Int = 11
  • 7.
    Factorial  Factorialusing functional part of Scala scala> object factorial extends(Int=>Int){ def apply(x:Int):Int={ x*(if(x>1) apply(x-1) else x) } }  defined object factorial  scala> factorial(3)  res4: Int = 6  Use scala to define your API, either a singleton or class for others to use
  • 8.
    Recursion  Basecase and inductive step  Tail Recursion  Recursion uses result of first step in next step. - Finding max/min requires looking at each element - Uses reduce - Fold is like reduce but has initial value  Scala collection operations - Reduce, foldLeft, foldRight implement TR - Not always easy to understand
  • 9.
    Factorial  scala>def factorial(x:Int) = (1 to x).reduce(_*_)  factorial: (x: Int)Int  scala> factorial(3)  res1: Int = 6
  • 10.
    For comprehension Not a for loop - For comprehension not a for loop as in Java, is a substitute for map/flatMap/filter - Reduce/Fold+For comprehension together
  • 11.
    Maven + Scalacheck  Add Maven  Add Scalatest/Scalacheck
  • 12.
    Scala Classes Employee[T], Employee[+T], Employee[-T]
  • 13.