SCALA :GOOD/BAD PARTS Xuefeng.Wu 2014.01.07
GOOD PARTSbad parts?
PROGRAMMING
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.
PROGRAMMING PARADIGMS
Turing Machine: Imperative Programming
Turing Machine: Object-oriented Programming
λ-calculus: Functional Programming
First-order logic: Logic Programming
PROGRAMMING PARADIGMS
!
Turing Machine: Object-oriented Programming
λ-calculus: Functional Programming
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
INCREASED UNDERSTANDING
Less Code
Less Logic
Less new things
write less code
no surprise
solve problem
EASE OF EVOLUTION
be careful of dependence
be careful of status
change object: data and behaviour
change self and not affect others
always together?
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 ?
encapsulation
immutableFP
OO
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.).
SHOW ME YOUR CODE
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
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;
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}
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
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)) }}
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
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
LEARN CURL
MOOC :Functional Programming Principles in Scala
INCOMPATIBLE
PK
Storm
storm
lime
KILLER