SlideShare une entreprise Scribd logo
1  sur  41
Télécharger pour lire hors ligne
聚石@taobao.com
https://github.com/zhongl
Real-World Scala
Get Started
Create Project
$ g8 typesafehub/scala-sbt
Scala Project Using sbt
organization [org.example]: me.zhongl
name [Scala Project]: demo
scala_version [2.9.2]:
version [0.1-SNAPSHOT]:
Template applied in ./demo
$ tree demo
demo
├── README
├── project
│ └── DemoBuild.scala
└── src
└── main
└── scala
└── me
└── zhongl
└── Demo.scala
Project Structure
Build Spec
import sbt._
import sbt.Keys._
object DemoBuild extends Build {
lazy val demo = Project(
id = "demo",
base = file("."),
settings = Project.defaultSettings ++ Seq(
name := "demo",
organization := "me.zhongl",
version := "0.1-SNAPSHOT",
scalaVersion := "2.9.2"
// add other settings here
)
)
}
Hello, demo
$ sbt run
[info] Loading global plugins from ~/.sbt/plugins
[info] Loading project definition from ~/demo/project
[info] Set current project to demo (in build file:~demo/)
[info] Running me.zhongl.Demo
Hello, demo
[success] Total time: 0 s, completed 2013-5-24 9:38:47
IDE Plugins
$ cat ~/.sbt/plugins/build.sbt // Global
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" %
"1.2.0")
$ cat ~/demo/project/plugins.sbt // Project
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" %
"1.2.0")
$ sbt gen-idea // Create IDE Files
Dependencies
settings = Project.defaultSettings ++ Seq(
name := "demo",
organization := "me.zhongl",
version := "0.1-SNAPSHOT",
scalaVersion := "2.9.2",
libraryDependencies := Seq(
"org.scala-lang" % "scala-library" % "2.9.2",
"org.scalatest" %% "scalatest" % "1.7.2" % "test"
// "org.scalatest" % "scalatest_2.9.2" % "1.7.2" % "test"
)
)
Resolver
# ~/.sbt/local.sbt
resolvers <<= resolvers {rs =>
val localMaven = "Local Maven Repository" at "file://"
+Path.userHome.absolutePath+"/.m2/repository"
localMaven +: rs
}
Package
$ sbt package
$ sbt package-bin
$ sbt package-doc
$ sbt package-src
Publish
$ sbt publish // central repos
$ sbt publish-local // local repos
● giter8
● sbt
● sbt-idea
● sbteclipse
● nbsbt
● Typesafe Activator
● Scala Maven Plugin
● Buildr
● Gradle Scala Plugin
References
Behavior-Drive Development
package me.zhongl
import org.scalatest.FunSpec
import org.scalatest.matchers.ShouldMatchers
class DemoSpec extends FunSpec with ShouldMatchers {
describe("Demo") {
it("should sum two integers") {
Demo sum (1, 2) should be (3)
}
}
}
Demo Spec
Continue Test
$ sbt
> ~ test
[info] Compiling 1 Scala source to ~/demo/target/scala-
2.9.2/test-classes...
[error] ~/demo/src/test/scala/me/zhongl/DemoSpec.scala:9:
value sum is not a member of object me.zhongl.Demo
[error] Demo sum (1, 2) should be (3)
[error] ^
[error] one error found
[error] (test:compile) Compilation failed
[error] Total time: 2 s, completed 2013-5-24 11:19:08
1. Waiting for source changes... (press enter to
interrupt)
Implement
package me.zhongl
object Demo extends App {
println("Hello, demo")
def sum(x: Int, y: Int) = x + y
}
[info] Compiling 1 Scala source to ~/demo/target/scala-
2.9.2/classes...
[info] DemoSpec:
[info] Demo
[info] - should sum two integers
[info] Passed: : Total 1, Failed 0, Errors 0, Passed 1,
Skipped 0
[success] Total time: 1 s, completed 2013-5-24 11:23:16
2. Waiting for source changes... (press enter to
interrupt)
Continue Test
Test Only
> test-only me.zhongl.DemoSpec
[info] DemoSpec:
[info] Demo
[info] - should sum two integers
[info] Passed: : Total 1, Failed 0, Errors 0, Passed 1,
Skipped 0
[success] Total time: 1 s, completed 2013-5-24 11:30:06
More Matchers
List(1, 2, 3) should have size (3)
"Scala" should startWith ("Sc")
Map("K" -> "V") should contain key ("K")
book should have ('title ("Programming in Scala"))
evaluating { assert(1 < 0) } should produce
[AssertionError]
● http://www.scalatest.org/ (备梯)
● http://etorreborre.github.io/specs2/
● https://code.google.com/p/scalacheck/
● http://scalamock.org/
References
Coverage
Scct plugin
# project/plugins.sbt
resolvers += Classpaths.typesafeResolver
resolvers += "scct-github-repository" at "http://mtkopone.
github.com/scct/maven-repo"
addSbtPlugin("reaktor" % "sbt-scct" % "0.2-SNAPSHOT")
# project/DemoBuild.scala
settings = Project.defaultSettings ++ Seq(
id := "demo"
...
) ++ ScctPlugin.instrumentSettings
Scct plugin
$ sbt clean scct:test
$ sbt
> ;clean ;scct:test
# open
./target/scala_2.9.2/coverage-report/index.html
Scct plugin
● http://mtkopone.github.io/scct/
References
Effective Scala
No statement
// Bad
def findPeopleIn(c: City, ps: Set[People]) = {
val found = new mutable.HashSet[People]
for (p <- ps) {
for(a <- p.addresses) {
if (a.city == c) found.put(p)
}
}
return found
}
Be expression
// Good
def findPeopleIn(c: City, ps: Set[People]) = {
for {
p <- ps
a <- p.addresses
if a.city = c
} yield p
}
Functional Magic
def firstPrimeGreatThan(num: Int): Int = {
def prime(s: Stream[Int],f: Int => Boolean): Int = s match
{
case h #:: t if f(h) => h
case h #:: t => prime(t filter (_ % h > 0), f)
}
prime(Stream from 2, _ > num)
}
assert(firstPrimeGreatThan(20) == 23)
assert(firstPrimeGreatThan(100) == 101)
Use require
class Person(val name: String, val age: Int) {
// Bad
if (name == null || age <= 0)
throw new IllegalArguemntException()
// Good
require(name != null, "name is required.")
require(age > 0, "age should greater than zero.")
}
DSL
class Matcher(s: String) {
def shouldMatch(regex: String) =
require(s != null && s.matches(regex),
"[" + s + "] should match " + regex)
}
implicit val str2Matcher = new Matcher(_:String)
class Person(val name: String, val age: Int) {
name shouldMatch """w+"""
}
Limit the scope of Implicits
● http://docs.scala-lang.org/
● http://twitter.github.io/effectivescala/
● http://twitter.github.io/scala_school/
● http://zh.scala-tour.com/
● http://stackoverflow.com/tags/scala/info
● https://github.com/languages/Scala
References
Books
● Scala for the Impatient (中文版)
● Programming Scala Tackle Multi-Core
Complexity on the Java Virtual Machine(中文
版)
● Scala in Depth (翻译中)
● Programming in Scala: A Comprehensive
Step-by-Step Guide, 2nd Edition
@odersky
@jboner
Typesafe
Akka
Slick
Play
@邓草原
@fujohnwang
@hongjiang_wang
China Scala User Group
Thanks

Contenu connexe

Tendances

Tasks: you gotta know how to run them
Tasks: you gotta know how to run themTasks: you gotta know how to run them
Tasks: you gotta know how to run themFilipe Ximenes
 
Flux and InfluxDB 2.0 by Paul Dix
Flux and InfluxDB 2.0 by Paul DixFlux and InfluxDB 2.0 by Paul Dix
Flux and InfluxDB 2.0 by Paul DixInfluxData
 
React native-firebase startup-mtup
React native-firebase startup-mtupReact native-firebase startup-mtup
React native-firebase startup-mtupt k
 
[Quase] Tudo que você precisa saber sobre tarefas assíncronas
[Quase] Tudo que você precisa saber sobre  tarefas assíncronas[Quase] Tudo que você precisa saber sobre  tarefas assíncronas
[Quase] Tudo que você precisa saber sobre tarefas assíncronasFilipe Ximenes
 
Меняем javascript с помощью javascript
Меняем javascript с помощью javascriptМеняем javascript с помощью javascript
Меняем javascript с помощью javascriptPavel Volokitin
 
Wprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache HadoopWprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache HadoopSages
 
Swift Sequences & Collections
Swift Sequences & CollectionsSwift Sequences & Collections
Swift Sequences & CollectionsCocoaHeads France
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJSSandi Barr
 
05 pig user defined functions (udfs)
05 pig user defined functions (udfs)05 pig user defined functions (udfs)
05 pig user defined functions (udfs)Subhas Kumar Ghosh
 
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash courseCodepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash courseSages
 
Flux and InfluxDB 2.0
Flux and InfluxDB 2.0Flux and InfluxDB 2.0
Flux and InfluxDB 2.0InfluxData
 
Async Microservices with Twitter's Finagle
Async Microservices with Twitter's FinagleAsync Microservices with Twitter's Finagle
Async Microservices with Twitter's FinagleVladimir Kostyukov
 
OO JS for AS3 Devs
OO JS for AS3 DevsOO JS for AS3 Devs
OO JS for AS3 DevsJason Hanson
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloadingRai University
 
Cambio de bases
Cambio de basesCambio de bases
Cambio de basesalcon2015
 
Compositional I/O Stream in Scala
Compositional I/O Stream in ScalaCompositional I/O Stream in Scala
Compositional I/O Stream in ScalaC4Media
 
Transducers in JavaScript
Transducers in JavaScriptTransducers in JavaScript
Transducers in JavaScriptPavel Forkert
 

Tendances (20)

Tasks: you gotta know how to run them
Tasks: you gotta know how to run themTasks: you gotta know how to run them
Tasks: you gotta know how to run them
 
Flux and InfluxDB 2.0 by Paul Dix
Flux and InfluxDB 2.0 by Paul DixFlux and InfluxDB 2.0 by Paul Dix
Flux and InfluxDB 2.0 by Paul Dix
 
React native-firebase startup-mtup
React native-firebase startup-mtupReact native-firebase startup-mtup
React native-firebase startup-mtup
 
[Quase] Tudo que você precisa saber sobre tarefas assíncronas
[Quase] Tudo que você precisa saber sobre  tarefas assíncronas[Quase] Tudo que você precisa saber sobre  tarefas assíncronas
[Quase] Tudo que você precisa saber sobre tarefas assíncronas
 
Меняем javascript с помощью javascript
Меняем javascript с помощью javascriptМеняем javascript с помощью javascript
Меняем javascript с помощью javascript
 
Oop assignment 02
Oop assignment 02Oop assignment 02
Oop assignment 02
 
Wprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache HadoopWprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache Hadoop
 
Swift Sequences & Collections
Swift Sequences & CollectionsSwift Sequences & Collections
Swift Sequences & Collections
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJS
 
05 pig user defined functions (udfs)
05 pig user defined functions (udfs)05 pig user defined functions (udfs)
05 pig user defined functions (udfs)
 
Ordered Record Collection
Ordered Record CollectionOrdered Record Collection
Ordered Record Collection
 
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash courseCodepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
Codepot - Pig i Hive: szybkie wprowadzenie / Pig and Hive crash course
 
Flux and InfluxDB 2.0
Flux and InfluxDB 2.0Flux and InfluxDB 2.0
Flux and InfluxDB 2.0
 
Async Microservices with Twitter's Finagle
Async Microservices with Twitter's FinagleAsync Microservices with Twitter's Finagle
Async Microservices with Twitter's Finagle
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
 
OO JS for AS3 Devs
OO JS for AS3 DevsOO JS for AS3 Devs
OO JS for AS3 Devs
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
 
Cambio de bases
Cambio de basesCambio de bases
Cambio de bases
 
Compositional I/O Stream in Scala
Compositional I/O Stream in ScalaCompositional I/O Stream in Scala
Compositional I/O Stream in Scala
 
Transducers in JavaScript
Transducers in JavaScriptTransducers in JavaScript
Transducers in JavaScript
 

Similaire à Real world scala

GPars For Beginners
GPars For BeginnersGPars For Beginners
GPars For BeginnersMatt Passell
 
Gradle build tool that rocks with DSL JavaOne India 4th May 2012
Gradle build tool that rocks with DSL JavaOne India 4th May 2012Gradle build tool that rocks with DSL JavaOne India 4th May 2012
Gradle build tool that rocks with DSL JavaOne India 4th May 2012Rajmahendra Hegde
 
Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Jonathan Felch
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
ゼロから始めるScalaプロジェクト
ゼロから始めるScalaプロジェクトゼロから始めるScalaプロジェクト
ゼロから始めるScalaプロジェクトRyuichi ITO
 
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Tsuyoshi Yamamoto
 
Gradleintroduction 111010130329-phpapp01
Gradleintroduction 111010130329-phpapp01Gradleintroduction 111010130329-phpapp01
Gradleintroduction 111010130329-phpapp01Tino Isnich
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsBartosz Kosarzycki
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016STX Next
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing UpDavid Padbury
 
Practical Testing of Ruby Core
Practical Testing of Ruby CorePractical Testing of Ruby Core
Practical Testing of Ruby CoreHiroshi SHIBATA
 
Java patterns in Scala
Java patterns in ScalaJava patterns in Scala
Java patterns in ScalaRadim Pavlicek
 
Gradle in 45min - JBCN2-16 version
Gradle in 45min - JBCN2-16 versionGradle in 45min - JBCN2-16 version
Gradle in 45min - JBCN2-16 versionSchalk Cronjé
 
Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+ConFoo
 
How to Write Node.js Module
How to Write Node.js ModuleHow to Write Node.js Module
How to Write Node.js ModuleFred Chien
 

Similaire à Real world scala (20)

SCALA - Functional domain
SCALA -  Functional domainSCALA -  Functional domain
SCALA - Functional domain
 
GPars For Beginners
GPars For BeginnersGPars For Beginners
GPars For Beginners
 
Gradle build tool that rocks with DSL JavaOne India 4th May 2012
Gradle build tool that rocks with DSL JavaOne India 4th May 2012Gradle build tool that rocks with DSL JavaOne India 4th May 2012
Gradle build tool that rocks with DSL JavaOne India 4th May 2012
 
Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
ゼロから始めるScalaプロジェクト
ゼロから始めるScalaプロジェクトゼロから始めるScalaプロジェクト
ゼロから始めるScalaプロジェクト
 
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
 
Gradle Introduction
Gradle IntroductionGradle Introduction
Gradle Introduction
 
Gradleintroduction 111010130329-phpapp01
Gradleintroduction 111010130329-phpapp01Gradleintroduction 111010130329-phpapp01
Gradleintroduction 111010130329-phpapp01
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
Refactoring
RefactoringRefactoring
Refactoring
 
Practical Testing of Ruby Core
Practical Testing of Ruby CorePractical Testing of Ruby Core
Practical Testing of Ruby Core
 
Java patterns in Scala
Java patterns in ScalaJava patterns in Scala
Java patterns in Scala
 
Gradle in 45min - JBCN2-16 version
Gradle in 45min - JBCN2-16 versionGradle in 45min - JBCN2-16 version
Gradle in 45min - JBCN2-16 version
 
Tres Gemas De Ruby
Tres Gemas De RubyTres Gemas De Ruby
Tres Gemas De Ruby
 
Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Marrow: A Meta-Framework for Python 2.6+ and 3.1+
 
How to Write Node.js Module
How to Write Node.js ModuleHow to Write Node.js Module
How to Write Node.js Module
 
Enter the gradle
Enter the gradleEnter the gradle
Enter the gradle
 

Plus de lunfu zhong

项目求生指南
项目求生指南项目求生指南
项目求生指南lunfu zhong
 
Lego: A brick system build by scala
Lego: A brick system build by scalaLego: A brick system build by scala
Lego: A brick system build by scalalunfu zhong
 
Zhongl scala summary
Zhongl scala summaryZhongl scala summary
Zhongl scala summarylunfu zhong
 
Instroduce Hazelcast
Instroduce HazelcastInstroduce Hazelcast
Instroduce Hazelcastlunfu zhong
 
Jvm分享20101228
Jvm分享20101228Jvm分享20101228
Jvm分享20101228lunfu zhong
 

Plus de lunfu zhong (7)

项目求生指南
项目求生指南项目求生指南
项目求生指南
 
Spring boot
Spring bootSpring boot
Spring boot
 
Lego: A brick system build by scala
Lego: A brick system build by scalaLego: A brick system build by scala
Lego: A brick system build by scala
 
Zhongl scala summary
Zhongl scala summaryZhongl scala summary
Zhongl scala summary
 
Housemd
HousemdHousemd
Housemd
 
Instroduce Hazelcast
Instroduce HazelcastInstroduce Hazelcast
Instroduce Hazelcast
 
Jvm分享20101228
Jvm分享20101228Jvm分享20101228
Jvm分享20101228
 

Dernier

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 

Dernier (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 

Real world scala

  • 1.
  • 5. Create Project $ g8 typesafehub/scala-sbt Scala Project Using sbt organization [org.example]: me.zhongl name [Scala Project]: demo scala_version [2.9.2]: version [0.1-SNAPSHOT]: Template applied in ./demo
  • 6. $ tree demo demo ├── README ├── project │ └── DemoBuild.scala └── src └── main └── scala └── me └── zhongl └── Demo.scala Project Structure
  • 7. Build Spec import sbt._ import sbt.Keys._ object DemoBuild extends Build { lazy val demo = Project( id = "demo", base = file("."), settings = Project.defaultSettings ++ Seq( name := "demo", organization := "me.zhongl", version := "0.1-SNAPSHOT", scalaVersion := "2.9.2" // add other settings here ) ) }
  • 8. Hello, demo $ sbt run [info] Loading global plugins from ~/.sbt/plugins [info] Loading project definition from ~/demo/project [info] Set current project to demo (in build file:~demo/) [info] Running me.zhongl.Demo Hello, demo [success] Total time: 0 s, completed 2013-5-24 9:38:47
  • 9. IDE Plugins $ cat ~/.sbt/plugins/build.sbt // Global addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.2.0") $ cat ~/demo/project/plugins.sbt // Project addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.2.0") $ sbt gen-idea // Create IDE Files
  • 10. Dependencies settings = Project.defaultSettings ++ Seq( name := "demo", organization := "me.zhongl", version := "0.1-SNAPSHOT", scalaVersion := "2.9.2", libraryDependencies := Seq( "org.scala-lang" % "scala-library" % "2.9.2", "org.scalatest" %% "scalatest" % "1.7.2" % "test" // "org.scalatest" % "scalatest_2.9.2" % "1.7.2" % "test" ) )
  • 11. Resolver # ~/.sbt/local.sbt resolvers <<= resolvers {rs => val localMaven = "Local Maven Repository" at "file://" +Path.userHome.absolutePath+"/.m2/repository" localMaven +: rs }
  • 12. Package $ sbt package $ sbt package-bin $ sbt package-doc $ sbt package-src
  • 13. Publish $ sbt publish // central repos $ sbt publish-local // local repos
  • 14. ● giter8 ● sbt ● sbt-idea ● sbteclipse ● nbsbt ● Typesafe Activator ● Scala Maven Plugin ● Buildr ● Gradle Scala Plugin References
  • 16. package me.zhongl import org.scalatest.FunSpec import org.scalatest.matchers.ShouldMatchers class DemoSpec extends FunSpec with ShouldMatchers { describe("Demo") { it("should sum two integers") { Demo sum (1, 2) should be (3) } } } Demo Spec
  • 17. Continue Test $ sbt > ~ test [info] Compiling 1 Scala source to ~/demo/target/scala- 2.9.2/test-classes... [error] ~/demo/src/test/scala/me/zhongl/DemoSpec.scala:9: value sum is not a member of object me.zhongl.Demo [error] Demo sum (1, 2) should be (3) [error] ^ [error] one error found [error] (test:compile) Compilation failed [error] Total time: 2 s, completed 2013-5-24 11:19:08 1. Waiting for source changes... (press enter to interrupt)
  • 18. Implement package me.zhongl object Demo extends App { println("Hello, demo") def sum(x: Int, y: Int) = x + y }
  • 19. [info] Compiling 1 Scala source to ~/demo/target/scala- 2.9.2/classes... [info] DemoSpec: [info] Demo [info] - should sum two integers [info] Passed: : Total 1, Failed 0, Errors 0, Passed 1, Skipped 0 [success] Total time: 1 s, completed 2013-5-24 11:23:16 2. Waiting for source changes... (press enter to interrupt) Continue Test
  • 20. Test Only > test-only me.zhongl.DemoSpec [info] DemoSpec: [info] Demo [info] - should sum two integers [info] Passed: : Total 1, Failed 0, Errors 0, Passed 1, Skipped 0 [success] Total time: 1 s, completed 2013-5-24 11:30:06
  • 21. More Matchers List(1, 2, 3) should have size (3) "Scala" should startWith ("Sc") Map("K" -> "V") should contain key ("K") book should have ('title ("Programming in Scala")) evaluating { assert(1 < 0) } should produce [AssertionError]
  • 22. ● http://www.scalatest.org/ (备梯) ● http://etorreborre.github.io/specs2/ ● https://code.google.com/p/scalacheck/ ● http://scalamock.org/ References
  • 24. Scct plugin # project/plugins.sbt resolvers += Classpaths.typesafeResolver resolvers += "scct-github-repository" at "http://mtkopone. github.com/scct/maven-repo" addSbtPlugin("reaktor" % "sbt-scct" % "0.2-SNAPSHOT") # project/DemoBuild.scala settings = Project.defaultSettings ++ Seq( id := "demo" ... ) ++ ScctPlugin.instrumentSettings
  • 25. Scct plugin $ sbt clean scct:test $ sbt > ;clean ;scct:test # open ./target/scala_2.9.2/coverage-report/index.html
  • 29. No statement // Bad def findPeopleIn(c: City, ps: Set[People]) = { val found = new mutable.HashSet[People] for (p <- ps) { for(a <- p.addresses) { if (a.city == c) found.put(p) } } return found }
  • 30. Be expression // Good def findPeopleIn(c: City, ps: Set[People]) = { for { p <- ps a <- p.addresses if a.city = c } yield p }
  • 31. Functional Magic def firstPrimeGreatThan(num: Int): Int = { def prime(s: Stream[Int],f: Int => Boolean): Int = s match { case h #:: t if f(h) => h case h #:: t => prime(t filter (_ % h > 0), f) } prime(Stream from 2, _ > num) } assert(firstPrimeGreatThan(20) == 23) assert(firstPrimeGreatThan(100) == 101)
  • 32. Use require class Person(val name: String, val age: Int) { // Bad if (name == null || age <= 0) throw new IllegalArguemntException() // Good require(name != null, "name is required.") require(age > 0, "age should greater than zero.") }
  • 33. DSL class Matcher(s: String) { def shouldMatch(regex: String) = require(s != null && s.matches(regex), "[" + s + "] should match " + regex) } implicit val str2Matcher = new Matcher(_:String) class Person(val name: String, val age: Int) { name shouldMatch """w+""" }
  • 34. Limit the scope of Implicits
  • 35. ● http://docs.scala-lang.org/ ● http://twitter.github.io/effectivescala/ ● http://twitter.github.io/scala_school/ ● http://zh.scala-tour.com/ ● http://stackoverflow.com/tags/scala/info ● https://github.com/languages/Scala References
  • 36. Books ● Scala for the Impatient (中文版) ● Programming Scala Tackle Multi-Core Complexity on the Java Virtual Machine(中文 版) ● Scala in Depth (翻译中) ● Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition
  • 38.