scala: object-oriented and functional programming combined · scala: object-oriented and functional...

23
Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Upload: others

Post on 04-Jun-2020

28 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Scala: Object-oriented and functional programming combined

INF3110 - Eivind Barstad Waaler 14.11.2011

Page 2: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Eivind B. Waaler

Fagsjef Java

Systemutvikler og teknoideolog i BEKK. Høgskoleingeniør fra 1998. Master i informatikk UiO 2010.

Page 3: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011
Page 4: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Today – Scala

INF3110-Scala Side 4

§ What?

§ How!

§ Why?

Page 5: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

What?

INF3110-Scala Side 5

§ OO + FP

§ 2001 à 2003 à 2006 à 2011

Page 6: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

JVM – The Java Platform

INF3110-Scala Side 6

Page 7: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Java example

INF3110-Scala Side 7

Page 8: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

ML example

INF3110-Scala Side 8

Page 9: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Scala basics

INF3110-Scala Side 9

§  No semicolons

§  val/var

§  Type after name

§  Type inference

§  Pure OO

§  REPL

<Demo>

Page 10: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Scala basics

INF3110-Scala Side 10

Page 11: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Functional Programming in Scala

INF3110-Scala Side 11

§  Higher-order functions

§  Anonymous functions

§  Pattern matching

§  For-comprehensions

§  Immutable values

§  Currying

<Demo>

Page 12: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Pattern Matching

INF3110-Scala Side 12

Page 13: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

For-comprehensions

INF3110-Scala Side 13

<Demo>

Page 14: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Performance – for vs. while

INF3110-Scala Side 14

§  1000 x 1000 matrix

–  for with yield – 8090 ms

–  for without yield – 3507 ms

–  while – 1865 ms

Page 15: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Object-oriented Programming in Scala

INF3110-Scala Side 15

§  Classes

§  Singleton objects – like static in Java

§  Traits

–  Interface with implementation

–  Multiple inheritance

Page 16: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Classes, objects & traits

INF3110-Scala Side 16

<Demo>

Page 17: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Example – Image processing blur filter

INF3110-Scala Side 17

§  Structuring element – se (here 3 x 3 matrix)

§  New point is average of area covered by se

Page 18: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Combining FP + OO

INF3110-Scala Side 18

Page 19: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Need for functions in Java?

INF3110-Scala Side 19

Page 20: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Conclusion

INF3110-Scala Side 20

§  OO + FP combined

§  Statically typed and compiled

§  JVM leverage

§  High complexity?

§  Performance vs. conciseness

§  The new Java?

Page 21: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Questions and more info

INF3110-Scala Side 21

§  Questions?

§  Scala: http://www.scala-lang.org/

§  Embedding Efficient DSLs on the JVM – Eivind B Waaler

–  DUO: http://www.duo.uio.no/sok/work.html?WORKID=102094

Page 22: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

BEKK CONSULTING AS SKUR 39, VIPPETANGEN. P.O. BOX 134 SENTRUM, 0102 OSLO, NORWAY. WWW.BEKK.NO

Eivind Barstad Waaler Fagsjef

98219322 [email protected]

Page 23: Scala: Object-oriented and functional programming combined · Scala: Object-oriented and functional programming combined INF3110 - Eivind Barstad Waaler 14.11.2011

Fargekode Farge Vekting (veiledende) Kommentar

RGB: 255, 255, 255 Hvit Hovedfarge. Sikrer at foilene i størst mulig grad er oversiktlig og luftige.

RGB: 0, 0, 0 Sort Fontfarge (tekstfarge). Sikrer høy kontrast og god lesbarhet.

RGB: 50, 38, 26 Mørk brun Identitetsfarge. Brukes i illustrasjoner og modeller som alternativ til sort.

RGB: 133, 115, 98 Brun Identitetsfarge. Brukes som overskrift til foiler og i illustrasjoner og modeller etc.

RGB: 203, 192, 183 Lys brun Identitetsfarge. Brukes i illustrasjoner og modeller etc.

RGB: 16, 100, 112 Mørk blå Støttefarge. Brukes forsiktig i illustrasjoner og modeller etc.

RGB: 130, 174, 182 Blå Identitetsfarge. Brukes i illustrasjoner og modeller etc.

RGB: 162, 199, 202 Mellomblå Støttefarge. Brukes i illustrasjoner og modeller etc.

RGB: 194, 216, 215 Lys blå Støttefarge. Brukes i illustrasjoner og modeller etc.

RGB: 240, 78, 82 Rød Spotfarge. Brukes forsiktig for å understreke detaljer i illustrasjoner og modeller etc.

40 %

5 %

15 %

5 %

5 %

15 %

5 %

2,5 %

2,5 %

2,5 %

Fargepalett