Scala the good and bad parts

2,607 views

Published on

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,607
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
17
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Scala the good and bad parts

  1. 1. SCALA :GOOD/BAD PARTS Xuefeng.Wu 2014.01.07
  2. 2. PROGRAMMING GOOD PARTS bad parts?
  3. 3. 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, objectoriented, logic and symbolic programming.
  4. 4. PROGRAMMING PARADIGMS Turing Machine: Imperative Programming Turing Machine: Object-oriented Programming λ-calculus: Functional Programming First-order logic: Logic Programming
  5. 5. PROGRAMMING PARADIGMS ! Turing Machine: Object-oriented Programming λ-calculus: Functional Programming
  6. 6. OBJECT-ORIENTED PROGRAMMING An object has state (data) and behaviour (code). is an approach to designing modular reusable software systems. Increased understanding Ease of maintenance encapsulation and information hiding Ease of evolution inheritance and polymorphism
  7. 7. INCREASED UNDERSTANDING Less Code write less code Less Logic Less new things no surprise solve problem
  8. 8. EASE OF EVOLUTION change self and not affect others be careful of dependence be careful of status change object: data and behaviour always together?
  9. 9. 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 Effective ? FP / OO
  10. 10. FP immutable OO encapsulation
  11. 11. 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.).
  12. 12. SHOW ME YOUR CODE
  13. 13. Java Scala public class Point {
 
 private int x;
 private int y;
 
 public Point(int x, int y) {
 this.x = x;
 this.y = y;
 }
 
 
 public public public public } int getX() {return x;}
 int getY() {return y;}
 void setX(int x) {this.x = x;}
 void setY(int y) {this.y = y;}
 case class Point(x: Int, y: Int)

  14. 14. Java 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; Scala points.filter(_.y > 0).sortBy(_.x)
  15. 15. 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;
 Java 
 default: return false;
 }
 } businessResult match {
 case OKResult(createdAccount: CreatedAccount) => Created case FailedResult(emailAlreadyExists) => Conflict …
 case FailedResult(_) => BadRequest
 Scala }
  16. 16. AN INTERN Worker: salary Student: courses Employee: company Intern trait trait trait trait class Person
 Employee extends Person
 Student extends Person
 Worker extends Person
 Intern extends Person with Employee with Student with Worker * Scala’s Stackable Trait Pattern
  17. 17. Java 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;
 }
 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))
 }
 }
  18. 18. ENDLESS scalaz Haskell 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
  19. 19. 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
  20. 20. LEARN CURL MOOC :Functional Programming Principles in Scala
  21. 21. INCOMPATIBLE
  22. 22. orm PK Storm lime
  23. 23. KILLER

×