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.
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
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. 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
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.).
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. 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. 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. 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. 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. 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. 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