Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Scala on the Run

A Short Intro to Scala
by

Nimrod Argov

on 1 August 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Scala on the Run

Scala on the Run
What is Scala?
Scalable Language
Imperative & Functional
JVM Based
Concise
Fun
Scalable
One Liners
Scripts
Full fledged, multi-tier applications
Less Code
Code is Read more than it is Written
Hello World
Scala in the Enterprise
Expect reduction in factor of at least 2 (many times more than that)
object Hello extends App {
println(
"Hello World!"
)
}
Some Basics
Variables
val message = "Hello World"
val x: Int = 5
Functions
General
def add(x: Int, y: Int): Int = {
x + y
}
One Line
def add(x: Int, y: Int): Int = x + y
Short Version (Anonymous)
(x, y) => x + y
Even Shorter
_ + _
Control
Structures
if ( x > y ) x else y
To Be Continued...
If
If is an Expression
val bigger = if (x > y) x else y
Control
Structures
Cont.
While / Do While
Same as in Java:

var i = 0
while(i < 10){
// Do Something
i += 1
}
But...
var line = ""
do {
line = readLine()
println(line)
} while line != ""
Control
Structures
Cont.
Recursion
Functional
Concise
Can be more readable (depends on scope)
def gcd(x: Long, y: Long): Long = {
var a = x
var b = y
while (a != 0){
val temp = a
a = b % a
b = temp
}
b
}
def gcd(x: Long, y: Long): Long = {
if (y == 0) x else gcd(y, x % y)
}
Control
Structures
Cont.
For
for (file <- files)

for (i <- 1 to 4)

for (i <- 0 until 4)
Same as Java's for(File file : files)
1 to 4 generates a Range of the numbers 1 to 4 inclusive
0 until 4 generates a Range of the numbers 0 to 4 not inclusive (0, 1, 2, 3)
For Filtering
for (file <- files if file.getName.endsWith(".scala"))
Advanced Topics
Mid Stream Variable Binding
Collection Production (for-yield)
Exception
Handling
try {
...
}
catch{
case e: FileNotFoundException => // do something
case e: IOException => // do something else
}
finally{
...
}
Match
Expressions
Match
someString match {
case "foo" => print ("foo-ed")
case "bar" => print ("bar-ed")
case _ => print("huh?")
}
Missing Something?
No breaks
No Fall-Throughs
Nimrod Argov
Tikal Knowledge

Classes
Simple Class
class Accumulator {
var sum = 0
}
Singleton
object Accumulator {
def add(x: Int): Unit = sum += x
}
Class
Members
Public is Default
class Foo {
val msg = "Foobar"
}
val bar = new Foo()
println(bar.msg)
Private Members
class foo {
private msg: String = ""
}
Class Methods (Member Functions)
class foo {
def bar (msg: String): String = msg + " bar"
}
Packages
&
Imports
Java Style
package com.tikalk.scala
class Foo ...
Namespace (C# style)
package com.tikalk.scala {
class Foo
}
Import Anything, Anywhere
import com.tikalk.scala.Foo
import com.tikalk.scala._
import com.tikalk.scala.Foo._

But also...
import com.tikalk.scala.{Foo, Bar}
import com.tikalk.scala.{Foo => Fu}
import java.sql.{Date => SDate}
import com.tikalk.scala.{Foo => _, _}
Try - Catch - Finally
Immutable Value
Mutable Value
var y = 2
y += 1
y++
Tuples
val point = (x, y)
Actually...
Abstract Class
abstract class Accumulator{
def add(x: Int): Unit
}
Constructors
Primary Constructor
class Coordinate(x: Int, y: Int) {
val xPos = x
val yPos = y
}
Auxiliary Constructors
class Coordinate(x: Int, y: Int) {
val xPos = x
val yPos = y

def this(x: Int) = this(x, 0)
def this() = this(0, 0)
Extending
Classes
Inheritance
class Foo extends Bar { ... }
Superclass Constructors
class Element(name: String) { ... }

class Point(x: Int, y: Int) extends Element("point at " + x + "," + y) { ... }
Overriding Members
class Cat {
val dangerous = false
}

clasee Tiger extends Cat {
override
val dangerous = true
}
Parametric
Fields
Parametric Fields
class Point(val x: Int, val y: Int)
Parametric Override
class Cat(val dangerous: Boolean = false)

class Tiger(
override val dangerous: Boolean = true,
private val age: Int
) extends Cat
Traits
Definition
trait Philosophical {
def philosophize() {
println("I think, therefore I am")
}
}
Usage
class Frog extends Philosophical

or...

class Animal
class Frog extends Animal with Philosophical
Traits
Cont.
Stackable Modifications
abstract class IntQueue{
def get(): Int
def put(x: Int)

class BasicIntQueue extends IntQueue{
private val buff = new ArrayBuffer[int]
def get() = buff.remove(0)
def put(x: Int) { buff += x }
}

trait Doubling extends IntQueue{
abstract override def put(x: Int) {super.put(2 * x)}
}
Traits
Cont.
Stackable Modifications
trait Incrementing extends IntQueue {
abstract override def put(x: Int) {super.put(x + 1)}
}

class MyQueue Extends BasicIntQueue with Doubling with Incrementing
Case
Classes
Remember Match?
foo match {
case "bar" => // do something
case Point(x,y) => // this is a Point
case _ => // default
}
Case Class
case class Point(val x: Int, val y: Int)
Martin Odersky
Implicit
Conversions
Complex Implicit Casting
implicit def Double2Complex(value : Double) =
new Complex(value, 0.0)
Builtin Conversions
class Predef
Full transcript