• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Scala: Devnology - Learn A Language Scala
 

Scala: Devnology - Learn A Language Scala

on

  • 3,222 views

 

Statistics

Views

Total Views
3,222
Views on SlideShare
3,092
Embed Views
130

Actions

Likes
3
Downloads
73
Comments
0

6 Embeds 130

http://devnology.nl 58
http://www.janwillemtulp.com 51
http://www.slideshare.net 10
http://www.linkedin.com 9
https://devnology.nl 1
http://www.lmodules.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Scala: Devnology - Learn A Language Scala Scala: Devnology - Learn A Language Scala Presentation Transcript

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