How to START using                     Ngoc Dao
This talk is not about Scala syntax(You can learn 80% of it in 2 days, Sat & Sun)
Source code:https://github.com/ngocdaothanh/sclart
Why I use Scala●   Syntax is similar to Ruby (human oriented)●   Faster than Ruby (= Java, ~C)●   Can reuse all Java libs ...
Agenda1. “scala” and “scalac” commands2. SBT (Scala Build Tool)3. Some useful frameworks
Part 1: scala & scalac
Install Scala1. Download from scala-lang.org2. Config PATH  .bash_profile:  PATH=~/opt/scala-2.9.2/bin:$PATH
~/opt/scala-2.9.2:
Interactive console●   For trying out code snippets●   Like “irb” of Ruby
$ scalaWelcome to Scala version 2.9.2Type :help for more information.scala> 1 + 2res0: Int = 3scala> "hello".lengthres1: I...
Scripting Scala●   Can run a script right away, no compile step is    needed●   To run, “scala” command is needed; it will...
$ cat Hello1.scalaprintln(1 + 2)println("hello".length)$ scala Hello.scala35
Compile Scala●   Compile .scala file to .class file●   Can use “scala” or normal “java” command to    run .class file●   S...
$ scalac Hello1.scalaHello1.scala:1: error: expectedclass or object definitionprintln(1 + 2)^Hello1.scala:2: error: expect...
$ cat Hello2.scalaobject Hello2 {  // Similar to normal Java/C programs  def main(args: Array[String]) {    println(1 + 2)...
Run with “scala” command$ scalac Hello2.scala$ lsHello2$.class    Hello2.class Hello2.scala$ scala Hello235
Run with “java” command$ java Hello2Exception in thread "main"java.lang.NoClassDefFoundError:scala/ScalaObject$ java -cp~/...
Part 2: SBT
SBT (Scala Build Tool)●   Dependency library manager●   Build tool●   Like “gem” + “rake” of Ruby●   Like Maven of Java
Install SBTThere are several ways to install:https://github.com/harrah/xsbt/wiki/Getting-Started-SetupI prefer installing ...
Install SBT manually~/opt/sbt:sbt:java -Xmx512M -XX:MaxPermSize=128M -jar `dirname $0`/sbt-launch-0.12.0.jar "$@".bash_pro...
Project structurehello  build.sbt  src    main      scala        should_be_under_a_package          Hello2.scala      java...
build.sbtorganization := "com.kayac"name              := "hello"                           Like Gemfile of Rubyversion    ...
SBT tasks$ sbt tasksconsole         Starts the Scala interpretercompile         Compiles sourcesclean           Deletes fi...
Demo●   https://github.com/ngocdaothanh/sclart/tree/master/sbt/hello●   sbt console, run, package etc.
SBT pluginshttps://github.com/harrah/xsbt/wiki/sbt-0.10-plugins-listSome useful plugins:* Create Eclipse project file (wit...
Install SBT plugins1. Install plugins at global level~/.sbt/plugins/build.sbtEx:addSbtPlugin("com.typesafe.sbteclipse" % "...
Demo●   Use sbt eclipse to create Eclipse project and    see lib paths
Part3: Some useful frameworkshttps://wiki.scala-lang.org/display/SW/Tools+and+Libraries
Xitrum ● Combine web server + web framework ● https://github.com/ngocdaothanh/xitrum+------------------+          Scalable...
Demo●   https://github.com/ngocdaothanh/xitrum-demos●   Routes●   WebSocket●   xitrum-package
Akka●   http://akka.io/●   Better than Scalas default actor library●   Has power of Erlang:    concurrency    distribution...
FSM Demohttps://github.com/ngocdaothanh/code-lock-fsm-akka
Upcoming SlideShare
Loading in...5
×

How to start using Scala

1,883

Published on

Published in: Technology, Education
5 Comments
3 Likes
Statistics
Notes
  • Mot cai nua co the ke ra la JRebel free cho Scala^^

    http://zeroturnaround.com/jrebel/free-javarebel-for-scala-users-zeroturnaround-announces/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dân dùng Java để viết server side có kinh nghiệm thì biết là Java chậm hơn C k đáng kể:
    http://shootout.alioth.debian.org/u32q/benchmark.php?test=all&lang=java&lang2=gpp

    K đáng kể thế nào thì hãy xem thêm kết quả so sánh Java với PHP v.v.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • lần đầu em nghe thấy từ Human Oriented. Java chậm hơn C đáng kể chứ nhỉ?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Là tốc độ Scala ngang với Java. Suy ra gần bằng C.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • speed = java , ~ C là sao anh :D trừu tượng quá ^^
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
1,883
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
50
Comments
5
Likes
3
Embeds 0
No embeds

No notes for slide

How to start using Scala

  1. 1. How to START using Ngoc Dao
  2. 2. This talk is not about Scala syntax(You can learn 80% of it in 2 days, Sat & Sun)
  3. 3. Source code:https://github.com/ngocdaothanh/sclart
  4. 4. Why I use Scala● Syntax is similar to Ruby (human oriented)● Faster than Ruby (= Java, ~C)● Can reuse all Java libs (Java has lots of libs!)● Easy to program for multicores (like Erlang)
  5. 5. Agenda1. “scala” and “scalac” commands2. SBT (Scala Build Tool)3. Some useful frameworks
  6. 6. Part 1: scala & scalac
  7. 7. Install Scala1. Download from scala-lang.org2. Config PATH .bash_profile: PATH=~/opt/scala-2.9.2/bin:$PATH
  8. 8. ~/opt/scala-2.9.2:
  9. 9. Interactive console● For trying out code snippets● Like “irb” of Ruby
  10. 10. $ scalaWelcome to Scala version 2.9.2Type :help for more information.scala> 1 + 2res0: Int = 3scala> "hello".lengthres1: Int = 5scala> :q$
  11. 11. Scripting Scala● Can run a script right away, no compile step is needed● To run, “scala” command is needed; it will compile on the fly then run● For simple one-file programs
  12. 12. $ cat Hello1.scalaprintln(1 + 2)println("hello".length)$ scala Hello.scala35
  13. 13. Compile Scala● Compile .scala file to .class file● Can use “scala” or normal “java” command to run .class file● Simple programs: scalac (like javac)● Bigger programs: SBT (see next slides)
  14. 14. $ scalac Hello1.scalaHello1.scala:1: error: expectedclass or object definitionprintln(1 + 2)^Hello1.scala:2: error: expectedclass or object definitionprintln("hello".length)^two errors found Need to wrap the script with “main”
  15. 15. $ cat Hello2.scalaobject Hello2 { // Similar to normal Java/C programs def main(args: Array[String]) { println(1 + 2) println("hello".length) }}
  16. 16. Run with “scala” command$ scalac Hello2.scala$ lsHello2$.class Hello2.class Hello2.scala$ scala Hello235
  17. 17. Run with “java” command$ java Hello2Exception in thread "main"java.lang.NoClassDefFoundError:scala/ScalaObject$ java -cp~/opt/scala-2.9.2/lib/scala-library.jar:.Hello2
  18. 18. Part 2: SBT
  19. 19. SBT (Scala Build Tool)● Dependency library manager● Build tool● Like “gem” + “rake” of Ruby● Like Maven of Java
  20. 20. Install SBTThere are several ways to install:https://github.com/harrah/xsbt/wiki/Getting-Started-SetupI prefer installing SBT manually
  21. 21. Install SBT manually~/opt/sbt:sbt:java -Xmx512M -XX:MaxPermSize=128M -jar `dirname $0`/sbt-launch-0.12.0.jar "$@".bash_profile:PATH=~/opt/sbt:$PATH
  22. 22. Project structurehello build.sbt src main scala should_be_under_a_package Hello2.scala java (Java source files, if any) test (Scala/Java test source files, if any) scala java target scala-2.9.2 classes (Compiled .class files)
  23. 23. build.sbtorganization := "com.kayac"name := "hello" Like Gemfile of Rubyversion := "1.0-SNAPSHOT"scalaVersion := "2.9.2"scalacOptions ++= Seq( Packaged .jar file: "-deprecation", hello_2.9.2-1.0-SNAPSHOT.jar "-unchecked") (Management of dependency libraries will be talked at part 3)
  24. 24. SBT tasks$ sbt tasksconsole Starts the Scala interpretercompile Compiles sourcesclean Deletes files produced by the buildrun Runs a main classpackage Produces project jar filepublish Publishes project jar file to a repositorypublish-local Copies project jar file to ~/.ivy2test Executes all teststest-only Executes the tests provided as argumentstest-quick Executes the tests that either failed before
  25. 25. Demo● https://github.com/ngocdaothanh/sclart/tree/master/sbt/hello● sbt console, run, package etc.
  26. 26. SBT pluginshttps://github.com/harrah/xsbt/wiki/sbt-0.10-plugins-listSome useful plugins:* Create Eclipse project file (with library paths):https://github.com/typesafehub/sbteclipse* One jar plugins
  27. 27. Install SBT plugins1. Install plugins at global level~/.sbt/plugins/build.sbtEx:addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")2. Install plugins at project levelproject/plugins.sbtEx:https://github.com/ngocdaothanh/xitrum-demos/blob/master/project/plugins.sbt
  28. 28. Demo● Use sbt eclipse to create Eclipse project and see lib paths
  29. 29. Part3: Some useful frameworkshttps://wiki.scala-lang.org/display/SW/Tools+and+Libraries
  30. 30. Xitrum ● Combine web server + web framework ● https://github.com/ngocdaothanh/xitrum+------------------+ Scalable: session, realtime data etc.| Your app |+------------------+| Xitrum ||+----------------+||| Web framework || <-- Hazelcast --> Other instances||----------------|||| HTTP(S) Server |||+----------------+|+------------------+ Fast static file serving: https://gist.github.com/3293596 WebSocket
  31. 31. Demo● https://github.com/ngocdaothanh/xitrum-demos● Routes● WebSocket● xitrum-package
  32. 32. Akka● http://akka.io/● Better than Scalas default actor library● Has power of Erlang: concurrency distribution failure supervisor FSM (Finite State Machine, very good for realtime multiplayer games, to manage states of game sessions and inter-player interactions)
  33. 33. FSM Demohttps://github.com/ngocdaothanh/code-lock-fsm-akka
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×