Download Scala - Dave Reed

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Combinatory logic wikipedia , lookup

Intuitionistic type theory wikipedia , lookup

Anonymous function wikipedia , lookup

Closure (computer programming) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Standard ML wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Scala (programming language) wikipedia , lookup

Transcript
Scalable Language
“Object-Oriented Meets Functional”
SARAH BIRNBAUM
KATHERINE JOYCE
Overview
History
Design Goals
Components of Scala
Sample Code
Memory Management
Real-Life Application
History
● Marin Odersky
o Java Compiler and Java Generics
o Pizza (1996)
o Programming Methods Laboratory (LAMP)
o Swiss Federal Institute of Technology in Lausanne (EPFL)
● Timeline
o 2001 development began
o 2003 internal release
o 2004 first official announcement
o 2006 Java 2.0
Design Goals
● Integrate object-oriented programming and functional
programming
o Functional: E.g. pass functions as arguments
o OO: has classes, everything is an object
● Wanted to design a language built on the JVM
● Express programs in concise, elegant, and type-safe way
o Statically bound types
o Strongly typed
o Can look very script-y
Everything is an object
● Scala is a pure object-oriented language as everything is
an object
o Java is not a pure OO language - has primitive types
(like boolean and int)
● Even numbers and functions are objects
o Since numbers are objects they have methods
 1+2
 (1).+(2)
Binding Addresses
● Scala uses the JVM so stack/heap allocation are the
same
o References and primitives on stack, Objects on heap
● Scala doesn’t have primitives, but types (like Int or
Long) get compiled to the JVM’s primitive types in
bytecode whenever possible
Data Types & Structures
● Byte, short, int, long, float, double, char, string,
boolean,unit, null, nothing, any, anyref
● Many of the same data structures as Java
o Arrays, Lists, Sets, Maps
● Tuple
o Like a list but can hold objects with different types
o val x = (10, “Scala”)
A Scala Class
class Rational(n: Int, d: Int) {
private val g = gcd(n, d)
val numer: Int = n/g
val denom: Int = d/g
private def gcd(x: Int, y: Int): Int = {
if (x == 0) y
else if (x < 0) gcd(-x, y)
else if (y < 0) -gcd(x, -y)
else gcd(y % x, x)
}
def +(that: Rational) =
new Rational(numer * that.denom + that.numer * denom,denom
*that.denom)
}
Variables
● Scala is statically typed and variables are statically
bound
● When writing code, there are few explicit type
definitions because Scala can usually infer the type
o E.g. val x = 3
 compiler infers that 'x' must be type Int as '3' is a
integer literal. I
● Variables in Scala can be declared as either mutable or
immutable
Mutable Variables
Mutable variables can be changed after they are defined
In Java:
int x = 5
In Scala:
var i = 1 // type inferencing done by compiler
var i : Int = 5
Immutable Variables
Immutable variables cannot be changed once they’ve been
assigned
In Java:
final int x = 1
In Scala:
val i = 1
i += 1 // returns an error since i has been declared
immutable
A Scala Class
class Rational(n: Int, d: Int) {
numer and denom are both
private val g = gcd(n, d)
immutable - their values will
val numer: Int = n/g
not changed after they’ve been
val denom: Int = d/g
assigned
private def gcd(x: Int, y: Int): Int = {
if (x == 0) y
else if (x < 0) gcd(-x, y)
else if (y < 0) -gcd(x, -y)
else gcd(y % x, x)
}
def +(that: Rational) =
new Rational(numer * that.denom + that.numer * denom,denom
*
that.denom)}
Type Inferencing
Gives Scala the feel of being dynamically typed.
Explicit Typing
var mySecondVar : String = “Bread”;
var myThirdVar : Int = 13;
Implicit Typing
var mySecondVar = “Bread”; // Scala compiler infers this
is a string
var myThirdVar = 13; // Scala compiler infers this is an
integer
Functions are Objects
Can pass functions as arguments, store them in variables,
and return them
def sum(n: Int) : Int =
if (n == 0) else n + sum(n - 1)
def sum(n: Int, acc: Int) : Int =
if (n == 0) acc else sum(n - 1, acc + n)
Defining Functions
Python has two types - functions and anonymous functions
def adder(x, y)
return x + y
// can write an anonymous function that does the same
// thing using a lambda expression
adder = lambda x, y: x + y
Defining Functions (cont)
Scala also has functions and anonymous functions
def adder1(x: Int, y: Int): Int = {
return x + y}
def adder2(x: Int, y: Int) = {
x + y}
val adder3 = (x: Int, y: Int) => x + y // anonymous function
While Loops
class Rational(n: Int, d: Int) {
private def gcd(x: Int, y: Int): Int
= {
if (x == 0) y
else if (x < 0) gcd(-x, y)
else if (y < 0) -gcd(x, -y)
else gcd(y % x, x)
}
private val g = gcd(n, d)
val numer: Int = n/g
val denom: Int = d/g
def +(that: Rational) =
new Rational(numer *
that.denom + that.numer * denom,denom *
that.denom)}
/**Prints the sum of all
numbers 1/i where i ranges from
1 to 10.*/
var i = 1
var x = new Rational(0, 1)
while (i <= 10) {
x += new Rational(1,
i)
i += 1
}
println("" + x.numer + "/" +
x.denom)
Sample Code - quicksort
/** Quick sort, imperative style */
def sort(xs: Array[Int]) {
def swap(i: Int, j: Int) {
val t = xs(i); xs(i) = xs(j);
xs(j) = t}
def sort1(l: Int, r: Int) {
val pivot = xs((l + r) / 2)
var i = l; var j = r
while (i <= j) {
while (xs(i) <
pivot) i += 1
while (xs(j) >
pivot) j -= 1
if (i <= j) {
swap(i,
j)
i += 1
j -= 1}}
if (l < j) sort1(l, j)
if (j < r) sort1(i, r)}
sort1(0, xs.length - 1)}
/** Quick sort, functional style */
object sort1 {
def sort(a: List[Int]): List[Int] = {
if (a.length < 2)
a
else {
val pivot = a(a.length / 2)
sort(a.filter(_ < pivot)) :::
a.filter(_ == pivot) :::
sort(a.filter(_ > pivot))}}
def main(args: Array[String]) {
val xs = List(6, 2, 8, 5, 1)
println(xs)
println(sort(xs))}}
Memory Management
● The JVM does automatic memory management
● Garbage collection is very similar to Java
● Scala code tends to generate more (short-lived) garbage
than Java code because of the functional coding style
(garbage collection is quick)
Real-Life Application: Twitter
● One of the main application programming languages
used at Twitter
● Much to Twitter’s infrastructure written in Scala
● Scala code handles billions of operations a day
● Chose Scala over Ruby, Java, C/C++, JavaScript
● Advantages of Scala
o Can be very idiomatic or functional - flexible
o It’s fast - runs on the JVM and can use Java libraries
o The syntax is more flexible than Java
Real-life Application: Coursera
Scala is Coursera’s primary serving language
Why use Scala:
● “Scala provides a type safe
language with powerful
concurrency primitives on top of a
mature technology platform.”
Concerns with Scala:
● Scalac (the scala compiler) is
known to run very slowly
● Scala is much more safe with types
than python
● There are a lot of ways to
write a ton of things in Scala hard to read someone else’s
code
● Scala supports multithreading
much better than python or PHP
● It is possible to use unicode
(such as <|***|>)
● The JVM is a big advantage
● Scala doesn’t have a good IDE
Bibliography
Websites:
www.tutorialspoint.com/scala/scala_functions.htm
docs.scala-lang.org/search.html?q=abstraction
tech.coursera.org/blog/2014/02/18/why-we-love-scala-at-coursera/
twitter.github.io/effectivescala/
www.redfin.com/devblog/2010/05/how_and_why_twitter_uses_scala.html#.VSsKEJTF_H4
www.slideshare.net/Odersky/scala-the-simple-parts?related=1
www.scala-lang.org/files/archive/spec/2.11/
docs.scala-lang.org/tutorials/scala-for-java-programmers.html
www.simplyscala.com/
pizzacompiler.sourceforge.net/doc/tutorial.html
Books:
Beginning Scala by David Pollak
Programming in Scala by Martin Odersky, Lex Spoon, Bill Venners