scala the good and bad parts

27
SCALA :GOOD/BAD PARTS Xuefeng.Wu 2014.01.07

Upload: benewu

Post on 10-May-2015

1.477 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Scala the good and bad parts

SCALA :GOOD/BAD PARTS Xuefeng.Wu 2014.01.07

Page 2: Scala the good and bad parts

GOOD PARTSbad parts?

PROGRAMMING

Page 3: Scala the good and bad parts

PROGRAMMING PARADIGM

A programming paradigm is a fundamental style of computer programming, a way of building the structure and elements of computer programs. !There are five main paradigms: imperative, functional, object-oriented, logic and symbolic programming.

Page 4: Scala the good and bad parts

PROGRAMMING PARADIGMS

Turing Machine: Imperative Programming

Turing Machine: Object-oriented Programming

λ-calculus: Functional Programming

First-order logic: Logic Programming

Page 5: Scala the good and bad parts

PROGRAMMING PARADIGMS

!

Turing Machine: Object-oriented Programming

λ-calculus: Functional Programming

Page 6: Scala the good and bad parts

OBJECT-ORIENTED PROGRAMMING

is an approach to designing modular reusable software systems.

An object has state (data) and behaviour (code).

Increased understanding Ease of maintenance Ease of evolution

encapsulation and information hiding

inheritance and polymorphism

Page 7: Scala the good and bad parts

INCREASED UNDERSTANDING

Less Code

Less Logic

Less new things

write less code

no surprise

solve problem

Page 8: Scala the good and bad parts

EASE OF EVOLUTION

be careful of dependence

be careful of status

change object: data and behaviour

change self and not affect others

always together?

Page 9: Scala the good and bad parts

FUNCTIONAL PROGRAMMING

computation as the evaluation of mathematical functions and avoids state and mutable data.

emphasises functions that produce results that depend only on inputs and not on the program state

Increased understanding Ease of maintenance Ease of evolution

FP / OOEffective ?

Page 10: Scala the good and bad parts

encapsulation

immutableFP

OO

Page 11: Scala the good and bad parts

WHAT IS SCALA

Scala (/ˈskɑːlə/ skah-lə) is an object-functional programming and scripting language for general software

applications, statically typed

designed to concisely express solutions in an elegant, type-safe and lightweight (low ceremonial) manner.

Scala has full support for functional programming (including currying, pattern matching, algebraic data

types, lazy evaluation, tail recursion, immutability, etc.).

Page 12: Scala the good and bad parts

SHOW ME YOUR CODE

Page 13: Scala the good and bad parts

public class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() {return x;} public int getY() {return y;} public void setX(int x) {this.x = x;} public void setY(int y) {this.y = y;}}

case class Point(x: Int, y: Int)

Java Scala

Page 14: Scala the good and bad parts

Java Scala

points.filter(_.y > 0).sortBy(_.x)

points.filter(_.y > 0).sortBy(_.x)

List filtered = new ArrayList<Point>();for(Point p:points){ if(p.y > 0) { filtered.add(p); }} Collections.sort(points, new Comparator<Point>() { public int compare(Point p0, Point p1) { return p1.getX() - p0.getX(); }});return points;

Page 15: Scala the good and bad parts

Java

Scala

public boolean checkPrime(int number) { // checks if a number between 1 and 10 is prime switch (number) { case 1: return true; case 2: return true; case 3: return true; case 5: return true; case 7: return true; default: return false; }}

businessResult match { case OKResult(createdAccount: CreatedAccount) => Created case FailedResult(emailAlreadyExists) => Conflict … case FailedResult(_) => BadRequest}

Page 16: Scala the good and bad parts

AN INTERN

Worker: salary Student: courses Employee: company

Intern

trait Persontrait Employee extends Persontrait Student extends Persontrait Worker extends Personclass Intern extends Person with Employee with Student with Worker

* Scala’s Stackable Trait Pattern

Page 17: Scala the good and bad parts

public class Quicksort { private int[] numbers; private int number; public void sort(int[] values) { if (values ==null || values.length==0){ return; } this.numbers = values; number = values.length; quicksort(0, number - 1); } private void quickSort(int low, int high) { int i = low, j = high; int pivot = numbers[low + (high-low)/2]; while (i <= j) { while (numbers[i] < pivot) { i++; } while (numbers[j] > pivot) { j--; } if (i <= j) { exchange(i, j); i++; j--; } } // Recursion if (low < j) quicksort(low, j); if (i < high) quicksort(i, high); } private void exchange(int i, int j) { int temp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = temp; }}

Java

Scala

def quickSort[T <: Ordered[T]](list: List[T]): List[T] = { list match { case Nil => Nil case x::xs => val (before, after) = xs partition (_ < x) quickSort(before) ++ (x :: quickSort(after)) }}

Page 18: Scala the good and bad parts

ENDLESS

sealed trait KiloGram def KiloGram[A](a: A): A @@ KiloGram = Tag[A, KiloGram](a) val mass = KiloGram(20.0) 2 * mass res: Double = 40.0

case class KiloGram(value: Double) val mass = KiloGram(20.0) 2 * mass.value

scalaz Haskell

Page 19: Scala the good and bad parts

A NUMBER OF COMPLAINTS

Compile Times - TDD

Libraries and the Community - invocation syntax & terrible documentation

Magic Syntax - ~, — !!

Everything is a Type - HTTP request/response cycle.

‘Local’ Type Inference - Scala can’t perform full type inference across your program

Page 20: Scala the good and bad parts

LEARN CURL

MOOC :Functional Programming Principles in Scala

Page 21: Scala the good and bad parts

INCOMPATIBLE

Page 22: Scala the good and bad parts
Page 23: Scala the good and bad parts
Page 24: Scala the good and bad parts

PK

Storm

storm

lime

Page 25: Scala the good and bad parts

KILLER

Page 26: Scala the good and bad parts
Page 27: Scala the good and bad parts