+ All Categories
Home > Documents > Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for...

Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for...

Date post: 22-May-2020
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
33
Enthusiasts BS Scala Philipp Wille Beyond Scala‘s Standard Library
Transcript
Page 1: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Enthusiasts BSScalaPhilipp Wille

Beyond Scala‘s Standard Library

Page 2: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

OO or Functional Programming?

• Martin Odersky:

“Systems should be composedfrom modules. Modules shouldbe simple parts that can becombined in many ways to giveinteresting results.”

About Scala: The Simple Parts

2

Page 3: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Modular Programming• atomic module

object Greeting {

def apply: String = "Hello"

}

• templates to create modules

class Greet(name: String) {

def apply: String = "Hello "+name+"!"

}

object Greeting extends Greet("Martin")

3

Page 4: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Modular Programming• atomic module

val Greeting: () => String = {

() => "Hello"

}

• templates to create modules

val Greet: String => () => String = {

case name => () => "Hello "+name+"!"

}

val Greeting: () => String = Greet("Martin")

4

Page 5: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Modular Programming• mixable slices of behavior

trait Politeness {

val bePolite = " How are you today?"

}

class Greet(name: String) extends Politeness {

def apply = "Hello "+name+"!"+bePolite

}

val greeting = new Greet("Martin")

5

Page 6: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Modular Programming

• Martin Odersky:

“Modular Programming is puttingthe focus on how modules canbe combined, not so much whatthey do.”

About Scala: The Simple Parts

• in this talk we will focus on modules

6

Page 7: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Scalaz

• “Scalaz is a Scala library for functional

programming. It provides purely func-

tional data structures to complement

those from the Scala Standard Library.”http://github.com/scalaz/scalaz

• provided modules: i.a. data structures and

methods

7

Page 8: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Scalaz Memos• speed up function calls by memoization

• think: caching

• example:

val fibonacci: Int => Int = {

case 0 => 0

case 1 => 1

case n => fibonacci(n - 2) + fibonacci(n - 1)

}

• problem: recomputation of fibonacci values

8

Page 9: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Scalaz Memos• speed up function calls by memoization

• think: caching

• solution:

val fibonacci: Int => Int = Memo.mutableHashMapMemo {

case 0 => 0

case 1 => 1

case n => fibonacci(n - 2) + fibonacci(n - 1)

}

• once a value is computed it is cached in a mutable HashMap and will be reused

9

Page 10: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Scalaz Ordering• monadic way for defining orderings for types

• defines types LT, GT, and EQ

• defines functions ?|?, lt, gt, lte, gte, min, and max

1.0 ?|? 2.0 // scalaz.Ordering = LT

1.0 gt 2.0 // Boolean = false

def compare(a: String, b: String): Ordering =

(a.length ?|? b.length) |+| (a ?|? b)

compare("viktor", "martins") // scalaz.Ordering = LT

compare("viktor", "martin") // scalaz.Ordering = GT

10

Page 11: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Scalaz – additional features

• Type classes• Functor• Applicative• Monad• Zipper• Lenses• Free Monad• State Monad

11

Page 12: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Shapeless

• “Shapeless is a type class and

dependent type based generic

programming library for Scala.”http://github.com/milessabin/shapeless

• provided modules: i.a. data structures and

additional methods for Standard Library types

12

Page 13: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Shapeless• Scala’s Standard Library does not provide any

collection methods for tuples (for a reason)

• Shapeless adds support for them

val tuple = ("dog", true)

tuple.head // String = dog

tuple.drop(1) // (Boolean,) = (true,)

tuple.split(1) // ((String,), (Boolean,)) = ((dog,), (true,))

23 +: tuple // (Int, String, Boolean) = (23, dog, true)

13

Page 14: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

• Scala’s Standard Library does not support polymorphic function values

• Shapeless adds them

val tuple = ("dog", true)

object AsList extends (Id ~> List) {

def apply[A](a: A) = List(a)

}

tuple.map(elem => AsList(elem))

// (List[String], List[Boolean]) = (List(dog), List(true))

Shapeless

14

Page 15: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

• type specific polymorphic function

values

• heterogenous lists (including map

over polymorphic function values)

• generic representation of case

classes

Shapeless – additional features

15

Page 16: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Akka

• “Akka is a toolkit and runtime

for building highly concurrent,

distributed, and fault tolerant

event-driven applications on

the JVM.”http://akka.io

• provided modules: i.a. actors, data structures

16

Page 17: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Akka Actors• concurrent entities (think: each actor runs

in its own thread)

• send and receive messages (think: they

act like mailboxes)

• they have a message queue and process

one message at a time

• difficulties: unordered arrivals and no

guaranteed delivery

17

Page 18: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Sample Actor Model

18

Page 19: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Actor Model

class Lookup extends Actor {

val data = Map("Martin" -> "Odersky")

def receive = {

case Key(key) =>

val value = data.getOrElse(key, "")

sender() ! Value(value)

}

}

19

Page 20: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Actor Modelclass Master extends Actor {

override def preStart() {

val viktor = context.actorOf(Props[Lookup], "lookup-viktor")

val martin = context.actorOf(Props[Lookup], "lookup-martin")

viktor ! Key("Viktor")

martin ! Key("Martin")

}

val results = ListBuffer[String]()

def receive = {

case Value(value) => results += value

}

}

20

Page 21: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Actor Model – Local

• deploying the actors on a local machine

val local = ConfigFactory.load("local")

val system = ActorSystem("Master", local)

system.actorOf(Props[Master], "master")

21

Page 22: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Actor Model – Remote

• deploying the actors on a remote machine

val master = ConfigFactory.load("remote-master")

val system = ActorSystem("Master", master)

system.actorOf(Props[Master], "master")

val worker = ConfigFactory.load("remote-worker")

ActorSystem("Worker", worker)

22

Page 23: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Actor Model – Remote

• configuring the remote machine’s address

akka.actor.deployment {

"/master/*" {

remote = "akka.tcp://[email protected]:13371"

}

}

23

Page 24: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Actor Model – Cluster• deploying the actors in a cluster

val master = ConfigFactory.load("cluster-master")

val system = ActorSystem("Master", master)

Cluster(system).registerOnMemberUp {

system.actorOf(Props[Master], "master")

}

val worker = ConfigFactory.load("cluster-worker")

val system = ActorSystem("Worker", worker)

system.actorOf(Props[Lookup], "lookup")

24

Page 25: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Actor Model – Cluster• configuring the cluster

akka.actor.deployment {"/master/*" {router = adaptive-groupmetrics-selector = mixroutes.paths = ["/user/lookup"]cluster {enabled = onuse-role = worker

}}}

25

Page 26: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Akka – additional features

• Akka Persistence

• Akka Http (former Spray.io)

• Akka Futures (now in Scala‘s

Standard Library)

• Akka Streams

• Akka Finite State Machine

26

Page 27: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Sbt (Scala/Simple Build Tool)

• interactive build tool

• compiles both, Scala and Java

• uses Maven dependencies

• compile, run, package your code

• multi-project builds

27

Page 28: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Sample build.sbt definitionname := "root"

version := "1.0"

scalaVersion := "2.11.1"

libraryDependencies ++= Seq(

"com.typesafe.akka" % "akka-actor_2.11" % "2.3.4"

)

lazy val hello = ProjectRef(file("../hello"), "hello")

lazy val world = ProjectRef(file("../world"), "world")

lazy val root = project.in(file(".")).dependsOn(hello, world)

28

Page 29: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Sbt – additional features

• user defined tasks

• huge number of plugins

(e.g. assembly)

• interactive Scala console

• code deployment

• cross Scala versions building

29

Page 30: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Conclusions• Modular Programming means thinking

about software design first• different types of modules• different ways to combine them• multiple ways of solving a problem

• Modular Programming will make you abetter software engineer• the more modules you know the better• the more combinations you know the better• scale your knowledge by scaling your language

30

Page 31: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Thank you for your attention

Questions?

31

Page 32: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

Pickling

• automatic serialization framework• works out-of-the-box

• no need for implementing interfaces/traits

• typesafety• compile-time errors for serialization

• uses Scala Macros

• runtime errors for deserialization

32

Page 33: Scala Enthusiasts BSscala-bs.de/slides/Metting-1-Beyond-Scalas-Standard... · 2015-06-11 · for building highly concurrent, distributed, ... •Akka Futures (now in Scala‘s Standard

import scala.pickling._import json._

val pckl = List(1, 2, 3, 4).pickleval list = pckl.unpickle[List[Int]]

case class Cloud(shape: String)case class Sky(clouds: Set[Cloud])val pckl = Sky(Set(Cloud("Dog"), Cloud("Banana"))).pickleval sky = pckl.unpickle[Sky]

{ "tpe": "Sky","clouds": {

"tpe": "Set[Cloud]","elems": [{ "tpe": "Cloud",

"shape": "Dog" },{ "tpe": "Cloud",

"shape": "Banana" }] }}

Pickling

33

{ "tpe": "List[Int]","elems": [1, 2, 3, 4] }


Recommended