SCALA 知多少
2
▫︎A. 仅仅听过该名词,或更少
▫︎ B. 了解过基本语法,有 Hello World 经验
▫︎C. 了解⽐比较多,有项⺫⽬目经验
SCALA 起源
3
▫︎出⽣生⽇日期:1958 - 09 - 05
▫︎国籍:德国 ▫︎⺟母校:苏黎世联邦理⼯工学院 ▫︎公司:Typesafe Inc.
▫︎成就:Java 泛型、Scala、MOOC
Martin Odersky
SCALA ⼤大事记
4
▫︎ 2001年,Scala 的设计在 EPFL 开始;
▫︎ 2004年初,Java 版发布;
▫︎ 2004年6⽉月,.NET 版发布;
▫︎ 2006年3⽉月,Scala 2.0 Java 版发布;
▫︎ 2011年5⽉月,Odersky和Jonas Bonér 创办 Typesafe;
▫︎ 2012年,官⽅方停⽌止维护 Scala .NET 版;
▫︎ 2014年,Scala 2.11.2 发布。
YOU DON’T KNOW WHAT YOU DON’T KNOWAs a developer, 最可怕的事情是什么?
5
AWESOME SCALA
6
BUILD TOOL - MAVEN
7
<repository> <id>scala-tools.org</id> <name>Scala-tools Maven2 Repository</name> <url>http://scala-tools.org/repo-releases</url> </repository> <pluginRepository> <id>scala-tools.org</id> <name>Scala-tools Maven2 Repository</name> <url>http://scala-tools.org/repo-releases</url> </pluginRepository> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.11.1</version> </dependency> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin>
BUILD TOOL - GRADLE
8
apply plugin: 'scala'repositories { mavenCentral()} dependencies { compile 'org.scala-lang:scala-library:2.11.1'}
BUILD TOOL - SBT
9
name := "hello world"version := "0.0.1"scalaVersion := "2.11.1"resolvers ++= Seq ( Resolver.mavenLocal, Resolver.sonatypeRepo ("releases"), Resolver.typesafeRepo ("releases") ) libraryDependencies ++=Seq ("org.scala-lang" % "scala-compiler" % "2.11.1") addSbtPlugin ("com.typesafe.play" % "sbt-plugin" % "2.3.1")
http://www.scala-sbt.org/
DB - SCALIKEJDBC
10
PostgreSQL、MySQL、H2、HSQLDB、Oracle、SQL Server… …
val orders: List[Order] = withSQL { select .from(Order as o) .innerJoin(Product as p).on(o.productId, p.id) .leftJoin(Account as a).on(o.accountId, a.id) .where.eq(o.productId, 123) .orderBy(o.id).desc .limit(4) .offset(0) }.map(Order(o, p, a)).list.apply()
SELECT o.*, p.*, a.*FROM orders o INNER JOIN products p ON o.product_id = p.id LEFT JOIN accounts a ON o.account_id = a.id WHERE o.product_id = 123ORDER BY o.id DESCLIMIT 4 OFFSET 0
DB - SQUERYL
11
Postgres、Oracle、MySQL、H2、DB2、SQL Server、Derby
def songCountByArtistId: Query[GroupWithMeasures[Long, Long]] = from(artists, songs)((a, s) => where(a.id === s.artistId) groupBy (a.id) compute (countDistinct(s.id)) )
SELECT Artist1.id AS g0, count(DISTINCT Song2.id) AS c0 FROM Artist Artist1, Song Song2WHERE (Artist1.id = Song2.artistId)GROUP BY Artist1.id
DB - SLICK
12
Derby、H2、HSQLDB、Access、MySQL、PostgreSQL、SQLite、Oracle、DB2 、SQL Server
val q = coffees.filter(_.price > 20) .sortBy(_.name.desc) .drop(10) .take(5)
SELECT * FROM coffeesWHERE price > 20 ORDER BY name DESCLIMIT 5 OFFSET 10
WEB - PLAY
13
GET /hello/:username HelloController.say(username: String)
import play.api.mvc.{Action, Controller}object HelloController extends Controller { def say(username: String) = Action { Ok(s"hello, $username") }}
route
controller
WEB - *ATRA
14
WEB - LIFT
15
View First
TEST - SCALACHECK
16
object StringSpec extends Properties("String") { property("startWith") = forAll { (a: String, b: String) => (a + b).startsWith(a) } property("concatenate") = forAll { (a: String, b: String) => (a + b).length > a.length && (a + b).length > b.length }}
+ String.startWith: OK, passed 100 tests.! String.concat: Falsified after 0 passed tests.> ARG_0: ""> ARG_1: ""!
Test
Result
TEST - SPEC2
17
import org.specs2.mutable._class HelloWorldSpec extends Specification { "The 'Hello world' string" should { "contain 11 characters" in { "Hello world" must have size(11) } "start with 'Hello'" in { "Hello world" must startWith("Hello") } "end with 'world'" in { "Hello world" must endWith("world") } }}
Test
TEST - GATLING
18
class BasicSimulation extends Simulation { val httpConf = http.baseURL("http://tax.myhpsnet.com:8800") val scn = scenario("BasicSimulation") .exec(http("get an invoice") .get("/api/invoices/d361bb03-e762-3842-b6ae-a3062d1e5cf7")) .pause(1) setUp(scn.inject(atOnceUsers(100)).protocols(httpConf)) }
DISTRIBUTED SYSTEM
19
BIG DATA
20
100x faster than Hadoop MapReduce in memory
10x faster on disk.
REFERENCES
21
1. http://en.wikipedia.org/wiki/Martin_Odersky
2. http://en.wikipedia.org/wiki/Scala_(programming_language)
3. http://www.infoq.com/cn/articles/scala-technology
4. https://github.com/lauris/awesome-scala
5. http://agiledon.github.io/blog/2014/01/13/testing-styles-of-scalatest/
6. http://agiledon.github.io/blog/2014/07/20/scala-resource/
AD
22
Coursera上有⼀一⻔门免费的课程叫Functional Programming Principles in Scala,由Scala的作者Martin Odersky主讲。
https://www.coursera.org/course/progfun
Q&A