scala the-good-parts

77
Scala The Good Parts @ ಡाᘌ http://afoo.me

Upload: fuqiang-wang

Post on 12-Feb-2017

916 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Scala the-good-parts

Scala The Good Parts

@ http://afoo.me

Page 2: Scala the-good-parts

also the Simple Parts

Page 3: Scala the-good-parts

The Good Parts

Of The

Language

Page 4: Scala the-good-parts

–the guys who came from Java to Scala said

“ (I will never turn back to Java

any more)”

Page 5: Scala the-good-parts

farewell, semi-colon

Page 6: Scala the-good-parts

Type Inference

Page 7: Scala the-good-parts

类型后置+类型推导的时代

Scala

Go

Swift

u name it…

Page 8: Scala the-good-parts

expression everywhere

Page 9: Scala the-good-parts

var retVal = for{ a <- numList if a != 3; if a < 8 }yield a

for( a <- retVal){

println( "Value of a: " + a ); }

val delimiter = if(defaultDelimiter)’;’ else ‘-’;

No return any more!

Page 10: Scala the-good-parts

Combinator &

Composition

Page 11: Scala the-good-parts

Flexible Parameters

But not friendly to Javaers

Page 12: Scala the-good-parts

case class

Page 13: Scala the-good-parts

How you do it in Java Way?

Page 14: Scala the-good-parts

IDEs can help

Page 15: Scala the-good-parts

Library can helpCommons Lang

Google Guava

Lombok

Page 16: Scala the-good-parts

Let’s do it in Scala Way!

Page 17: Scala the-good-parts

tuples

Page 18: Scala the-good-parts

static Companion object

Page 19: Scala the-good-parts

special `apply` in companion object

Page 20: Scala the-good-parts

match

everything and everywhere

Page 21: Scala the-good-parts

Pattern Matching Takes Them All

Page 22: Scala the-good-parts

extractor object

unapply : Input => Optional[Output]

case class is lucky

Page 23: Scala the-good-parts

monad and container objects

Page 24: Scala the-good-parts

monad and container objectsyou have met Option, right?

But it’s more than Some or None!

Page 25: Scala the-good-parts

Monad is too academic, let’s know it in an easy way

import scala.collection._Goand Explore…

Page 26: Scala the-good-parts

function Rocks

Page 27: Scala the-good-parts

Function literals A=>B

Page 28: Scala the-good-parts

Partial Applied functions

Are NOT Partial Functions

Page 29: Scala the-good-parts

Partial Applied Functions

Page 30: Scala the-good-parts

Partial Functions

Page 31: Scala the-good-parts

Partial Function Tip

scala> val m = Map(1->2, 2->3) m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 2 -> 3)

scala> m.collect({case (k, v) if k % 2 ==0 => (k,v) }) res7: scala.collection.immutable.Map[Int,Int] = Map(2 -> 3)

scala> m collect {case (k, v) if k % 2 ==0 => (k,v) } res7: scala.collection.immutable.Map[Int,Int] = Map(2 -> 3)

Page 32: Scala the-good-parts

Curried Functions

Caution! Dark Magic

Page 33: Scala the-good-parts

high-order FunctionsTry Function

as return value

Page 34: Scala the-good-parts

How about Java8?

Both Cool~ as long as you like

Page 35: Scala the-good-parts

The Good Parts

Of The

Ecosystem

Page 36: Scala the-good-parts

REPL快速验证你的疑惑和想法

Page 37: Scala the-good-parts

The JVM Family

Page 38: Scala the-good-parts

What comes into your mind when u hear the Java word?

Page 39: Scala the-good-parts

Inter-communication

Libraries

Frameworks

Tools

Page 40: Scala the-good-parts

SBT makes build-life easier.

Page 41: Scala the-good-parts

Triggered Execution

Page 42: Scala the-good-parts

Wonderful Plugins

SBT Native Packager Plugin

sbt-assembly

sbt-dependency-graph

you favoured IDEs’ Plugins

Page 43: Scala the-good-parts

Concurrency in another way

Page 44: Scala the-good-parts

SIP14 Rocks - Futures and Promises

Composable !

Page 45: Scala the-good-parts

SIP14 Rocks - Futures and Promises

Page 46: Scala the-good-parts

Actor Model

Don’t be silly

Page 47: Scala the-good-parts

A Best Practice Of

Message Passing Concurrency

Page 48: Scala the-good-parts

Hello ActorGravityu can’t escape

Page 49: Scala the-good-parts

IoC Wisely

Page 50: Scala the-good-parts

Taming Actor

Throttling

Hibernating

Journaling

Clustering

Page 51: Scala the-good-parts

web another way

Page 52: Scala the-good-parts

URL-Oriented design

Page 53: Scala the-good-parts

route - the central hub

Page 54: Scala the-good-parts

what a route looks like

[Http Method] [path] [controller method]

GET / controllers.Application.main POST / controllers.Application.index

# Map static resources from the /public folder to the /assets URL path GET /assets/*file controllers.Assets.at(path="/public", file) GET /webjars/*file controllers.WebJarAssets.at(file)

Page 55: Scala the-good-parts

Perfect For Web API Scenarios

Page 56: Scala the-good-parts

Breaking out of Web Containers

Page 57: Scala the-good-parts

make a killing with big data

Page 58: Scala the-good-parts

do you know

DryadLINQMicrosoft does NOT always

suck

Page 59: Scala the-good-parts

from single framework to family

Page 60: Scala the-good-parts

Memory Computing enhanced with

neat DSL

Page 61: Scala the-good-parts

You know Scala Collections well, you

play with Spark well ;)

val textFile = spark.textFile("hdfs://...") val errors = textFile.filter(line => line.contains("ERROR")) errors.count() errors.filter(line => line.contains("MySQL")).count() errors.filter(line => line.contains("MySQL")).collect()

Page 62: Scala the-good-parts

Micro-batching

is not so realtime

Page 63: Scala the-good-parts

Immediately VS. Wait a moment

Page 64: Scala the-good-parts

– who said that?!

“Where the fucking awesome type system of scala?”

Page 65: Scala the-good-parts

Scala wisely

Page 66: Scala the-good-parts

扶墙老师はpuristない

Page 67: Scala the-good-parts

Find your own toolbox not ONLY a

Hammer

Page 68: Scala the-good-parts

海纳百Lang 有容乃大

Java Scala ^_-

Page 69: Scala the-good-parts

string interpolation wisely

@see also org.slf4j.helpers.MessageFormatter

Page 70: Scala the-good-parts

About Implicit

1. 如果有普遍的共识

和约定的, 可以使用;

2. 否则,不建议使用!

Page 71: Scala the-good-parts

Annotation wisely

Page 72: Scala the-good-parts

No Perfect Annotation Solution

Ugly Thing

macross

Page 73: Scala the-good-parts

enumeration wisely

or sealed trait?

Page 74: Scala the-good-parts

scala the

endpoints instead of

the Dependencies.

Standalone

Page 75: Scala the-good-parts

http://afoo.me/posts/2015-03-24-java-scala-project-type-choice-principle.html

Page 76: Scala the-good-parts

Questions?

Page 77: Scala the-good-parts

over