set software engineering thailand meeting: functional programming with scala - prequel

13
Meet-up, May 28, 2015, Launchpad, Bangkok Roland Petrasch & Brian Topping Welcome!

Upload: roland-petrasch

Post on 12-Aug-2015

37 views

Category:

Software


0 download

TRANSCRIPT

Meet-up, May 28, 2015, Launchpad, BangkokRoland Petrasch & Brian Topping

Welcome!

SET Meet-upProgramming with Scala

Agenda

SET — Software Engineering Thailand

Programming with Scala – Prequel (speaker: Roland Petrasch)

Type Systems / Data Types

Functional Programming

Combination of OOP and FP

Programming with Scala - Object-Oriented meets Functional (Speaker: Brian Topping)

Discussion

Roland Petrasch28.5.2015, SET Meet-up Programming with Scala - Prequel

Slide 2

ProgrammingDebugging

DebuggingProgramming

SET — Software Engineering Thailand

The Interest Group from & (not only) for Developers

Open Group: Members, Sponsors and Organizers welcome

Next topics: HCI Patterns, Scrum, Model-Based UID, Agile Testing, Software Architectures & Frameworks, S.O.L.I.D. & Co., UML ...

Contact

Roland Petrasch, Professor for Software EngineeringThammasat University, Department of Computer Science, Rangsit Campus, Pathum [email protected]@gmail.com

Roland Petrasch28.5.2015, SET Meet-up Programming with Scala - Prequel

Slide 3

Type system

Ruleset: assign types to variables, expressions, functions

Error prevention (type errors), e.g. float calculateThrust(Position p)

Other purposes: documentation, compiler optimization

Evolution

Type safety: type checking

During compile-time (static typing), e.g. C++, C#, Java, Scala

During run-time (dynamic typing), e.g. JavaScript, Ruby, Python

Combination, e.g. C# 4.0 (dynamic)

SET Meet-upProgramming with Scala - Prequel

Roland Petrasch28.5.2015, SET Meet-up Programming with Scala - Prequel

Slide 4

Untyped Primitive Data Types Abstract Data Types Classes …

Assembler Fortran, Pascal, C C++, Java, C#

JavaScript: dynamic typing, implicit type conversion, type inference

var five = '5', two = 2, sth;

sth = five * two; → 10

sth = five + two; → 52

sth = five + two * 5; → 510

sth = five + two * 5 + true; → 510true

sth = five + two * 5 + true / 10; → 5100.1

sth = (five + two * 5 + true) / 10; → NaN

Sth = 10 / 0; → Infinity

SET Meet-upProgramming with Scala - Prequel

Roland Petrasch28.5.2015, SET Meet-up Programming with Scala - Prequel

Slide 5

Type system

Type inference: automatically deduce the type at compile timepartly or completely, e.g. var i = 5; // inferred type: int

Type inference is not only a feature of dynamically typed languages

Static/dynamic typing and strong/weak typing are orthogonal:

Many discussions about strong and weak typing, no precise definition

Weak typing: use a value of one type as if it were a value of another type, e.g. in C: char c = 'A'; c++; c=42;or in JavaScript: var s = 'x' + 3; // get 'x3'

Strong typing e.g. in Java: String s = "1" + ' ' * 2 + false; // 164false int i = '3' * 2.2; // error

Importance of „strong” and semantically rich data types

SET Meet-upProgramming with Scala - Prequel

Roland Petrasch28.5.2015, SET Meet-up Programming with Scala - Prequel

Slide 6

To a C programmer strong typing means pressing the keys harder

Data Types

Data Types and type checking is very (!) important

Example: float calculateThrust(Position p)

2 software components:One is providing and another one is using the calculateThrust function

SET Meet-upProgramming with Scala - Prequel

Roland Petrasch28.5.2015, SET Meet-up Programming with Scala - Prequel

Slide 7

Mars Lander by Brian McMullinSource: Defense Intelligence Agency, DoD, USA

ThrustContolUnitNavigationSystem

calculateThrust(Position p) Call

Germany USA

The thrust is a float. A good data type? Yes? Okay? Right?No: Sematics (unit) not clear: Can be „newton” (metric) or „pound-force” (non metric) → sematically poor in this case

Semantically rich data types and error prevention

„NASA lost a $125 million Mars orbiter because one engineering team used metric units while another used English units for a key spacecraft operation.“(http://edition.cnn.com/TECH/space/9909/30/mars.metric/)

Example 2.0: Thrust calculateThrust(Position p)

A new data type Thrust is needed

class Thrust { private Unit unit; private Value value;

public float getValue(Unit unit) …}

SET Meet-upProgramming with Scala - Prequel

Roland Petrasch28.5.2015, SET Meet-up Programming with Scala - Prequel

Slide 8

Mars Polar LanderSource: NASA

Functional programming

Declarative programming paradigm

Evaluation of functions, avoids changing-state & mutable data,eliminate side-effects

Functions: output depends only on input → error prevention

Example for side-effect in Javaint i=0;if (true != false || ++i==0) { }System.out.println(i); // i is 0

Based on lambda calculus for simple semantics for computation

Anonymous Functions (→ closures), no explicit names

Functions of a single input

Example (λx.x+1) 4 → 4+1 → 5 -function -expression computaton

SET Meet-upProgramming with Scala - Prequel

Roland Petrasch28.5.2015, SET Meet-up Programming with Scala - Prequel

Slide 9

Functional programming

λ-Example 2.0

(λx.x+2)((λy.y*10)4)

SET Meet-upProgramming with Scala - Prequel

Roland Petrasch28.5.2015, SET Meet-up Programming with Scala - Prequel

Slide 10

Alonzo Church (1903–1995), Photo: Wikipedia

"Answer to the Ultimate Question of Life, the Universe, and Everything“(The Hitchhiker's Guide to the Galaxy)

Combination of object-oriented and functional paradigms

OO and FP have similarities, e.g.

Expressive power (semantics)

Encapsulation of small parts (reduction of complexity)

Data types

...

OOP: High in cohesion (class consists of data and functions) and low in coupling (classes should be independent as far as possible)

FP: Data is only loosely coupled to functions, functions hide their implementation, immutability, no side-effects

Multi-paradigm languages like Scala combine the features from object-oriented and functional paradigms

SET Meet-upProgramming with Scala - Prequel

Roland Petrasch28.5.2015, SET Meet-up Programming with Scala - Prequel

Slide 11

Combination of Object-oriented and Functional (object-functional)

OOP & FP are orthogonal concepts, i.e. they are not contradictory, e.g.encapsulation, inheritance, polymorphism, abstration do not imply mutability

SET Meet-upProgramming with Scala - Prequel

Roland Petrasch28.5.2015, SET Meet-up Programming with Scala - Prequel

Slide 12

calcValueIntegerValue

Class

calcValue

OO: Every value is an object; attributes & behavior of an object are described by a class; objects are instances of a class)

FP: Every function is a value; and every value is an object → so every function is an object

This was the little prequel,thank you for your attention

SET Meet-upProgramming with Scala - Prequel

Brian Topping

Entrepreneurial, results-oriented architect

Developer and leader with > 20 years experience

Companies: Apple, Oracle, Nokia, AT&T, Intel and Bloomberg.

Dozens of programming & scripting languages, OS, and IDE