packagenl.devnology.workshop<br />importjava.util._<br />classLearnALanguage(today: Calendar) extendsDevnology{<br /> defw...
What’s the plan?<br />Setup development environment (Eclipse + Scalaplugin)<br />Form pair-programming pairs<br />Explanat...
Euh...Scala?<br />Scala runs on JVM and .Net VM<br />integrates 100% with existing libraries<br />hybrid language: both fu...
Scala in the enterprise<br />
Run Scala as...<br />Interactive / REPL (Read Eval Print Loop): the scala command starts an interactive shell<br />As scri...
Variables, Values & Type Inference<br />varmsg = &quot;welcome to ...&quot; // msg is mutable<br />msg += &quot; Devnology...
Variables, Values & Type Inference<br />valmsg = &quot;welcome to ...&quot; // msg is immutable<br />msg += &quot; Devnolo...
Functions<br />def min(x: Int, y: Int) = if (x &lt; y) x else y<br />// equivalent:<br />def invert(x: Int) = -x // last s...
Every operation is a function call<br />1 + 2    is the same as1.+(2)<br />“to” is not a keyword:  for (i &lt;- 0 to 10) p...
Lists<br />valscores = List(1, 2, 3) // immutable<br />valextraScores: List[Int] = 4 :: 5 :: 6 :: Nil<br />// allScores = ...
Foreach<br />valscores = List(1, 2, 3)<br />// equivalent<br />scores.foreach((n: Int) =&gt; println(n))<br />scores.forea...
For comprehensions<br />valscores = List(1, 2, 3)<br />for (s &lt;- scores)<br />println(s)<br />for (s &lt;- scores if s ...
Arrays<br />valnames = Array(&quot;John&quot;, &quot;Jane&quot;)<br />names(0) = &quot;Richard&quot; // Arrays are mutable...
Maps<br />valtowns = Map(&quot;John&quot; -&gt; &quot;Amsterdam&quot;, &quot;Jane&quot; -&gt; &quot;Rotterdam&quot;) // de...
Classes & Constructors<br />class Person(name: String, age: Int) {<br />if (age &lt; 0) thrownewIllegalArgumentException<b...
Classes & Constructors<br />class Person(name: String, age: Int) {<br />def this(name: String) = this(name, 21) // auxilia...
Classes & Constructors<br />class Person(name: String, age: Int)<br />...<br />val p = new Person(&quot;John&quot;, 33)<br...
Companion Objects<br />// must be declared in same file as Person class<br />object Person {<br />defprintName = println(&...
Traits<br />trait Student {<br />varage = 10;<br />  def greet() = {<br />&quot;Hello teacher!&quot;<br />  }<br />  def s...
Extending Traits<br />class FirstGrader extends Student {<br />  override def greet() = {<br />&quot;Hello amazing teacher...
Trait Mixin<br />// compiler error: abstract function study from trait Student is not implemented<br />valjohn = new Perso...
Scala Application<br />object Person<br />  def main(args: Array[String]) { // define a main method<br />    for (arg &lt;...
Pattern Matching<br />valcolor = if (args.length &gt; 0) args(0) else &quot;&quot;<br />valfruit match {<br />case&quot;re...
Case Classes<br />case class Var(name: String) extendsExpr<br />val v = Var(&quot;sum&quot;)<br />adds a Factory method wi...
Exceptions<br />try {<br />args(0).toFloat<br />} catch {<br />  case ex: NumberFormatException =&gt; println(&quot;Oops!&...
Tuples<br />valpair = (&quot;John&quot;, 28) // Tuple2[String, Int]<br />println(pair._1)<br />println(pair._2)<br />def d...
LABS!!<br />Resources:<br />http://www.scala-lang.org/api<br />http://scala-tools.org/scaladocs/scala-library/2.7.1/<br />...
THANK YOU!<br />
Upcoming SlideShare
Loading in...5
×

Scala: Devnology - Learn A Language Scala

2,540

Published on

Published in: Technology
0 Comments
12 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,540
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
88
Comments
0
Likes
12
Embeds 0
No embeds

No notes for slide

Scala: Devnology - Learn A Language Scala

  1. 1. packagenl.devnology.workshop<br />importjava.util._<br />classLearnALanguage(today: Calendar) extendsDevnology{<br /> defwelcome(participants: List[Participant]) {<br />participants.foreach(p =&gt; println(&quot;welcome&quot; + p))<br /> }<br /> def facilitators() = {<br /> Facilitator(&quot;SoemirnoKartosoewito&quot;) ::<br /> Facilitator(&quot;Jan Willem Tulp&quot;) :: Nil<br /> }<br />}<br />
  2. 2. What’s the plan?<br />Setup development environment (Eclipse + Scalaplugin)<br />Form pair-programming pairs<br />Explanation of basic concepts and syntax<br />Labs, labs, labs...<br />... and of course: share knowledge!<br />
  3. 3. Euh...Scala?<br />Scala runs on JVM and .Net VM<br />integrates 100% with existing libraries<br />hybrid language: both functional and OO<br />statically typed<br />“everything is an object”<br />...Immutability, Currying, Tuples, Closures, Higher Order Functions, etc....<br />
  4. 4. Scala in the enterprise<br />
  5. 5. Run Scala as...<br />Interactive / REPL (Read Eval Print Loop): the scala command starts an interactive shell<br />As script<br />Compiled: scalac command compiles Scala code<br />See: http://www.scala-lang.org/node/166<br />
  6. 6. Variables, Values & Type Inference<br />varmsg = &quot;welcome to ...&quot; // msg is mutable<br />msg += &quot; Devnology&quot; <br />msg = 3 // compiler error<br />
  7. 7. Variables, Values & Type Inference<br />valmsg = &quot;welcome to ...&quot; // msg is immutable<br />msg += &quot; Devnology&quot; // compiler error<br />val n : Int = 3 // explicit type declaration<br />valname : String = &quot;John&quot;<br />
  8. 8. Functions<br />def min(x: Int, y: Int) = if (x &lt; y) x else y<br />// equivalent:<br />def invert(x: Int) = -x // last statement is return value<br />def invert(x: Int) : Int = { return –x }<br />Unit can be considered as java’s void<br />Watch out!<br />def invert(x: Int) { // will return Unit, = is absent<br /> -x<br />}<br />
  9. 9. Every operation is a function call<br />1 + 2 is the same as1.+(2)<br />“to” is not a keyword: for (i &lt;- 0 to 10) print(i)<br />map containsKey ‘a’ is the same as map.containsKey(‘a’)<br />
  10. 10. Lists<br />valscores = List(1, 2, 3) // immutable<br />valextraScores: List[Int] = 4 :: 5 :: 6 :: Nil<br />// allScores = List(1, 2, 3, 4, 5, 6)<br />// scores = List(1, 2, 3)<br />// extraScores = List(4, 5, 6)<br />valallScores = scores ::: extraScores<br />valevenMoreScores = 7 :: allScores<br />Nil is synonym for empty list<br />
  11. 11. Foreach<br />valscores = List(1, 2, 3)<br />// equivalent<br />scores.foreach((n: Int) =&gt; println(n))<br />scores.foreach(n =&gt; println(n))<br />scores.foreach(println)<br />
  12. 12. For comprehensions<br />valscores = List(1, 2, 3)<br />for (s &lt;- scores)<br />println(s)<br />for (s &lt;- scores if s &gt; 1)<br />println(s)<br />
  13. 13. Arrays<br />valnames = Array(&quot;John&quot;, &quot;Jane&quot;)<br />names(0) = &quot;Richard&quot; // Arrays are mutable<br />val cities = new Array[String](2)<br />cities(0) = &quot;Amsterdam&quot;<br />cities(1) = &quot;Rotterdam&quot;<br />for (i &lt;- 0 to 1)<br />println(cities(i))<br />
  14. 14. Maps<br />valtowns = Map(&quot;John&quot; -&gt; &quot;Amsterdam&quot;, &quot;Jane&quot; -&gt; &quot;Rotterdam&quot;) // default immutable Map<br />var a = towns(&quot;John&quot;) // returns &quot;Amsterdam&quot;<br />a = towns get &quot;John”// returns Some(Amsterdam)<br />a = towns get &quot;John&quot;get // returns &quot;Amsterdam&quot;<br />var r = towns(&quot;Bill&quot;) // throws NoSuchElementException<br />r = towns get &quot;Bill”// returns None<br />towns.update(&quot;John&quot;, &quot;Delft&quot;) // returns a new Map<br />
  15. 15. Classes & Constructors<br />class Person(name: String, age: Int) {<br />if (age &lt; 0) thrownewIllegalArgumentException<br />defsayHello() { println(&quot;Hello, &quot; + name) }<br />}<br />class Person(name: String, age: Int) {<br />require (age &gt;= 0)<br />defsayHello() { println(&quot;Hello, &quot; + name) }<br />}<br />
  16. 16. Classes & Constructors<br />class Person(name: String, age: Int) {<br />def this(name: String) = this(name, 21) // auxiliary<br />def this(age: Int) = this(“John”, age) // auxiliary<br />def this() = this(“John”, 21) // auxiliary<br />}<br />
  17. 17. Classes & Constructors<br />class Person(name: String, age: Int)<br />...<br />val p = new Person(&quot;John&quot;, 33)<br />val a = p.age// compiler error<br />p.name = &quot;Richard&quot; // compiler error<br />class Person(var name: String, val age: Int)<br />
  18. 18. Companion Objects<br />// must be declared in same file as Person class<br />object Person {<br />defprintName = println(&quot;John&quot;)<br />}<br />valp = Person // Singleton is also an object<br />Person.printName// similar to static methods in Java/C#<br />
  19. 19. Traits<br />trait Student {<br />varage = 10;<br /> def greet() = {<br />&quot;Hello teacher!&quot;<br /> }<br /> def study(): String // abstract method<br />}<br />
  20. 20. Extending Traits<br />class FirstGrader extends Student {<br /> override def greet() = {<br />&quot;Hello amazing teacher!&quot;<br /> }<br /> override def study(): String = {<br />“I am studying really hard!&quot;<br /> }<br />}<br />
  21. 21. Trait Mixin<br />// compiler error: abstract function study from trait Student is not implemented<br />valjohn = new Person with Student<br />traitSimpleStudent {<br />def greet() = &quot;Hello amazing teacher!&quot;<br />}<br />// this is ok<br />val john = new Person withSimpleStudent<br />println(john.greet())<br />
  22. 22. Scala Application<br />object Person<br /> def main(args: Array[String]) { // define a main method<br /> for (arg &lt;- args)<br />println(&quot;Hello &quot; + arg)<br /> }<br />}<br />// or extend Application trait<br />object Person extends Application { <br /> for (name &lt;- List(&quot;John&quot;, &quot;Jane&quot;)) <br />println(&quot;Hello &quot; + name)<br />}<br />
  23. 23. Pattern Matching<br />valcolor = if (args.length &gt; 0) args(0) else &quot;&quot;<br />valfruit match {<br />case&quot;red&quot;=&gt;&quot;apple&quot;<br />case&quot;orange&quot; =&gt;&quot;orange&quot;<br />case&quot;yellow&quot; =&gt;&quot;banana&quot;<br />case_ =&gt; &quot;yuk!&quot;<br />}<br />
  24. 24. Case Classes<br />case class Var(name: String) extendsExpr<br />val v = Var(&quot;sum&quot;)<br />adds a Factory method with the name of the class<br />all parameters implicitly get a val prefix, so they are maintained as fields<br />“natural” implementation of toString, hashCode and equals<br />big advantage: support pattern matching<br />
  25. 25. Exceptions<br />try {<br />args(0).toFloat<br />} catch {<br /> case ex: NumberFormatException =&gt; println(&quot;Oops!&quot;)<br />}<br />
  26. 26. Tuples<br />valpair = (&quot;John&quot;, 28) // Tuple2[String, Int]<br />println(pair._1)<br />println(pair._2)<br />def divProd(x: Int, y:Int) = (x / y, x * y)<br />valdp = divProd(10, 2)<br />println(pair._1) // 5<br />println(pair._2) // 20<br />
  27. 27. LABS!!<br />Resources:<br />http://www.scala-lang.org/api<br />http://scala-tools.org/scaladocs/scala-library/2.7.1/<br />http://www.codecommit.com/blog/scala/roundup-scala-for-java-refugees<br />http://blogs.sun.com/sundararajan/entry/scala_for_java_programmers<br />
  28. 28. THANK YOU!<br />
  1. A particular slide catching your eye?

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

×