Scala the-good-parts

Post on 12-Feb-2017

914 views 1 download

Transcript of Scala the-good-parts

Scala The Good Parts

@ http://afoo.me

also the Simple Parts

The Good Parts

Of The

Language

–the guys who came from Java to Scala said

“ (I will never turn back to Java

any more)”

farewell, semi-colon

Type Inference

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

Scala

Go

Swift

u name it…

expression everywhere

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!

Combinator &

Composition

Flexible Parameters

But not friendly to Javaers

case class

How you do it in Java Way?

IDEs can help

Library can helpCommons Lang

Google Guava

Lombok

Let’s do it in Scala Way!

tuples

static Companion object

special `apply` in companion object

match

everything and everywhere

Pattern Matching Takes Them All

extractor object

unapply : Input => Optional[Output]

case class is lucky

monad and container objects

monad and container objectsyou have met Option, right?

But it’s more than Some or None!

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

import scala.collection._Goand Explore…

function Rocks

Function literals A=>B

Partial Applied functions

Are NOT Partial Functions

Partial Applied Functions

Partial Functions

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)

Curried Functions

Caution! Dark Magic

high-order FunctionsTry Function

as return value

How about Java8?

Both Cool~ as long as you like

The Good Parts

Of The

Ecosystem

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

The JVM Family

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

Inter-communication

Libraries

Frameworks

Tools

SBT makes build-life easier.

Triggered Execution

Wonderful Plugins

SBT Native Packager Plugin

sbt-assembly

sbt-dependency-graph

you favoured IDEs’ Plugins

Concurrency in another way

SIP14 Rocks - Futures and Promises

Composable !

SIP14 Rocks - Futures and Promises

Actor Model

Don’t be silly

A Best Practice Of

Message Passing Concurrency

Hello ActorGravityu can’t escape

IoC Wisely

Taming Actor

Throttling

Hibernating

Journaling

Clustering

web another way

URL-Oriented design

route - the central hub

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)

Perfect For Web API Scenarios

Breaking out of Web Containers

make a killing with big data

do you know

DryadLINQMicrosoft does NOT always

suck

from single framework to family

Memory Computing enhanced with

neat DSL

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()

Micro-batching

is not so realtime

Immediately VS. Wait a moment

– who said that?!

“Where the fucking awesome type system of scala?”

Scala wisely

扶墙老师はpuristない

Find your own toolbox not ONLY a

Hammer

海纳百Lang 有容乃大

Java Scala ^_-

string interpolation wisely

@see also org.slf4j.helpers.MessageFormatter

About Implicit

1. 如果有普遍的共识

和约定的, 可以使用;

2. 否则,不建议使用!

Annotation wisely

No Perfect Annotation Solution

Ugly Thing

macross

enumeration wisely

or sealed trait?

scala the

endpoints instead of

the Dependencies.

Standalone

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

Questions?

over