SlideShare a Scribd company logo
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

More Related Content

PDF
Applications of stack
PPTX
Infix-Postfix expression conversion
PPTX
Data structures and algorithms lab3
PPTX
Stack and queue -polish notations
PPTX
Infix to postfix conversion
PPT
Java findamentals1
PPT
Java findamentals1
PPT
Java findamentals1
Applications of stack
Infix-Postfix expression conversion
Data structures and algorithms lab3
Stack and queue -polish notations
Infix to postfix conversion
Java findamentals1
Java findamentals1
Java findamentals1

What's hot (19)

PPTX
Operators in java
PPTX
DATA STRUCTURE - STACK
PPTX
Data structures and algorithms lab2
PPTX
Unit 3 stack
ODP
Demystifying Eta Expansion
PPTX
Operators in Java
PDF
Reactive programming
PPT
Intro To Scala
PDF
OOPS With CSharp - Jinal Desai .NET
PPT
Conversion of Infix To Postfix Expressions
PPTX
Java 8 Intro - Core Features
PDF
Overriding methods
PDF
Python - Lecture 10
PPTX
stack
DOC
5.program structure
PPT
358 33 powerpoint-slides_9-stacks-queues_chapter-9
ODP
OCL 2.4. (... 2.5)
PPTX
Deque and its applications
PPTX
Java 8 stream and c# 3.5
Operators in java
DATA STRUCTURE - STACK
Data structures and algorithms lab2
Unit 3 stack
Demystifying Eta Expansion
Operators in Java
Reactive programming
Intro To Scala
OOPS With CSharp - Jinal Desai .NET
Conversion of Infix To Postfix Expressions
Java 8 Intro - Core Features
Overriding methods
Python - Lecture 10
stack
5.program structure
358 33 powerpoint-slides_9-stacks-queues_chapter-9
OCL 2.4. (... 2.5)
Deque and its applications
Java 8 stream and c# 3.5
Ad

Viewers also liked (7)

PPT
Spark Streaming Info
ODP
Bigtop june302013
PPT
Hadoop applicationarchitectures
ODP
Apache bigtopwg7142013
PPTX
Demographics andweblogtargeting
ODP
Training
ODP
Bigtop elancesmallrev1
Spark Streaming Info
Bigtop june302013
Hadoop applicationarchitectures
Apache bigtopwg7142013
Demographics andweblogtargeting
Training
Bigtop elancesmallrev1
Ad

Similar to Odersky week1 notes (20)

PDF
Introduction to Scala for JCConf Taiwan
PDF
Power of functions in a typed world
PDF
Functional programming ii
PDF
Scala Functional Patterns
PDF
Introductiontoprogramminginscala
PDF
Scala by Luc Duponcheel
PPTX
Scala Introduction
PDF
Scala cheatsheet
PDF
Scala or functional programming from a python developer's perspective
PDF
Meet scala
ODP
Functional Objects & Function and Closures
PPTX
PPT
An introduction to scala
PDF
Functional Operations - Susan Potter
ODP
Functional Objects & Function and Closures
PDF
여자개발자모임터 6주년 개발 세미나 - Scala Language
PPTX
Practically Functional
PDF
Scala intro workshop
PPTX
Scala training workshop 02
ODP
Scala oo (1)
Introduction to Scala for JCConf Taiwan
Power of functions in a typed world
Functional programming ii
Scala Functional Patterns
Introductiontoprogramminginscala
Scala by Luc Duponcheel
Scala Introduction
Scala cheatsheet
Scala or functional programming from a python developer's perspective
Meet scala
Functional Objects & Function and Closures
An introduction to scala
Functional Operations - Susan Potter
Functional Objects & Function and Closures
여자개발자모임터 6주년 개발 세미나 - Scala Language
Practically Functional
Scala intro workshop
Scala training workshop 02
Scala oo (1)

More from Doug Chang (6)

PPTX
BRV CTO Summit Deep Learning Talk
PPTX
DOC
Capital onehadoopclass
PPT
Capital onehadoopintro
PPTX
L'Oreal Tech Talk
PPTX
Hadoop/HBase POC framework
BRV CTO Summit Deep Learning Talk
Capital onehadoopclass
Capital onehadoopintro
L'Oreal Tech Talk
Hadoop/HBase POC framework

Recently uploaded (20)

PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
communication and presentation skills 01
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPT
Total quality management ppt for engineering students
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PPTX
Fundamentals of Mechanical Engineering.pptx
PPTX
Artificial Intelligence
PPTX
UNIT - 3 Total quality Management .pptx
PDF
737-MAX_SRG.pdf student reference guides
PDF
Visual Aids for Exploratory Data Analysis.pdf
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Information Storage and Retrieval Techniques Unit III
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
communication and presentation skills 01
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Total quality management ppt for engineering students
III.4.1.2_The_Space_Environment.p pdffdf
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
Fundamentals of Mechanical Engineering.pptx
Artificial Intelligence
UNIT - 3 Total quality Management .pptx
737-MAX_SRG.pdf student reference guides
Visual Aids for Exploratory Data Analysis.pdf
R24 SURVEYING LAB MANUAL for civil enggi
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Exploratory_Data_Analysis_Fundamentals.pdf
Safety Seminar civil to be ensured for safe working.
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
UNIT 4 Total Quality Management .pptx
Information Storage and Retrieval Techniques Unit III

Odersky week1 notes

  • 1. Odersky Scala Week 1 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 slide before  scala> object test extends (Int=>Int){  | override def apply(x:Int)={  | x+10  | }  | }  defined object test
  • 4. 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
  • 5. 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
  • 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  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
  • 8. 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
  • 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]