Your SlideShare is downloading. ×
0
Scala Intro
val you = new Developer[Any] with ScalaKnowledge
Is Scala really hard?

Can I start using it without going mad?

Why zombies?
Scala History

.
Scala
2003 AD
•
•
•
•
•

Pizza
GJ
generics in Java
Funnel
Scala
Scala is a pure object-oriented language
Scala is also a functional language
Strongly statically typed
Runs on JVM, compil...
Scala – scalable language
def factorial(x: BigInt): BigInt =
if (x == 0) 1 else x * factorial(x - 1)
In contrast with C++,
the more you study Scala, the easier it gets.
Unknown Scala community member
Scala in Production

http://www.scala-lang.org/old/node/1658
Hello …

object HelloZombie extends App {
println("Hello, " + "Zombie!")
}
Another Hello

object HelloZombie {
def main(args: Array[String]) {
println("Hello, " + "Zombie!")
}
}
You do not need CS degree to code in Scala
Application Programmers:
A1 – A3 levels
Library Designers:
L1 – L3 levels

«One...
Scala functional basics
#1 - Mutability is evil

#2 – Everyhting returns data

#3 – Functions are first-class citizens
Mutability is evil
• Mutable objects are complicated to think about
• Defensive copies

• Need to be synchronized in multi...
Mutability is evil
val salary = "2000$"
salary = "2500$" // does not compile

var salary = "2000$”
salary = "2500$” // com...
Everything returns data

val result = if (true) value1 else value2
val result = for (i <- 1 to 10) yield i
def getRandom()...
Functions are first-class citizens

Assign function definitions to variables
val doubled = (x:Int) => x * 2
in Scala a function value is an object!

Scala => pure OOP & full-blown functional language
Sooo many objects around

Baaaad performance, yeah?
http://readwrite.com/2011/06/06/cpp-go-java-scala-performance-benchmark
Scala OOP basics
#1 - Class
#2 - Object = build-in singleton pattern

#3 – Case class
#4 – Abstract class vs Trait
#5 – Multiple inheritanc...
Class
Class is similar to any other languages’ class notion
class MyZombie {
private val index: Int = 0
val name: String =...
Class + Constructor

class MyZombie(index: Int, val name: String) {
println("me likes brainz!")
}
Object

Objects are singletons
object LoneZombie {
def growl = println("me sooo sad")
}
Companion Object
class CrawlerZombie(name: String) {
def crawl() = println("crawling...")
}
object CrawlerZombie {
def app...
Case Class
Case class = class + “sugar” + compiler hint
case class Zombie(index: Int, name : String)
Case Class Sugar

1. Properties
2. Pattern Matching
3. Companion Object
Abstract class vs Trait

Abstract class defines structure
Trait defines behaviour
Abstract Class
abstract class Undead(name: String) {
def eat() = {
println("Om nom nom")
digest()
}
def digest()
}
class Z...
Trait
abstract class Undead(name: String) {
def eat() = {
println("Om nom nom")
digest()
}
def digest()
}
trait Growling{
...
Scala Basic Types
#1 - Scala basic types are objects
#2 – Scala implement it’s own types, wraps and extends
some Java classes and types

#3 ...
Value type

Range

Byte

8-bit signed two's complement integer (27 to 27 - 1, inclusive)

Short

16-bit signed two's compl...
Operators are methods

Operators uses infix notation
val sum = 1 + 2

// Scala invokes (1).+(2)
Class Hierarchy
Null ?

var zombie: Zombie = null
zombie = new Zombie("Alex")
Nothing?

def throwLocalizedException(key: String): Nothing = {
throw LocalizedException(resolveMessage(key))
}
Functions vs methods
Functions and methods are not the same!
©
Functions

No side effects
Evaluates only body and parameters
Compiles to FunctionN
Defined mostly in Objects
Functions

object KillerModule {
def kill = (z: Zombie) => println("Killing " + z.name)
}
Methods

Defined in classes
Works with class scope
Methods

class ZombieTortureMachine(z: Zombie) {
def kill() = println("Killing " + z.name)
def starve() = println("Starvin...
Functions are first-class citizens

Assign function definitions to variables
val doubled = (x:Int) => x * 2
Functions are first-class citizens
Pass function as a parameter
def traverseTree (callback: (Element) => Unit) {
val eleme...
Functions are first-class citizens

Return function as a result value
def prepareCalculator(x: Int, y: Int): () => Int = {...
Type Inference
You can omit types in declarations!
The compiler will infer them for you.
Less typing -> less reading -> happier developer...
Type inference with variables

private val index = 0
val name = "Zombie0"
private val index: Int = 0
val name: String = "Z...
Type inference with functions

Scala compiler can not read thoughts 
def func (a: Int, b: String) = { a + b } // compiles...
Type inference

def getZombies(severity: Int) = {
if (severity > 10) {
List()
} else {
List(Zombie("Mig"), Zombie("Alex"))...
Lambda Syntax

val doubler = (n: Int) => n * 2
val yadobler = { n: Int => n * 2 }
Closures
Closure is special kind of a function
Closure encloses the local lexical context inside the function body
Closures can be ...
Simple Closure

val multiplier = 2
val doubler = (n: Int) => n * multiplier
Closure vs Function

Pure function calculates its result solely in terms of its arguments!

Closure can use the outer lexi...
Collections
Sugar : Map Initialization
Java:
Map<String, Integer> mappings = new HashMap<String, Integer>() {{
put("One", 1);
put("Two...
Sugar : Filtering
Java:
List<Integer> numbers = new ArrayList<Integer>(){{
add(1); add(2); add(-55); add(-33); add(122);
}...
Sugar : Classification
Java:
List<Integer> numbers = new ArrayList<Integer>(){{
add(1); add(2); add(-55); add(-33); add(12...
Stuff : Tuples
• Tuples can be viewed as simple immutable collections.
• Tuple can contain up to 22 elements of different ...
Stuff : Tuples
val pair = (22, "zombies")
val pair = (22 -> "zombies")
// the type is Tuple2[Int, String]

println(pair._1...
Mutable vs Immutable

Prefer immutable collections by default. Period.
Google for details.

Scala uses immutable collectio...
Collections practices
Map, Set and List are mostly used collections in Scala
Use traits’ companion object calls to create
...
Collections API
example #1
case class User(name: String, password: String)
val users = List("admin:nimda", "user1:asddsa",...
Collections API
example #2
val names = List(
"Alex,Viktor,Eugeny",
"Dmitry,Yegor, Sergey",
"Michael,Sergey")

val splitted...
Loops & For comprehension
Loops & For comprehension
• Scala has a while loop
• Scala has a do-while loop
• They are not expressions (the return type...
Loops & For comprehension
Because the while loop results in no value, it is often left out of
pure functional languages.
S...
While loop
while (brains.size > 0) {
zombie eat brains
}
Do-While loop
do {
zombie eat brains
} while (brains.size > 0)
Friendly advise

Try use recursion instead of loops!
And don’t feed zombies
For expression
For expression
• Looks like a for-loop
• Behaves as a swiss knife 
For + generator

for (zombie <- zombieHorde)
kill(zombie)
For + generator + filter

for (zombie <- zombieHorde if zombie.isMoving)
kill(zombie)
For + generator + filter + yield
This will produce a new collection!

val killedZombies =
for (zombie <- zombieHorde if !z...
For expression
Can be used instead of map, filter and flatMap combination
Pattern Matching
Simple matching
val status: String = "R”
val translated = status match {
case "R" => "running”
case "D" => "digesting”
cas...
Structure matching
case class User(name: String, lastname: String, age: Int)
val users = List(
User("Alexey", "Migutsky", ...
Structure matching
def describe(list: List[Int]) {
list match {
case first :: second :: third :: tail => println("First ca...
Exception handling
try {
//something
} catch {
case e: IllegalArgumentException => println("Illegal argument")
case _ => p...
Implicits
Implicits

The compiler can insert parameters and call conversion methods
automatically based on the types used
This behav...
#1 - Implicit parameters
#2 – Implicit type conversion
#3 – “Pimp my Library”
Implicit parameters
case class Context(data: List[String])
implicit val context = Context(List("a", "b", "c"))
object Some...
Implicit type conversion
Compiler will use the implicit conversion method automatically!
case class User(first: String, la...
Pimp My Library
case class User(first: String, last: String)
implicit class ExtendedString(str: String) {
def toUser: User...
Moar Sugar!
For those who survived
Stuff : Object equality
Use == to compare anything

1 == 1.0 // true
List(1,2,3) == List(1,2,3) // true
null == List(1,2,3...
Stuff : Object equality
The equality operator == do the following

1. Check the left side for null
2. If left side is not ...
Stuff : Packages and Imports
• Scala code resides in the Java platform’s global
hierarchy of packages.
• Package definitio...
Stuff : Packages and Imports
• The other way you can place code into packages in Scala is
more like C# namespaces (called ...
Stuff : Packages and Imports
In Scala, packages and their members can be imported using
import clauses. Imported items can...
Stuff : Packages and Imports
• Imports may appear anywhere
• Import may refer to objects (singleton or regular) in additio...
Stuff : Identifiers
1. CamelCase
2. Alphanumeric for variables – letters + digits
3. Do not use starting $ - it is reserve...
Stuff : Method call

Method with 0 parameter can be called without ()
Method with 1 parameter can be called using infix no...
Stuff : Method call
Use higher-order functions with infix notation, but not with mixed notation

List("abcd","zyxwvu").map...
Stuff : Method calls

Use infix notation only with methods without side effects
(which do not modify internal class state)...
Lazy evaluation

An expression that has a value, but that is not
evaluated until it's actually needed (in another words
– ...
Call by-name parameter
Lazy evaluated parameter
def callByValue(x: Int) = {
//parameter get evaluated upon function call
p...
Lazy initialization

When a val is declared with the lazy modifier the righthand side of the value (the definition) will n...
Stuff : Streams

A stream is like a list except that its elements are
computed lazily. Because of this, a stream can be
in...
Stuff : Streams

Classical example: Fibonacci sequence
def fibFrom(a: Int, b: Int): Stream[Int] = {
a #:: fibFrom(b, a + b...
Tail recursion

Scala compiler can transform a recursive call into a
loop so that it won’t use stack for evaluation!
But i...
Tail recursion

Tail recursion is simple:
If the last evaluated expression in a function only
makes a recursive call, but ...
Tail recursion : example

def boom(x: Int): Int =
if (x == 0) throw new Exception("boom!")
else boom(x - 1) + 1
@tailrec
d...
Monads
Monad
Monad is a name of abstraction.
1.
2.
3.
4.

Chain function calls
“Container” type
Composable with each other
Wrap t...
Monad
You may have been working with monads already!
• Promises are monads!
• jQuery object is a monad!
• Futures (partial...
Monad
Every monad can:
• Wrap a value into a Monad type
• Unwrap a value from a Monad type
• Work transparently with wrapp...
Monads like Zombies
Every good zombie can:
• Turn a person into a zombie
• Turn back into a person if cured
• Communicate ...
Monads

Monad is a really tough topic for beginners!
Spend at least a week cranking the theory to create
you own vision of...
Option

Scala has a standard type named Option for optional
values.
One can say, that Option is a replacement for Nullobje...
Option

Option have two forms:
• Some(x)
• None
Scala collections do not return nulls,
they return None!
Option

The most common way to take optional values apart is
through a pattern match.
def show(x: Option[String]) = x matc...
Option
Option is a collection!
Well, it is actually a single-value container 
You can use map, filter, flallen, for-compr...
Option : Practices
• You should avoid null as much as possible in Scala
• Use Option when you can return something like a ...
Try

A monad for exception handling.
Its main purpose is to wrap the exception and pass it
around the code (e.g. to anothe...
Try

Try has two forms:
• Failure[Throwable]
• Success[T]
Try : Example

Try(Integer.parseInt("3")).getOrElse(println("Sorry, I failed"))
Try : Practices
• Use Try rather than a catch block for handling
unexpected failure.
• Use Try when working with Future.
•...
Underscore

Wildcard on steroids!
Underscore usages
Tons of them!
1. “Placeholder syntax”
List(1, 2, 3) map (_ + 2)

2. Partially applied functions
List(1, ...
Thanks!

val you = new Developer[Any] with ScalaKnowledge
Resources
• Scala Twitter School [link]
• Effective Scala [link]
• Scala for Java Programmers [link]
• The Neophyte’s Guid...
Scala Intro
Scala Intro
Scala Intro
Scala Intro
Scala Intro
Scala Intro
Scala Intro
Scala Intro
Scala Intro
Scala Intro
Scala Intro
Scala Intro
Scala Intro
Upcoming SlideShare
Loading in...5
×

Scala Intro

4,252

Published on

Scala Intro training @ Lohika, Odessa, UA.

This is a basic Scala Programming Language overview intended to evangelize the language among any-language programmers.

Published in: Technology
0 Comments
18 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,252
On Slideshare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
131
Comments
0
Likes
18
Embeds 0
No embeds

No notes for slide
  • Step 1 – 10 mins1. Scala history2. Complexity3. Use cases4. Who is using5. Use Java libs6. Multiparadigm language7. Prefer functional style8. Expressiveness9. Strongly statical typed10. Hello World
  • Design started in 2001First release – 2003Author - Martin Odersky1000 downloads/month on official site today
  • NB: the actions uponBigInt seems like actions over the built-in language features (types)* Scalable language – can be “scaled” by developer’s needs.* It suits great to write DSLs.
  • Step 2 – 15 mins1. What is immutability2. Why is it matter3. Difference between val / var (Example showing that we cannot re-asinval)4. Collections are immutable5. Mutable approaches allowed but not prefered6. Everything returns data (example with Java vsScala if statement)
  • ----- Meeting Notes (16.10.13 10:57) -----Add val and var examples
  • The second main idea of functional programming is that the operations of a program should map input values to output values rather than change data in place. Another way of stating this second idea of functional programming is that methods should not have any side effects.
  • ----- Meeting Notes (16.10.13 14:02) -----Add additional pointsAdd note to slide to see later
  • Promotes declarative functional style vs imperative style
  • Immutability =&gt; bad performance (memory)Objects everywhere =&gt; bad performance NOPE!Scalavsoptimezed C++ = (x2.5 - x3.6)Java vs opt C++ = (x3.7 – x5.8)
  • Step 3 – 25 mins1. Class = Java Class. Nothing special. Example. How constructor works.2. Object = singleton pattern (Example)3. Case class = class with convenient extensions (POJO in other words) (Example)4. Abstract class - do define structure5. Trait - to define behaviour. Stack modifications.6. Diamond problem solved
  • ----- Meeting Notes (16.10.13 10:57) -----Fiz &gt;&gt; to &quot;
  • Companion object pattern ?
  • What does “case” mean?Getters/setters + constructor + apply/unapply (pattern matching) + companion object
  • What does “case” mean?Getters/setters + constructor + apply/unapply (pattern matching) + companion object
  • ----- Meeting Notes (16.10.13 11:24) -----Add logger exampleAdd abstract class and trait exampleAdd #5 - Diamond problem solved slide
  • Step 4 – 10 minutes1. Scala basic types are objects (no primitives) 2. Scala implement own types3. Type list
  • + Literals+ Reside in scala package+ Imports automatic
  • Step 5 – 20mins1. Any is a top of hierarchy2. Basic types extends AnyVal3. Others extends AnyRef4. Any class extends Null implicitly5. Any class extends Nothing. Nothing is used in methods which returnds nothing (throw exception of terminate application)6. Unit = void----- Meeting Notes (16.10.13 11:24) -----Add User = null exampleadd Nothing example with throwLocalozedException
  • defthrowLocalizedException(key: String): Nothing = { throw LocalizedException(resolveMessage(key))}
  • Step 6 – 25 mins----- Meeting Notes (16.10.13 14:02) -----Add first class function example and slides
  • Note that it is &quot;idealogically&quot;Add right examples
  • ----- Meeting Notes (16.10.13 14:02) -----Add additional pointsAdd note to slide to see later
  • Step 7 – 15 min1. What is type inference2. Example with assigning variable3. Example with method return type4. Method parameters require types!Yes, it is logical. No, compiler cannot read your mind.
  • ----- Meeting Notes (16.10.13 14:02) -----Remove class wrapper context
  • ----- Meeting Notes (16.10.13 14:02) -----remove b:String - StringAdd Alex&apos;s examplesAdd example with if then else with
  • Lambda syntax
  • Step 8 – 30 minReplace with:2) Closure (definition, difference from lambda)3) Posible problems4) Closures like anonymous class in Java5) Full example
  • Step 9 – 60 min1. 2 Different types of collections. Mutable / Immutable. Show package names.2. Class Hierarchy (http://www.programmera.net/scala/img/seq_tree.png)3. 3 main traits (Map, Set, List) examples. (Immutable)Live codding. Show only main methods like map, flatMap, filter, foreach, reduce.Others will be shown in a table with a short description
  • Step 10 – 15 minsSimple examples with fors, filters, embededfors.Live coding?
  • Step 10 – 15 minsSimple examples with fors, filters, embededfors.Live coding?
  • Step 10 – 15 minsSimple examples with fors, filters, embededfors.Live coding?
  • Step 10 – 15 minsSimple examples with fors, filters, embededfors.Live coding?
  • Step 10 – 15 minsSimple examples with fors, filters, embededfors.Live coding?
  • Step 10 – 15 minsSimple examples with fors, filters, embededfors.Live coding?
  • Step 10 – 15 minsSimple examples with fors, filters, embededfors.Live coding?
  • ----- Meeting Notes (16.10.13 14:02) -----Add embedded for and filtersAdd for definitions
  • Step 12 – 30 mins1. Switch on steroids2. Exceptions example3. Case classes with pattern mathing + parameters extraction
  • Step 13 – 20 minsWhat are implicits2. Implicit variables3. Implicit class structure extension (pimp my library)vals, methods, prototyping
  • Step 14 – 10 mins+Tail recursionlazy evaluationlazy initializationOptionsMonadse.t.c in the end
  • Backticks to override reserved identifiers
  • Transcript of "Scala Intro"

    1. 1. Scala Intro val you = new Developer[Any] with ScalaKnowledge
    2. 2. Is Scala really hard? Can I start using it without going mad? Why zombies?
    3. 3. Scala History . Scala 2003 AD
    4. 4. • • • • • Pizza GJ generics in Java Funnel Scala
    5. 5. Scala is a pure object-oriented language Scala is also a functional language Strongly statically typed Runs on JVM, compiles to bytecode
    6. 6. Scala – scalable language def factorial(x: BigInt): BigInt = if (x == 0) 1 else x * factorial(x - 1)
    7. 7. In contrast with C++, the more you study Scala, the easier it gets. Unknown Scala community member
    8. 8. Scala in Production http://www.scala-lang.org/old/node/1658
    9. 9. Hello … object HelloZombie extends App { println("Hello, " + "Zombie!") }
    10. 10. Another Hello object HelloZombie { def main(args: Array[String]) { println("Hello, " + "Zombie!") } }
    11. 11. You do not need CS degree to code in Scala Application Programmers: A1 – A3 levels Library Designers: L1 – L3 levels «One can program very productively in Scala on level A1, which one should be able to pick up in a day or so, coming from Java. Mastering A2 will doubtlessly increase programmer productivity. A3 is for expert programmers with more specialized tasks, not everyone needs to get to that level.» http://www.scala-lang.org/old/node/8610.html
    12. 12. Scala functional basics
    13. 13. #1 - Mutability is evil #2 – Everyhting returns data #3 – Functions are first-class citizens
    14. 14. Mutability is evil • Mutable objects are complicated to think about • Defensive copies • Need to be synchronized in multithreaded env • Unobvious side effects
    15. 15. Mutability is evil val salary = "2000$" salary = "2500$" // does not compile var salary = "2000$” salary = "2500$” // compiles
    16. 16. Everything returns data val result = if (true) value1 else value2 val result = for (i <- 1 to 10) yield i def getRandom() = { Math.random() }
    17. 17. Functions are first-class citizens Assign function definitions to variables val doubled = (x:Int) => x * 2
    18. 18. in Scala a function value is an object! Scala => pure OOP & full-blown functional language
    19. 19. Sooo many objects around Baaaad performance, yeah?
    20. 20. http://readwrite.com/2011/06/06/cpp-go-java-scala-performance-benchmark
    21. 21. Scala OOP basics
    22. 22. #1 - Class #2 - Object = build-in singleton pattern #3 – Case class #4 – Abstract class vs Trait #5 – Multiple inheritance WAT? Diamond problem solved!
    23. 23. Class Class is similar to any other languages’ class notion class MyZombie { private val index: Int = 0 val name: String = "Zombie0" }
    24. 24. Class + Constructor class MyZombie(index: Int, val name: String) { println("me likes brainz!") }
    25. 25. Object Objects are singletons object LoneZombie { def growl = println("me sooo sad") }
    26. 26. Companion Object class CrawlerZombie(name: String) { def crawl() = println("crawling...") } object CrawlerZombie { def apply(name: String) = { println(name + " is here!") new CrawlerZombie(name) } } val zombie = CrawlerZombie("Mig") zombie.crawl()
    27. 27. Case Class Case class = class + “sugar” + compiler hint case class Zombie(index: Int, name : String)
    28. 28. Case Class Sugar 1. Properties 2. Pattern Matching 3. Companion Object
    29. 29. Abstract class vs Trait Abstract class defines structure Trait defines behaviour
    30. 30. Abstract Class abstract class Undead(name: String) { def eat() = { println("Om nom nom") digest() } def digest() } class Zombie(name: String) extends Undead(name) { def digest() = { println("grab brainz") } }
    31. 31. Trait abstract class Undead(name: String) { def eat() = { println("Om nom nom") digest() } def digest() } trait Growling{ def growl(msg: String) = println(msg) def scream(msg: String) = println(msg + " !!!") } class Zombie(name: String) extends Undead(name) with Growling { def digest() = { growl("grab brainz") } }
    32. 32. Scala Basic Types
    33. 33. #1 - Scala basic types are objects #2 – Scala implement it’s own types, wraps and extends some Java classes and types #3 Scala operators are method calls
    34. 34. Value type Range Byte 8-bit signed two's complement integer (27 to 27 - 1, inclusive) Short 16-bit signed two's complement integer (-215 to 215 - 1, inclusive) Int 32-bit signed two's complement integer (-231 to 231 - 1, inclusive) Long 64-bit signed two's complement integer (-263 to 263 - 1, inclusive) Char 16-bit unsigned Unicode character (0 to 216 - 1, inclusive) String a sequence of Chars Float 32-bit IEEE 754 single-precision float Double 64-bit IEEE 754 double-precision float Boolean true or false
    35. 35. Operators are methods Operators uses infix notation val sum = 1 + 2 // Scala invokes (1).+(2)
    36. 36. Class Hierarchy
    37. 37. Null ? var zombie: Zombie = null zombie = new Zombie("Alex")
    38. 38. Nothing? def throwLocalizedException(key: String): Nothing = { throw LocalizedException(resolveMessage(key)) }
    39. 39. Functions vs methods
    40. 40. Functions and methods are not the same!
    41. 41. ©
    42. 42. Functions No side effects Evaluates only body and parameters Compiles to FunctionN Defined mostly in Objects
    43. 43. Functions object KillerModule { def kill = (z: Zombie) => println("Killing " + z.name) }
    44. 44. Methods Defined in classes Works with class scope
    45. 45. Methods class ZombieTortureMachine(z: Zombie) { def kill() = println("Killing " + z.name) def starve() = println("Starving " + z.name) }
    46. 46. Functions are first-class citizens Assign function definitions to variables val doubled = (x:Int) => x * 2
    47. 47. Functions are first-class citizens Pass function as a parameter def traverseTree (callback: (Element) => Unit) { val element = ??? //Getting tree element callback(element) }
    48. 48. Functions are first-class citizens Return function as a result value def prepareCalculator(x: Int, y: Int): () => Int = { println("calculating...") () => x + y }
    49. 49. Type Inference
    50. 50. You can omit types in declarations! The compiler will infer them for you. Less typing -> less reading -> happier developers 
    51. 51. Type inference with variables private val index = 0 val name = "Zombie0" private val index: Int = 0 val name: String = "Zombie0"
    52. 52. Type inference with functions Scala compiler can not read thoughts  def func (a: Int, b: String) = { a + b } // compiles def func1 (a, b) = { a + b } // does not compile
    53. 53. Type inference def getZombies(severity: Int) = { if (severity > 10) { List() } else { List(Zombie("Mig"), Zombie("Alex")) } } // inferred type = List[Zombie]
    54. 54. Lambda Syntax val doubler = (n: Int) => n * 2 val yadobler = { n: Int => n * 2 }
    55. 55. Closures
    56. 56. Closure is special kind of a function Closure encloses the local lexical context inside the function body Closures can be implemented using Anonymous classes in Java (yuck!)
    57. 57. Simple Closure val multiplier = 2 val doubler = (n: Int) => n * multiplier
    58. 58. Closure vs Function Pure function calculates its result solely in terms of its arguments! Closure can use the outer lexical context for it computations. One may say that it stores “references” to the outer values.
    59. 59. Collections
    60. 60. Sugar : Map Initialization Java: Map<String, Integer> mappings = new HashMap<String, Integer>() {{ put("One", 1); put("Two", 2); put("Three", 3); }}; Scala: val mappings = Map( "one" -> 1, "two" -> 2, "three" -> 3 )
    61. 61. Sugar : Filtering Java: List<Integer> numbers = new ArrayList<Integer>(){{ add(1); add(2); add(-55); add(-33); add(122); }}; List<Integer> negativeNumbers = new ArrayList<Integer>(); for (Integer number : numbers) { if (number < 0) { negativeNumbers.add(number); } } Scala: val numbers = List(1, 2, -55, -33, 122) val negativeNumbers = numbers.filter(_ < 0)
    62. 62. Sugar : Classification Java: List<Integer> numbers = new ArrayList<Integer>(){{ add(1); add(2); add(-55); add(-33); add(122); }}; List<Integer> negativeNumbers = new ArrayList<Integer>(); List<Integer> positiveNumbers = new ArrayList<Integer>(); for (Integer number : numbers) { if (number < 0) { negativeNumbers.add(number); } else { positiveNumbers.add(number); } } Scala: val numbers = List(1, 2, -55, -33, 122) val (positiveNumbers, negativeNumbers) = numbers.span(_ > 0)
    63. 63. Stuff : Tuples • Tuples can be viewed as simple immutable collections. • Tuple can contain up to 22 elements of different types. • Very useful when you need to return a complex value from the expression
    64. 64. Stuff : Tuples val pair = (22, "zombies") val pair = (22 -> "zombies") // the type is Tuple2[Int, String] println(pair._1) println(pair._2)
    65. 65. Mutable vs Immutable Prefer immutable collections by default. Period. Google for details. Scala uses immutable collections by default val map = Map("one" -> 1) //results in scala.collection.immutable.Map[String,Int]
    66. 66. Collections practices Map, Set and List are mostly used collections in Scala Use traits’ companion object calls to create collections when you do not need specific implementation: Map("one" -> 1) // good HashMap("one" -> 1) // not so good new HashMap("one" -> 1) // won't compile :)
    67. 67. Collections API example #1 case class User(name: String, password: String) val users = List("admin:nimda", "user1:asddsa", "root:qwerty") val mappedUsers = users.map { user => val splitted = user.split(":") User(splitted(0), splitted(1)) } // List[User] = List(User(admin,nimda), User(user1,asddsa), User(root,qwerty))
    68. 68. Collections API example #2 val names = List( "Alex,Viktor,Eugeny", "Dmitry,Yegor, Sergey", "Michael,Sergey") val splitted = names.flatMap(_.split(",").toList).distinct // List(Alex, Viktor, Eugeny, Dmitry, Yegor, Michael, Sergey)
    69. 69. Loops & For comprehension
    70. 70. Loops & For comprehension • Scala has a while loop • Scala has a do-while loop • They are not expressions (the return type is Unit)
    71. 71. Loops & For comprehension Because the while loop results in no value, it is often left out of pure functional languages. Such languages have expressions, not loops. Scala includes the while loop nonetheless, because sometimes an imperative solution can be more readable, especially to programmers with a predominantly imperative background.
    72. 72. While loop while (brains.size > 0) { zombie eat brains }
    73. 73. Do-While loop do { zombie eat brains } while (brains.size > 0)
    74. 74. Friendly advise Try use recursion instead of loops! And don’t feed zombies
    75. 75. For expression
    76. 76. For expression • Looks like a for-loop • Behaves as a swiss knife 
    77. 77. For + generator for (zombie <- zombieHorde) kill(zombie)
    78. 78. For + generator + filter for (zombie <- zombieHorde if zombie.isMoving) kill(zombie)
    79. 79. For + generator + filter + yield This will produce a new collection! val killedZombies = for (zombie <- zombieHorde if !zombie.isMoving) yield zombie
    80. 80. For expression Can be used instead of map, filter and flatMap combination
    81. 81. Pattern Matching
    82. 82. Simple matching val status: String = "R” val translated = status match { case "R" => "running” case "D" => "digesting” case "E" => "eating brainz” case _ => "X3" }
    83. 83. Structure matching case class User(name: String, lastname: String, age: Int) val users = List( User("Alexey", "Migutsky", 25), User("Alexander", "Albul", 27), User("John", "Doe", 99) ) val determined = users.map { case User("Alexey", _, _) => "L” case User("Alexander", _, _) => "A” case _ => "X3" }.mkString(",") // L,A,X3
    84. 84. Structure matching def describe(list: List[Int]) { list match { case first :: second :: third :: tail => println("First case") case head :: tail => println("Second case") case Nil => println("Empty list") } } describe(List(1,2,3)) // First case describe(List(1, 2)) // Second case describe(List()) // Empty list
    85. 85. Exception handling try { //something } catch { case e: IllegalArgumentException => println("Illegal argument") case _ => println("Unknown exception") }
    86. 86. Implicits
    87. 87. Implicits The compiler can insert parameters and call conversion methods automatically based on the types used This behavior can be achieved using the implicit modifier
    88. 88. #1 - Implicit parameters #2 – Implicit type conversion #3 – “Pimp my Library”
    89. 89. Implicit parameters case class Context(data: List[String]) implicit val context = Context(List("a", "b", "c")) object SomeService { def printCtx(implicit ctx: Context) = println(ctx.data.mkString(",")) } SomeService.printCtx // a,b,c
    90. 90. Implicit type conversion Compiler will use the implicit conversion method automatically! case class User(first: String, last: String) def printUserInfo(user: User) { println("User name is: " + user.first + ", last name is: " + user.last) } implicit def stringToUser(userString: String): User = { val split = userString.split(" ") User(split(0), split(1)) } printUserInfo("Alexander Albul")
    91. 91. Pimp My Library case class User(first: String, last: String) implicit class ExtendedString(str: String) { def toUser: User = { val split = str.split(" ") User(split(0), split(1)) } } def printUserInfo(user: User) { println("User name is: " + user.first + ", last name is: " + user.last) } printUserInfo("Alexander Albul".toUser)
    92. 92. Moar Sugar! For those who survived
    93. 93. Stuff : Object equality Use == to compare anything 1 == 1.0 // true List(1,2,3) == List(1,2,3) // true null == List(1,2,3) // false List(1,2) == "string" // false
    94. 94. Stuff : Object equality The equality operator == do the following 1. Check the left side for null 2. If left side is not null, then call equals method Scala provides a facility for comparing reference equality, as well, under the name eq. However, eq and its opposite, ne, only apply to objects that directly map to Java objects.
    95. 95. Stuff : Packages and Imports • Scala code resides in the Java platform’s global hierarchy of packages. • Package definition is at the top of the file. • The structure is reflected on file system. package graveyard class Zombie
    96. 96. Stuff : Packages and Imports • The other way you can place code into packages in Scala is more like C# namespaces (called packaging) • Packages can be nested package graveyard { package gravestone { class Zombie } }
    97. 97. Stuff : Packages and Imports In Scala, packages and their members can be imported using import clauses. Imported items can then be accessed by a simple name like Zombie , as opposed to requiring a qualified name like graveyard.gravestone.Zombie. // import Zombie import graveyard.gravestone.Zombie // import all undead horde import graveyard._
    98. 98. Stuff : Packages and Imports • Imports may appear anywhere • Import may refer to objects (singleton or regular) in addition to packages • Import let you rename and hide some of the imported members // please don't do this IRL: aliases must be reasonable! import graveyard.gravestone.{Zombie => RoseBush} def hideUndead(): RoseBush = { import graveyard.gravestone.{Zombie => OldMan} new OldMan() }
    99. 99. Stuff : Identifiers 1. CamelCase 2. Alphanumeric for variables – letters + digits 3. Do not use starting $ - it is reserved for compiler variables 4. Do not use _ in names – underscore has other usages 5. Constants has first uppercase letter – math.Pi
    100. 100. Stuff : Method call Method with 0 parameter can be called without () Method with 1 parameter can be called using infix notation new Test().method // method call new Test().function // res1: () => Unit = <function0> new Test() method1 10 // 13
    101. 101. Stuff : Method call Use higher-order functions with infix notation, but not with mixed notation List("abcd","zyxwvu").map (_.toUpperCase).filter (_.length > 5) // bad! List("abcd","zyxwvu") map (_.toUpperCase) filter (_.length > 5) // good
    102. 102. Stuff : Method calls Use infix notation only with methods without side effects (which do not modify internal class state) List(1,2) mkString "" // ok new CustomDataset() add "some value" // not ok
    103. 103. Lazy evaluation An expression that has a value, but that is not evaluated until it's actually needed (in another words – until the value is actually read).
    104. 104. Call by-name parameter Lazy evaluated parameter def callByValue(x: Int) = { //parameter get evaluated upon function call println("x1=" + x) println("x2=" + x) } def callByName(x: => Int) = { println("x1=" + x) // parameter get evaluated here println("x2=" + x) // parameter get evaluated again }
    105. 105. Lazy initialization When a val is declared with the lazy modifier the righthand side of the value (the definition) will not be executed until the first time the value is accessed. val normal = 5 // evaluated immediately lazy val lzy = 5 // evaluated only upon first read
    106. 106. Stuff : Streams A stream is like a list except that its elements are computed lazily. Because of this, a stream can be infinitely long. Only those elements requested will be computed. Otherwise, streams have the same performance characteristics as lists.
    107. 107. Stuff : Streams Classical example: Fibonacci sequence def fibFrom(a: Int, b: Int): Stream[Int] = { a #:: fibFrom(b, a + b) }
    108. 108. Tail recursion Scala compiler can transform a recursive call into a loop so that it won’t use stack for evaluation! But it can apply its magic only in case of tail recursion.
    109. 109. Tail recursion Tail recursion is simple: If the last evaluated expression in a function only makes a recursive call, but does not make additional computations, than the function is tail recursive!
    110. 110. Tail recursion : example def boom(x: Int): Int = if (x == 0) throw new Exception("boom!") else boom(x - 1) + 1 @tailrec def bang(x: Int): Int = if (x == 0) throw new Exception("bang!") else bang(x - 1)
    111. 111. Monads
    112. 112. Monad Monad is a name of abstraction. 1. 2. 3. 4. Chain function calls “Container” type Composable with each other Wrap types, which are stored in the monad, into a monad type (like zombies – their bites turn everyone in a zombie)
    113. 113. Monad You may have been working with monads already! • Promises are monads! • jQuery object is a monad! • Futures (partial monadic API)
    114. 114. Monad Every monad can: • Wrap a value into a Monad type • Unwrap a value from a Monad type • Work transparently with wrapped value using Monad interface
    115. 115. Monads like Zombies Every good zombie can: • Turn a person into a zombie • Turn back into a person if cured • Communicate with other zombies as if those were people
    116. 116. Monads Monad is a really tough topic for beginners! Spend at least a week cranking the theory to create you own vision of this abstraction!
    117. 117. Option Scala has a standard type named Option for optional values. One can say, that Option is a replacement for Nullobject pattern. It eliminates the null-checks in the code! It eliminates Null-Pointer Exceptions!
    118. 118. Option Option have two forms: • Some(x) • None Scala collections do not return nulls, they return None!
    119. 119. Option The most common way to take optional values apart is through a pattern match. def show(x: Option[String]) = x match { case Some(s) => println(s) case None => println("?") }
    120. 120. Option Option is a collection! Well, it is actually a single-value container  You can use map, filter, flallen, for-comprehension and other collection API methods with Option! http://danielwestheide.com/blog/2012/12/19/the-neophytes-guide-to-scala-part-5-the-option-type.html http://blog.tmorris.net/scalaoption-cheat-sheet/
    121. 121. Option : Practices • You should avoid null as much as possible in Scala • Use Option when you can return something like a “no value” • Do not use null-checks in scala, use collection API/pattern matching over Option
    122. 122. Try A monad for exception handling. Its main purpose is to wrap the exception and pass it around the code (e.g. to another execution context) Was developed by Twitter team to pass exceptions across cluster nodes.
    123. 123. Try Try has two forms: • Failure[Throwable] • Success[T]
    124. 124. Try : Example Try(Integer.parseInt("3")).getOrElse(println("Sorry, I failed"))
    125. 125. Try : Practices • Use Try rather than a catch block for handling unexpected failure. • Use Try when working with Future. • Exposing Try in a public API has a similiar effect as a checked exception. Consider using exceptions instead.
    126. 126. Underscore Wildcard on steroids!
    127. 127. Underscore usages Tons of them! 1. “Placeholder syntax” List(1, 2, 3) map (_ + 2) 2. Partially applied functions List(1, 2, 3) foreach println _ 3. Wildcard pattern Some(5) match { case Some(_) => println("Yes") } 4. Wildcard imports import java.util._ http://stackoverflow.com/questions/8000903/what-are-all-the-uses-of-an-underscore-in-scala
    128. 128. Thanks! val you = new Developer[Any] with ScalaKnowledge
    129. 129. Resources • Scala Twitter School [link] • Effective Scala [link] • Scala for Java Programmers [link] • The Neophyte’s Guide to Scala [link] [book] • Functional Programming Principles in Scala @ Coursera [link] • Programming in Scala [book] • ScalaUA skype chat [link]
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×