0% found this document useful (0 votes)
52 views40 pages

Scala Introduction: Saiful Bahri

Scala is a multi-paradigm programming language that is both object-oriented and functional. It runs on the Java Virtual Machine but can also compile to JavaScript or .NET. Scala has advanced type systems and allows both object-oriented and functional programming approaches. Martin Odersky started development of Scala in 2001 and it has grown in popularity for use in industry at companies like LinkedIn, Twitter, and Spotify.

Uploaded by

Saiful Bahri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views40 pages

Scala Introduction: Saiful Bahri

Scala is a multi-paradigm programming language that is both object-oriented and functional. It runs on the Java Virtual Machine but can also compile to JavaScript or .NET. Scala has advanced type systems and allows both object-oriented and functional programming approaches. Martin Odersky started development of Scala in 2001 and it has grown in popularity for use in industry at companies like LinkedIn, Twitter, and Spotify.

Uploaded by

Saiful Bahri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

SCALA INTRODUCTION

Saiful Bahri
SCALABLE LANGUAGE
➤ a Modern multi-paradigm programming language
➤ Object oriented and functional
➤ Statically typed - advanced type system
➤ Compiled to JVM bytecode
but also to CLR, and to JavaScript
➤ Very good interoperability with Java
➤ Concise, elegant, and type-safe way
➤ Can do Concurrent & Synchronize processing
HISTORY

1995
Sun and Netscape team up
and announce first beta
release of Java
HISTORY

1995
Philip Wadler tells Martin
Odersky about Java.

Odersky decides to learn the


language by writing a
compiler
HISTORY

1999
Sun buys Java compiler from
Martin Odersky, it is
shipped with JDK 1.3
HISTORY

1999
Work on adding generics to
Java begins
HISTORY

2001
Martin Odersky starts
working on Scala at

Switzerland
AND THEN
2003 Scala 1.0
2006 Scala 2.0
2010 Scala 2.8
2011 Received a $3
million
“If I were to pick a
language to use today
other than Java, it
would be Scala”

James Gosling
NOW
➤ Open source projects in
Scala:
Akka, Lift, Spark, Kafka, Scalajs,
playframework, linkerd,
openwhisk, scio, …

https://github.com/trending/scala

➤ Tools:
built-in REPL, Eclipse, IntelliJ
and NetBeans plug-ins
integration modules for almost
every popular Java framework

http://scala-ide.org/
NOW
➤ Used in industry:
LinkedIn, Twitter, Spotify,
Foursquare, SAP
Novell, EDF, Coursera, IBM
The Guardian, Xebia, Xerox,
Sony, Siemens, …
INFLUENCES OF SCALA DESIGN
➤ Java, C#
syntax, basic types, and class libraries,
➤ Smalltalk
uniform object model,
➤ Eiffel
uniform access principle,
➤ Beta
systematic nesting,
➤ ML, Haskell
many of the functional aspects.
SCALA IS OBJECT ORIENTED
➤ Every value is object

val a = 1 + 2

val b = 1.+(2)
SCALA IS FUNCTIONAL
➤ every function is a value

scala>(x: Int) => x + 1


scala>res1(1) // 2

scala>def incFunc(x: Int) : Int =


x+1
scala>incFunc(2) //3
List(1, 2, 3).map((x: Int) => x + 1) à List(2, 3, 4)
With some syntax sugar:
List(1, 2, 3).map(x => x + 1)
List(1, 2, 3).map(_ + 1)
HOW TO INSTALL
➤ Need JDK 1.6 - 1.8
➤ Download Scala (http://www.scala-lang.org/download/)
➤ or get SBT (http://www.scala-sbt.org/)
➤ or install one of the Scala IDEs (http://scala-ide.org/)
HELLO SCALA
HELLO SCALA - TYPE INFERENCE
VAL VS VAR
VAL VS VAR
METHODS (VERBOSE)
METHODS (SHORTER)
METHODS (SHORTER YET)
METHODS (SHORT)
NO NOISE
➤ Semicolon inference
➤ Type inference
➤ Empty and single line blocks don’t need { }
➤ Infix notation for method invocations
➤ Implicit public
➤ Empty parameter lists don’t need ()
java

scala
NO STATEMENTS
➤ Everything is an expression
➤ Return is optional, last expression is returned def plus2(x:Int)
=x+2
➤ for, while, if/else are expressions
➤ Unit equivalent of void
NO “JAVA BEAN” BOILERPLATE
Java Scala
public class Person { class Person( var name: String, var age: Int)
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(age: int) {
this.age = age;
}
}
CASE CLASS
➤ Also generates equals, hashCode and allows decomposition
via pattern matching

abstract class Term


case class Var(name: String) extends Term
case class Fun(arg: String, body: Term) extends Term
case class App(f: Term, v: Term) extends Term
NO SWITCH - PATTERN MATCHING
➤ Extractor objects are auto-generated for case classes

n match {

  case it if 0 until 5 contains it  => "less than five"

  case it if 5 until 10 contains it => "less than ten"

  case _ => "a lot“
}
CLOSURES
LIST

val list = 1 :: 2 :: 3 :: Nil


list.head à1
list.tail à List(2, 3)
list.map(_ + 1) à List(2, 3, 4)
list.filter(_ < 2) à List(3)
list.exists(_ == 3) à true
list.drop(2) à List(3)
list.reverse à List(3, 2, 1)
list.sort(_ > _) à List(3, 2, 1)
List.flatten(list) à List(1, 2, 3)
list.slice(2, 3) à List(3)
...
TUPLES
FOR COMPREHENSIONS
➤ Find all attendees named Fred that speaks Danish

for {
att <- attendees
if att.name == “Fred”
lang <- att.spokenLanguages
if lang == “Danish”
} yield att
NO INTERFACES
➤ Traits for mixin composition
TRAITS FOR MULTIPLE INHERITANCE

val order = new Order(customer)


with Entity
with InventoryItemSet
with Invoicable
with PurchaseLimiter
with MailNotifier
with ACL
with Versioned
with Transactional
MANY MORE …
➤ Far more powerful generics
➤ Self types
➤ High-order types
➤ Structural and dependent types
➤ Declaration-site variance and existential types support
➤ Views (implicit conversions) and implicit arguments
MANY MORE …
➤ Currying, partial functions
➤ Continuations
➤ Built-in XML support
➤ …
twitter scala school docs.scala-lang.org
SCALA IS DEEP
BUT YOU DON’T NEED
TO GO VERY DEEP
TO STILL HAVE FUN
AND BE PRODUCTIVE
THIS PRESENTATION IS BASED ON
➤ http://docs.scala-lang.org/tutorials/
➤ https://github.com/scala/scala.github.com
➤ http://alvinalexander.com/scala/how-to-use-closures-in-scala-
fp-examples
➤ https://www.slideshare.net/whyme/scala-
introduction-6635575
➤ https://www.slideshare.net/whyme/all-about-scala
➤ https://twitter.github.io/scala_school/

You might also like