scala the good and bad parts
TRANSCRIPT
![Page 1: Scala the good and bad parts](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/1.jpg)
SCALA :GOOD/BAD PARTS Xuefeng.Wu 2014.01.07
![Page 2: Scala the good and bad parts](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/2.jpg)
GOOD PARTSbad parts?
PROGRAMMING
![Page 3: Scala the good and bad parts](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/3.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/4.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/5.jpg)
PROGRAMMING PARADIGMS
!
Turing Machine: Object-oriented Programming
λ-calculus: Functional Programming
![Page 6: Scala the good and bad parts](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/6.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/7.jpg)
INCREASED UNDERSTANDING
Less Code
Less Logic
Less new things
write less code
no surprise
solve problem
![Page 8: Scala the good and bad parts](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/8.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/9.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/10.jpg)
encapsulation
immutableFP
OO
![Page 11: Scala the good and bad parts](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/11.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/12.jpg)
SHOW ME YOUR CODE
![Page 13: Scala the good and bad parts](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/13.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/14.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/15.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/16.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/17.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/18.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/19.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/20.jpg)
LEARN CURL
MOOC :Functional Programming Principles in Scala
![Page 21: Scala the good and bad parts](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/21.jpg)
INCOMPATIBLE
![Page 22: Scala the good and bad parts](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/22.jpg)
![Page 23: Scala the good and bad parts](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/23.jpg)
![Page 24: Scala the good and bad parts](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/24.jpg)
PK
Storm
storm
lime
![Page 25: Scala the good and bad parts](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/25.jpg)
KILLER
![Page 26: Scala the good and bad parts](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/26.jpg)
![Page 27: Scala the good and bad parts](https://reader033.vdocument.in/reader033/viewer/2022052822/554f6b9bb4c905c8088b5209/html5/thumbnails/27.jpg)