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

No description
by

Oleg Ilyenko

on 17 March 2016

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Scala

Creator
James Strachan
"I can honestly say if someone had shown me the Programming in Scala book by by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I'd probably have never created Groovy."
Scala as the long term replacement for java/javac?
http://j.mp/On6Xpp
What is
Scala
?
Sca
lable
la
nguage
FP
+
OO
Statically typed
but
with Type Inference
Consistency
Immutability
Parallel/Distributed programming
Open-Source
Features
Scalability
Case Classes
public class User {

private String firstName;

private String lastName;

private int age;

public User(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}

public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public User() { }

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

User user = (User) o;

return age == user.age &&
firstName.equals(user.firstName) &&
lastName.equals(user.lastName);
}

@Override
public int hashCode() {
int result = firstName.hashCode();

result = 31 * result + lastName.hashCode();
result = 31 * result + age;

return result;
}

@Override
public String toString() {
return "User(" +
firstName + ", " +
lastName + ", " +
age + ')';
}
}
Thank you! Questions?
Oleg Ilyenko
Sangria Expert
@easyangel
Twitter:
Project size
Project runtime
Project maintenance
Dimensions
Basics
def

getFullName(
firstName
:
String
,
lastName:

String
):
String
= {

return

firstName
+
" "
+
lastName
}
object
MyApp
extends
App {
println(
"Hello World!"
)
}
// Hello World
def
getFullName(
firstName
:
String
,
lastName
:
String
):
String
= {

val

result
:
StringBuilder
= new StringBuilder


if
(!
firstName
.trim.isEmpty) {

result
append
firstName
}


if
(!
lastName
.trim.isEmpty) {

if
(!
result
.isEmpty) {

result
append
" "
}


result
append
lastName
}


return

result
.toString
}
// Simple Method
// Real World Method
def
getFullName(
firstName
:
String
,
lastName
:
String
) =
List(
firstName
,
lastName
) filterNot (_.trim.isEmpty) mkString
" "
// Nice!
Infix/Dot Notation
a
+
b

a
.+(
b
)



result
.append(
a
).append(
b
).append(
c
)

result
append
a
append
b
append
c
Function Literal
val

fn
:
(Int, Int) => Int
=
(
x
:
Int
,
y
:
Int
) =>
x
+
y

val

fn1
=
(
x
:
Int
,
y
:
Int
) =>
x
+
y

val

fn2
:
(Int, Int) => Int
=
(
x
,
y
) =>
x
+
y

val

fn3
:
(Int, Int) => Int
=
_
+
_

val

fn4
=
(
_
:
Int
) + (
_
:
Int
)
def
getFullName(
firstName
:
String
=
""
,
lastName
:
String
=
""
) =
// ...

getFullName(
firstName
=
"John"
,
lastName
=
"Doe"
)
getFullName(
lastName
=
"Smith"
)
// Default and Named Arguments
createUser(
user
,
true
,
false
,
false
,
true
,
false,

false
)
// How may times you saw this?
createUser(

user
=
user,


encryptPassword
=
true,


admin
=
false,


ldapAuth
=
false,


suspicious
=
true,


blocked
=
false,


visible
=
false
)
// Isn't it better?
def
getFullName(
firstName
:
Option[String]
,
lastName
:
Option[String]
) =
List(
firstName
,
lastName
).flatten mkString
" "

def
getFullNameAnon(
firstName
:
Option[String]
,
lastName
:
Option[String]
) =
List(
firstName
,
lastName
) map (
name
=>
name
getOrElse
"Anonymous"
) mkString
" "

println(getFullNameAnon(None, Some(
"Smith"
)))

// prints: Anonymous Smith
// Option
Everything is an Expression
val

color
=
if
(
user
.isBlocked)
"red"

else

"green"

val

number
=
try

"123"
.toInt
catch
{

case

e
:
NumberFormatException
=>
0
}
public List<User> findUsersByFirstName(List<User> users, String firstName) {
List<User> foundUsers = new ArrayList<User>();

for (user: users) {
if (
user.getFirstName()
.contains(firstName)) {
foundUsers.add(user);
}
}

return foundUsers;
}

public List<User> findUsersByLastName(List<User> users, String lastName) {
List<User> foundUsers = new ArrayList<User>();

for (user: users) {
if (
user.getLastName()
.contains(lastName)) {
foundUsers.add(user);
}
}

return foundUsers;
}
// Traversing (Java)
def
findBy
[
T
]
(
list
:
List[T]
)(
prop
:
T => String
,
value
:
String
) =

list
filter (
item
=>
prop
(
item
) contains
value
)

val
users
= List(User(
"Bob"
,
"Martin"
), User(
"John"
,
"Doe"
))

findBy(
users
)(_.firstName,
"o"
)
findBy(
users
)(_.lastName,
"Mar"
)
// Traversing (Scala)
Pattern Matching
val

user
= User(
"John"
,
"Doe"
,
20
)
val

GoodName
=
"""([A-Z])\w*"""
.r

val

result
= user match {

case
User(
GoodName
(
firstInitial
),
GoodName
(
lastInitial
), _) =>
s
"Initials are: $firstInitial $lastInitial"


case
User(_, _,
age
)
if

age
<
10
=>

"Just a kid"


case
_ =>
"Wrong name!!!"
}
Traits
The list goes on ...
ScalaTest
Like
interfaces
But can contain
concrete methods
Multiple inheritance!
DONE RIGHT
Implicits
Type Classes
Higher Kinds
Lazy Evaluatin
`import`
Parser Combinators
etc...
Project size/maintenance
From small scripts to big enterprise projects
Good IDE support (Eclipse, Intellij IDEA, Netbeans, Vi, Emacs, etc...)
Excellent Java integration
Compiles to Java Bytecode
Runs as any other java application
You can use any Java library
Statically typed
Safe refactorings, <Ctrl + Click> everywhere, etc...
A lot of excellent Books and Documentation
Parallel Collections
Scalaz/cats
Sangria (GraphQL)
Scaldi (DI)
Web frameworks
Play
Unfiltered
Spray
akka-http
etc...
def
isGoodSolvencyScore(
user
:
User
) =
// Slow HTTP request to Schufa

val

users
=
userService
.getSuspiciousUsers

users
filter isGoodSolvencyScore map (_ lastName) foreach println
users
.par
filter isGoodSolvencyScore map (_ lastName) foreach println
Support
Lightbend
Company behind Scala
Commercial support is there (if needed)
Board of advisors:
James Gosling
(Java creator)
Doug Lea
(Java concurrency expert)
Big and fast growing community
Many other consulting companies
http://akka.io
Simple Concurrency & Distribution
Scala and Java API
Actor based concurrency
Just like in Erlang
(famous nine nines reliability)
Asynchronous and distributed by design
"Let it crash" philosophy
Systems that self-heal
Remote and/or local supervisor hierarchies
High Performance
50 million msg/sec on a single machine.
Small memory footprint; ~2.7 million actors per GB of heap.
Akka Abstractions
Actors
Typed Actors
Agents
Futures and Promises
STM (Software Transactional Memory)
FSM (Finite State Machine)
Dataflow Concurrency
Location Transparency
Who's using Scala?
List(Some(
"a"
), None, Some(
"b"
)).flatten
==
List(
"a"
,
"b"
)
Full transcript