SlideShare ist ein Scribd-Unternehmen logo
1 von 88
Downloaden Sie, um offline zu lesen
민형기
S-Core Cloud Computing Lab
          Data Solution Part
                2013. 2. 18.
Contents

I.   Scala란?
II. Scala는 간결하다.
III. Scala는 객체지향 언어이다.
IV. Scala는 함수형 언어이다.
V. Scala는 동적 언어이다.
VI. Scala는 정적으로 타입화된 언어이다.
VII. Scala는 많은 좋은 기능이 있다.
VIII.Scala를 어떻게 공부하지?
                             1
Scala 란?
object Hello {
  def main(args:Array[String]) {
     println(“Hello, World”)
  }
}
                                   2
Scala 정의

          Scala [skah-lah]
         = scalable language
    The language is so named because it was designed to grow
    with the demands of its users.
                                   -Programming In Scala, p39


    Scala is a modern multi-paradigm programming language
    designed to express common programming patterns in a
    concise, elegant, and type-safe way.
                                              -scala-lang.com
                                                                3
Scala에 대한 의견들
   If I were to pick a language to use on the JVM today other
   than Java, it would be Scala.
                                     -James Gosling, creater of Java
   http://www.adam-bien.com/roller/abien/entry/java_net_javaone_which_programming



   My tip thought for the long term replacement of javac is Scala.
   I’m very impressed with it! I can honestly say if someone had
   shown me the Programming in Scala book […] back in 2003.
    I’d probably have never created Groovy.
                                  -James Strachan, creator of Groovy
   http://macstrac.blogspot.com/2009/04/scala-as-long-term-replacement-for.html



   Scala, it must be stated, is the current heir apparent to the Java
   throne. No other language on the JVM seems as capable of
   being a “replacement for Java” as Scala, and the momentum
   behind Scala is now uniquestionable.
                                         - Charlies Nutter, JRuby lead
   http://blog.headius.com/2009/04/future-part-one.html

                                                                                    4
Scalability in Programming Language


     어플리케이션이 사용자의 요구에 맞추기 위해 크기나 용량을 변
     경해도, 그 기능이 계속하여 잘 동작 할 수 있는 능력
                                    -Terms




     문제 영역의 크기나 복잡도가 증가하여도 유사한 방법으로 문제
     해결이 가능한 것.

     - 빌딩 블록의 수가 증가하면  추상화 / 축약
     - 언어의 지원 / 라이브러리 / 프레임 워크




                                             5
언어 확장의 예시 – 분수 (Rational)


    규칙:
                (a, b) 단, b != 0
                (a, b) + (c, d) = (ad+bc, bd)
                (a, b) * (c, d) = (ac, bd)



    *기타
    - Big Integer
    - Complex number
    - Vector
    - Matrix …



                                                6
BigInteger vs. BigInt
                     Java
                                                                       Integer와
         public BigInteger factorial(BigInteger x) {                  유사하지 않음
           if ( x == BigInteger.ZERO )
             return BigInteger.ONE;
           else
             return x.multiply(
                factorial (x.subtract(BigInteger.ONE)));
         }




                    Scala



         def factorial(x: BigInt):BigInt =
           if (x == 0) 1 else x * factorial(x-1)




출처: http://www.artima.com/scalazine/articles/scalable-language.html               7
Google Trends




출처: http://www.google.com/trends/explore#q=scala,%20clojure,%20groovy   8
Job Trends




출처: http://www.indeed.com/jobtrends?q=scala%2C+clojure%2C+groovy&l=   9
Scala Today




출처: http://www.slideshare.net/Odersky/devoxx   10
Scala Best Practices

      Twitter
         – Kestrel: queuing
         – FlockDB(and Gizzard): social graph store
         – Hawkwind: people search
         – Hosebird: streaming API
         – Scalding: MapReduce Wrapper (build on top of Cascading)


      Guardian: Open Platform
         – 2009.11:          java + guice + guice servlets + apache solr
         – 2010.01:          Integration tests with ScalaTest / maven scala plugin
         – 2010.03:          scala + guice + guice servlets + apache solr
         – 2010.06:          maven -> sbt
         – 2011.06:          scala + lift + apache solr ( 5k loc -> 3.5k)
출처: http://www.slideshare.net/mslinn/scala-adoption-by-enterprises                   11
Scala 언어 가계도
                                 Simula
   Lisp                              objects               Prolog
     functional
                   C
                                 Smalltalk
     programming       syntax
                                                            pattern
                                                            matching
   ML
                           C++

                                                        Erlang
    Haskell
                          Java
                                               Actors



                       Scala
                                                                       12
Scala를 멀리서 보기


      함수형                   객체 지향                   동적
     프로그래밍                  프로그래밍                  프로그래밍


                      강화된 타입 시스템


                             JVM/.Net
  ※동적 프로그래밍: 스칼라는 엄밀히 말하자면 동적 언어는 아니지만, 동적 언어에서 제공하는 많은 특징을 가지고 있다.
   (간결함(Concise), REPL, 고수준함수, Duck Typing 등)
                                                                      13
Scala 특징 1/3

 Scala는 함수형과 객체지향형의 성격을 다
  가지고 있다.
  –Method를 포함한 모든 함수 (function)은 값(value)이다.
  –모든 값(value)는 객체(object)이다.

 Scala는 정적 타입시스템이다.
  – 강력한 타입 추론 시스템:
    in Java 1.5:
    Pair p = new Pair<Integer, String>(1, "Scala");


   in Scala:
   val p = new MyPair(1, "scala");

                                                      14
Scala 특징 2/3
  가벼운 문법(lightweight syntax)
  – Anonymous functions
  – Higher-order functions
  – Nested Functions
  – Currying
  ML-style의 Pattern Matching
  XML과 통합
  – Scala 프로그램에서 XML을 직접 쓸 수 있다.
  – Scala 클래스 정의로 XML DTD로 변환할 수 있다.
  정규식 패턴
  DSLs
  Concurrency via actors

                                       15
Scala 특징 3/3 – Scala는 실용적이다.

 Java를 대체해서 사용이 가능함
   Java와 Scala를 혼합한 프로젝트가 가능


 기존 Java 라이브러리를 활용가능

 기존 Java 툴(Ant, Maven, Junit, etc…)을 활용가능

 기존 IDE 환경을 지원 (Eclipse, NetBeans, IntelliJ)



                                                16
Scala compared to Java
                            Scala adds                                    Scala removes

     + a pure object system                                 - static members

     + operator overloading                                 - primitive Types

     + closures                                             - break, continue

     + mixin composition with traits                        - special treatment of interfaces

     + existential types                                    - wildcards

     + abstract types                                       - raw types

     + pattern matching                                     - enums


     Modeled in libiaries:
     assert, enums, properties, events, actors, using, queries, …

출처: http://www.slideshare.net/Odersky/fosdem-2009-1013261                                       17
Scala는 간결하다.
object Hello {
  def main(args:Array[String]) {
     println(“Hello, World”)
  }
}
                                   18
Type Interface

 val sum = 1 + 2 + 3
 >sum: Int = 6

 val nums = List(1, 2, 3)
 >nums: List[Int] = List(1, 2, 3)

 val map = Map("abc" -> List(1,2,3))
 >map:
   scala.collection.immutable.Map[java.la
   ng.String,List[Int]] = Map(abc ->
   List(1, 2, 3))

                                            19
Explicit Types



 val sum: Int = 1 + 2 + 3


 val nums: List[Int] = List(1, 2, 3)


 val map: Map[String, List[Int]] = ...

                                         20
Higher Level
 // Java – Check if string has uppercase character
 boolean hasUpperCase = false;
 for(int i = 0; i < name.length(); i++) {
     if(Character.isUpperCase(name.charAt(i))) {
         hasUpperCase = true;
        break;
     }
 }


 // Scala
 val hasUpperCase = name.exists(_.isUpperCase)

                                                     21
Less Boilerplate
                  Java                                    Scala
  public class Person {                     class Person(var name: String,
    private String name;                                 var age: Int)
    private int age;
    public Person(String name, int age) {
       this.name = name;
       this.age = age;                      class Person(var name: String,
    }                                                    private var _age: Int){
    public String getName() {
                                              def age = _age           // Getter
      return name;
                                              def age_=(newAge:Int) { // Setter
    }
    public int getAge() {                       _age = newAge
      return age;                             }
    }                                       }
    public void setName(String name) {
      this.name = name;
    }
    public void setAge(int age) {
      this.age = age;
    }
  }
                                                                                   22
Variables and Values

 // variable
 var foo = "foo"
 foo = "bar" // okay

 // value
 val bar = "bar"
 bar = "foo" // nope

                       23
Scala는 객체지향 언어이다.
object Hello {
  def main(args:Array[String]) {
     println(“Hello, World”)
  }
}
                                   24
Class Hierarchy




출처: https://docs.google.com/document/pub?id=1kSNKKKwM8rjGhn9Gnw-6Q0VCImpwSRZ7_QzwNwXgMxM   25
Pure O.O.
  // Every value is an object
  1.toString

  // Every operation is a method call
  1 + 2 + 3  (1).+(2).+(3)

  // Can omit . and ( )
  "abc" charAt 1  "abc".charAt(1)



                                        26
Classes

 // Classes (and abstract classes) like Java
 abstract class Language(val name:String) {
   override def toString = name
 }

 // Example implementations
 class Scala extends Language("Scala")

 // Anonymous class
 val scala = new Language("Scala") { /* empty */ }


                                                     27
Case Classes
 case class Person(firstName: String = "Jamie", lastName: String = "Allen")
 val jamieDoe = Person(lastName = "Doe") // Person(Jamie,Doe)

 abstract class Tree
 case class Sum(l: Tree, r: Tree) extends Tree
 case class Var(n: String) extends Tree
 case class Const(v: Int) extends Tree
 …
 type Environment = String => Int
 def eval(t: Tree, env: Environment): Int = t match {
   case Sum(l, r) => eval(l, env) + eval(r, env)
   case Var(n) => env(n)
   case Const(v) => v
 }


    Data Transfer Objects (DTOs) done right
    By default, class arguments are immutable & public
    Should never be extended
    Provide equals(), copy(), hashCode() and toString() implementations
    Don’t have to use new keyword to create instances
    Named Parameters and Default arguments give us Builder pattern semantics
                                                                                28
Traits
  // Like interfaces in Java
  trait Language {

      val name:String

      // But allow implementation
      override def toString = name
  }




                                     29
Traits
trait JVM {
  override def toString = super.toString+" runs on JVM"     }
trait Static {
  override def toString = super.toString+" is Static"   }

// Traits are stackable
class Scala extends Language with JVM with Static {
  val name = "Scala"
}

println(new Scala)  "Scala runs on JVM is Static"


                                                                30
Objects
  object Bootstrapper extends App { Person.createJamieAllen }

  object Person {
    def createJamieAllen = new Person("Jamie", "Allen")
    def createJamieDoe = new Person("Jamie", "Doe")
    val aConstantValue = "A constant value”
  }

  class Person(val firstName: String, val lastName: String)




  Singletons within a JVM process
  No private constructor histrionics
  Companion Objects, used for factories and constants


                                                                31
Tuples
  def getNameAndAge: Tuple2[String, Int] = {
    val name = “Steve”
    val age = 30
    (name, age)
  }

  val (name, age) = getNameAndAge
  println(“Name: “+ name)   // Name: Steve
  println(“Age: “ + age)    // Age: 30

  val scalet = (“Scalet”, 27, “Seoul”)    // scalet: (String, Int, String) = (“Scalet”, 27, “Seoul”)
  println(“Address: “ + scalet._3)       // Address: Seoul



  Tuple is a generalization of that: a collection of two,
   three, four, and more values
  Tuple can hold objects with different types but they
   are also immutable
                                                                                                       32
Scala는 함수형 언어이다.
object Hello {
  def main(args:Array[String]) {
     println(“Hello, World”)
  }
}
                                   33
g(f(X))




          34
Imperative Style vs. Functional Style
                Characteristic                              Imperative                  Functional

                                                                                What information is
                                                   How to perform tasks
                                                                                desired and what
     Programmer focus                              (algorithms) and how to
                                                                                transformations are
                                                   track changes in state.
                                                                                required.

     State Changes                                 Important.                   Non-existent.

     Order of execution                            Important.                   Low importance.

                                                   Loops, conditionals, and     Function calls, including
     Primary flow control
                                                   function (method) calls      recursion.

                                                                                Functions as first-class
                                                   Instances of structures or
     Primary manipulation unit                                                  objects and data
                                                   classes.
                                                                                collections.


출처: http://msdn.microsoft.com/en-us/library/bb669144.aspx                                                   35
Function Type + Function Literal

 // Type
 (Int, Int)=>Int



 // Literal
 (x:Int, y:Int) => x + y


                                   36
First Class Functions
      // Lightweight anonymous functions
      (x:Int, y:Int) => x + y

      // Calling the anonymous function
      val add = (x:Int, y:Int) => x + y
       Function name                 Argument List      Function expression


      val add : (Int, Int) => Int = (x:Int, y:Int) => x + y
      val add : (Int, Int) => Int = _ + _
      val add = new Function2[Int, Int, Int] {
        def apply(x:Int, y:Int):Int = x + y
      }
                                                        어떤 함수가 다음 조건을 만족하면 일급 함수로 간주한다.

      add(5,1)  6                                      1.   변수나 데이터 구조안에 담을 수 있다.
      add.apply(5,1)  6                                2.
                                                        3.
                                                             파라미터로 전달 할 수 있다.
                                                             반환값(return value)으로 사용할 수 있다.
                                                        4.   할당에 사용된 이름과 관계없이 고유한 구별이 가능하다.
출처: http://en.wikipedia.org/wiki/First-class_function                                         37
Closures
 // plusFoo can reference any values/variables in scope
 var foo = 1
 val plusFoo = (x:Int) => x + foo

 plusFoo(5)    6

 // Changing foo changes the return value of plusFoo
 foo = 5
 plusFoo(5)  10

                                                          38
Pattern Matching
 def what(any:Any) = any match {
   case i:Int => "It's an Int"
   case s:String => "It's a String"
   case _ => "I don't know what it is"
 }

 what(123)        "It's an Int"
 what("hello")    "It's a String"
 what(false)      "I don't know what it is"


                                               39
Pattern Matching
 val nums = List(1,2,3)

 // Pattern matching to create 3 vals
 val List(a,b,c) = nums

 a    1
 b    2
 c    3



                                        40
Immutable Types
 // Immutable types by default
 var nums = Set(1,2,3)
 nums += 4  nums = nums.+(4)

 // Mutable types available
 import scala.collection.mutable._

 val nums = Set(1,2,3)
 nums += 4  nums.+=(4)


                                     41
Referential Transparency
 // Transparent
 val example1 = "jamie".reverse
 val example2 = example1.reverse
 println(example1 + example2) // eimajjamie

 // Opaque
 val example1 = new StringBuffer("Jamie").reverse
 val example2 = example1.reverse
 println(example1 append example2) // jamiejamie



   An expression is transparent if it can be replaced by its
    VALUE without changing the behavior of the program
   In math, all functions are referentially transparent

                                                                42
Scala Collections

 val   myMap = Map(1 -> "one", 2 -> "two", 3 -> "three")
 val   mySet = Set(1, 4, 2, 8)
 val   myList = List(1, 2, 8, 3, 3, 4)
 val   myVector = Vector(1, 2, 3...)



  You have the choice of mutable or immutable
   collection instances, immutable by default
  Rich implementations, extremely flexible



                                                           43
Rich Collection Functionality

 val numbers = 1 to 20 // Range(1, 2, 3, ... 20)

 numbers.head           //   Int = 1
 numbers.tail           //   Range(2, 3, 4, ... 20)
 numbers.take(5)        //   Range(1, 2, 3, 4, 5)
 numbers.drop(5)        //   Range(6, 7, 8, ... 20)

  There are many methods available to you in the
   Scala collections library
  Spend 5 minutes every day going over the
   ScalaDoc for one collection class


                                                      44
scala.collection




                   45
scala.collection.immutable




                             46
scala.collection.mutable




                           47
Or Use Existing Java Collections
 java.util
 Apache Commons Collections
 fastutil
 Trove
 Google Collections

 scala.collection.JavaConversion available to convert to
  and from java.util Interfaces



                                                            48
Higher Order Functions
 val plusOne = (x:Int) => x + 1
 val nums = List(1,2,3)

 // map takes a function: Int => T
 nums.map(plusOne)        List(2,3,4)

 // Inline Anonymous
 nums.map(x => x + 1)  List(2,3,4)

 // Short form
 nums.map(_ + 1)        List(2,3,4)




                                         49
Higher Order Functions
 val nums = List(1,2,3,4)
 // A few more examples for List class
 nums.exists(_ == 2)        true
 nums.find(_ == 2)          Some(2)
 nums.indexWhere(_ == 2)  1
 nums.reduceLeft(_ + _)     10
 nums.foldLeft(100)(_ + _)  110
 nums.filter(_ % 2 == 0)  List(2,4)

 // Many more in collections library
 flatMap, flatten, zip, fold, foldRight, reduce, reduceRight, groupBy
 partition, slice, take, takeRight, takeWhile, drop, dropRight, dropWhile, splitAt
 sortBy, sortWith, startsWith, endsWith, product, sum …



                                                                                     50
Higher Order Functions

 // functions as parameters
 def call(f: Int => Int) = f(1)



 call(plusOne)     2
 call(x => x + 1)  2
 call(_ + 1)     2


                                  51
Higher Order Functions
 // functions as parameters
 def each(xs: List[Int], fun: Int => Unit) {
   if(!xs.isEmpty) {
     fun(xs.head)
     each(xs.tail, fun)
   }
 }

 each(List(1,2,3), println)
     1
     2
     3
                                               52
Higher Order Functions
  class List[+A] … {
    def head: A
    def tail: List[A]
    …
  }
 // More complex example with generics & pattern matching
 @tailrec
 def each[T](xs: List[T], fun: T => Unit): Unit = xs match {
   case Nil =>
   case head :: tail => fun(head); each(tail, fun)
 }
 each(List(1,2), println)
     1
     2
 each(List("foo", "bar"), println)
     foo
     bar

                                                               53
Parallel Collections
 scala> 1 to 1000000
 res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3,...

 scala> res0.par
 res1: s.c.parallel.immutable.ParRange = ParRange(1, 2, 3,...

 scala> res1 map(_ + 1)
 res2: s.c.parallel.immutable.ParSeq[Int] = ParVector(2, 3, 4,...

 scala> res2.seq
 res3: s.c.immutable.Range = Range(2, 3, 4,...

   You can easily parallelize the application of a function literal to your collection by
    calling the par() method on a collection instance
   Uses JSR166 under the covers to fork/join for you
   Use the seq() method on the parallel collection to return to a non-parallel
    instance



                                                                                             54
Partial Functions
 val sample = 1 to 10
 val isEven: PartialFunction[Int, String] = {
   case x if x % 2 == 0 => x+" is even"
 }
 val isEven= new PartialFunction[Int, String] {
   override def apply(x:Int):String = x+" is even"
   override def isDefinedAt(x:Int):Boolean = x % 2 == 0
 }
 // the method collect can use isDefinedAt to select which members to collect
 val evenNumbers = sample collect isEven
 > evenNumbers = Vector(2 is even, 4 is even, 6 is even, 8 is even, 10 is even)

 val isOdd: PartialFunction[Int, String] = {
   case x if x % 2 == 1 => x+" is odd"
 }
 // the method orElse allows chaining another partial function to handle
 // input outside the declared domain
 val numbers = sample map (isEven orElse isOdd)
 > numbers = Vector(1 is odd, 2 is even, 3 is odd, 4 is even, 5 is odd, 6 is even, 7 is o
 dd, 8 is even, 9 is odd, 10 is even)
                                                                                            55
Currying
 // multiple arguments
 def add(x:Int, y:Int) = x + y
 add(1, 2) // 3
 add(7, 3) // 10

 // after currying, single arguments
 def add(x:Int) = (y:Int) => x + y
 def add(x:Int)(y:Int) = x + y
 add(1)(2) // 3
 add(7)(3) // 10
   In computer science, currying, invented by Moses Schönfinkel and
    Gottlob Frege, is the technique of transforming a function that takes
    multiple arguments into a function that takes a single argument (the
    other arguments having been specified by the curry).



                                                                            56
Partially Applied Functions
 def justSum(a:Int,b:Int,c:Int) = a+b+c
 val partial1 = justSum _
 val partial2 = justSum(1, _:Int, 8)

 def curriedSum(a:Int)(b:Int)(c:Int) = a+b+c



 println("Partial 1: "+partial1(5,6,7))   // Partial 1: 18
 println("Partial 2: "+partial2(4))       // Partial 2: 13


                                                             57
(엄밀히 말하자면 동적 언어는 아니지만, 동적 언어에서
         제공하는 많은 특징을 가지고 있다.)


   Scala는 동적 언어이다.
object Hello {
  def main(args:Array[String]) {
     println(“Hello, World”)
  }
}
                                    58
Scriptable

 // HelloWorld.scala
 println("Hello World")

 bash$ scala HelloWorld.scala
 Hello World

 bash$ scala -e 'println("Hello World")'
 Hello World

                                           59
Read-Eval-Print Loop
 bash$ scala
 Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM,
  Java 1.6.0_22).
 Type in expressions to have them evaluated.
 Type :help for more information.

 scala> class Foo { def bar = "baz" }
 defined class Foo

 scala> val f = new Foo
 f: Foo = Foo@51707653

 scala> f.bar
 res2: java.lang.String = baz

                                                                            60
Structural Typing – Type safe Duck Typing
     // Duck Typing (other language)
     def test(o)
      log o.getName # Let's hope this will work
     end

     // Type safe Duck Typing
     def doTalk(any:{def talk:String}) {
       println(any.talk)
     }
     class Duck { def talk = "Quack" }
     class Dog { def talk = "Bark" }

     doTalk(new Duck)                             "Quack"
     doTalk(new Dog)                              "Bark"

출처: http://beust.com/weblog/2008/02/11/structural-typing-vs-duck-typing/   61
Implicit Conversions
 // Extend existing classes in a type safe way

 // Goal: Add isBlank method to String class
 class RichString(s:String) {
   def isBlank = null == s || "" == s.trim
 }

 implicit def toRichString(s:String) = new RichString(s)



 // Our isBlank method is now available on Strings
 " ".isBlank     true
 "foo".isBlank  false


                                                           62
Implicit Conversions
 // Does not type check
 "abc".isBlank

 // Search in-scope implicits defs that take a
 // String & return a type with an isBlank method
 implicit def toRichString(s:String):RichString



 // Resulting code that type checks
 new RichString("abc").isBlank



                                                    63
method_missing (Scala 2.9 Feature)
 // Dynamic is a marker trait used by the compiler
 class Foo extends Dynamic {
   def typed[T] = error("not implemented")

     def applyDynamic(name:String)(args:Any*) = {
       println("called: "+name+"("+args.mkString(",")+")")
     }

 }

 val f = new Foo
 f.helloWorld          called: helloWorld()
 f.hello("world")      called: hello(world)
 f.bar(1,2,3)          called: bar(1,2,3)

                                                             64
Scala는 정적으로 타입화된 언어이다.
  object Hello {
    def main(args:Array[String]) {
       println(“Hello, World”)
    }
  }
                                     65
Generic Classes
     class Stack[T] {
       var elems: List[T] = Nil
       def push(x: T) { elems = x :: elems }
       def top: T = elems.head
       def pop() { elems = elems.tail }
     }
     object GenericsTest extends Application {
       val stack = new Stack[Int]
       stack.push(1)
       stack.push('a')
       println(stack.top) // 97
       stack.pop()
       println(stack.top) // 1
     }




출처: http://www.scala-lang.org/node/113           66
Variances
             구분               Scala         Java                                          Description

        Covariant              +T        ? extends T   Covariant subclassing. E.g., List[Tsub] is a subtype of List[T].

      Contravariant            -T         ? super T    Contravariant subclassing. E.g., X[Tsup] is a subtype of X[T].

         Invariant              T            T         Invariant subclassing. E.g., Can’t substitute Y[Tsup]or Y[Tsub] for Y[T].

     class   Animal
     class   Dog extends Animal
     class   Vector[+T] { … }
     class   Contravariant[-T] {…}
     class   Array[T] {…}

     val aCovariantVector: Vector[Animal] = Vector.empty[Animal]      // ok
     val aCovariantVector: Vector[Animal] = Vector.empty[Dog]         // ok
     val aCovariantVector: Vector[Dog] = Vector.empty[Animal]         // not ok

     val contravariantClass: Contravariant[Animal] = new Contravariant[Animal] // ok
     val contravariantClass: Contravariant[Dog] = new Contravariant[Animal]    // ok
     val contravariantClass: Contravariant[Animal] = new Contravariant[Dog]    // not ok

     val anInvariantArray: Array[Animal] = Array.empty[Animal]     // ok
     val anInvariantArray: Array[Animal] = Array.empty[Dog]        // not ok
     val anInvariantArray: Array[Dog] = Array.empty[Animal]        // not ok


출처: http://www.scala-lang.org/node/129                                                                                             67
Type Bounds (Upper, Lower)
                구분                       Scala                   Java                                     Description

          Upper Bounds                   A <: B              A extends B                          A must be a subtype of B

          Lower Bounds                   A >: B                    -                             A must be a supertype of B


     object Array {
       ...
       def apply[A <: AnyRef](xs: A*): Array[A] = {            // A must be a subtype of AnyRef
           val array = new Array[A](xs.length)
           var i = 0
           for (x <- xs.elements) { array(i) = x; i += 1 }
           array
       }
       ...
     }
     class List[+A] {                                          // A is covariant, List[+A] is subtype of List[A]
       ...
       def ::[B >: A](x : B) : List[B] = new scala.::(x, this)  // B must be a supertype of A
       ...
     }
     val languages = List("Scala", "Java", "Ruby", "C#", "C++", "Python") // languages:List[String] = List(…)
     val list = 3.14 :: languages                                              // list:List[Any] = List (…)
     println(list)                                                             // List(3.14, “Scala”, “Java”, “Ruby”, “C#”, “C++”, “Python”)

출처: http://www.scala-lang.org/node/136                                                                                                         68
Views
     Implicit parameters and methods can also define implicit conversions called views.
     The term view conveys the sense of having a view from one type (A) to another type (B).

     A view is applied in two circumstances.
      When a type A is used in a context where another type B is expected and there is a view in scope that can convert A to B.
      When a non-existent member m of a type A is referenced, but there is an in-scope view that can convert A to a B that has
       the m member.

     def m [A <% B](arglist): R = ...
     def m [A](arglist)(implicit viewAB: A => B): R = ...

     class Container[A <% Int] { def addIt(x: A) = 123 + x }
     (new Container[String]).addIt("123")    // ok, 246
     (new Container[Int]).addIt(123)        // ok, 246
     (new Container[Float]).addIt(123.2F) // not ok




                구분                        Scala                Java                             Description

           View Bounds                   A <% B                 -                   A can be converted to B using a view




출처: http://www.scala-lang.org/node/130                                                                                             69
Inner Classes
      class Graph {                                                                                     Use Companion Objects
        class Node {
          var connectedNodes: List[Node] = Nil                                        object Graph {
          def connectTo(node: Node) {                                                   class Node {
            if (connectedNodes.find(node.equals).isEmpty) {                                var connectedNodes: List[Node] = Nil
              connectedNodes = node :: connectedNodes                                      def connectTo(node: Node) {
            }                                                                                if (connectedNodes.find(node.equals).isEmpty) {
          }                                                                                    connectedNodes = node :: connectedNodes
        }                                                                                    }
        var nodes: List[Node] = Nil                                                        }
        def newNode: Node = {                                                            }
          val res = new Node                                                          }
          nodes = res :: nodes                                                        class Graph {
          res                                                                            var nodes: List[Graph.Node] = Nil
        }                                                                                def newNode: Node = {
      }                                                                                    val res = new Node
                                                                                           nodes = res :: nodes
      object IllegalGraphTest extends Application {                                        res
        val g: Graph = new Graph                                                         }
        val n1: g.Node = g.newNode                                                    }
        val n2: g.Node = g.newNode
        n1.connectTo(n2)     // legal
        val h: Graph = new Graph
        val n3: h.Node = h.newNode                                                                         Use Type Projection
        n1.connectTo(n3)     // illegal!                                              class Graph {
      }                                                                                 class Node {
                                                                                          var connectedNodes: List[Graph#Node] = Nil
                                                                                          def connectTo(node: Graph#Node) {
                                                                                            if (connectedNodes.find(node.equals).isEmpty) {
                                                                                              connectedNodes = node :: connectedNodes
                                                                                            }
                                                                                          }
                                                                                        }
      1. Use Companion Objects: object Graph for class Graph                            …
      2. Use Type Projection: Graph#Node is “a Node of any Graph”                     }

출처: http://www.scala-lang.org/node/115, http://weblogs.java.net/blog/cayhorstmann/archive/2011/08/05/inner-classes-scala-and-java              70
Abstract Types
     abstract class Buffer[+T] {
       val element: T
     }

     abstract class SeqBuffer[U, +T <: Seq[U]] extends Buffer[T] {
       def length = element.length
     }

     object AbstractTypeTest2 extends Application {
      def newIntSeqBuf(e1: Int, e2: Int): SeqBuffer[Int, Seq[Int]] =
        new SeqBuffer[Int, List[Int]] {
          val element = List(e1, e2)
        }

         val buf = newIntSeqBuf(7, 8)
         println("length = " + buf.length)     // length = 2
         println("content = " + buf.element)   // content = List(7, 8)
     }




출처: http://www.scala-lang.org/node/105                                   71
Explicitly Typed Self References
     abstract class SubjectObserver {
      type S <: Subject                                                  // S must be a subtype of Subject
      type O <: Observer                                                 // O must be a subtype of Observer

         trait Subject {
           self: S =>                                                      // #1.   val self: SubjectObserver.this.S
           private var observers = List[O]()
           def addObserver(observer: O) = observers ::= observer
           def notifyObservers = observers foreach (_.receiveUpdate(self)) // #2
         }

         trait Observer {
           def receiveUpdate(subject: S)
         }
     }

     - Comment #1 shows the self-type annotation, self: S =>. We can now use self as an alias for this, but whenever it appears, the
        type will be assumed to be S, not Subject.

     - Comment #2 shows that we pass self instead of this to receiveUpdate.




출처: http://www.scala-lang.org/node/124                                                                                                 72
Polymorphic Methods
     object PolyTest extends Application {
       def dup[T](x: T, n: Int): List[T] =
        if (n == 0) Nil
        else x :: dup(x, n - 1)
       println(dup[Int](3, 4))      // required parameters
       println(dup("three", 3)) // not required to give actual type parameters explicitly
     }



     Method dup in object PolyTest is parameterized with type T and with the value parameters x:
     T and n: Int




출처: http://www.scala-lang.org/node/121                                                             73
Scala는 많은 좋은 기능이 있다.
object Hello {
  def main(args:Array[String]) {
     println(“Hello, World”)
  }
}
                                   74
Default Parameter Values


 def hello(foo:Int = 0, bar:Int = 0) {
   println("foo: "+foo+" bar: "+bar)
 }

 hello()             foo: 0   bar: 0
 hello(1)            foo: 1   bar: 0
 hello(1,2)          foo: 1   bar: 2

                                         75
Named Parameters


 def hello(foo:Int = 0, bar:Int = 0) {
   println("foo: "+foo+" bar: "+bar)
 }

 hello(bar=6)          foo: 0   bar: 6
 hello(foo=7)          foo: 7   bar: 0
 hello(foo=8,bar=9)    foo: 8   bar: 9

                                          76
Everything Returns a Value
 val a = if(true) "yes" else "no"

 val b = try{
   "foo"
 } catch {
   case _ => "error"
 }

 val c = {
   println("hello")
   "foo"
 }
                                    77
Lazy Definitions
  // initialized on first access
  lazy val foo = {
    println("init")
    "bar"
  }

  foo      init
  foo   
  foo   


                                   78
Nested Functions
 // Can nest multiple levels of functions
 def outer() {
     var msg = "foo"
     def one() {
         def two() {
             def three() {
                 println(msg)
             }
             three()
         }
         two()
     }
     one()
 }

                                            79
By-Name Parameters
 // msg parameter automatically wrapped in closure
 def log(doLog:Boolean, msg: => String) {
   if(doLog) {
     msg // evaluates msg
     msg // evaluates msg again!
   }
 }

 def foo:String = {
   println("in foo"); "Foo"
 }

 log(true, foo+" Bar")    // foo called twice
      in foo
      in foo

 log(false, foo+" Bar")   // foo never called


                                                     80
Many More Features
  Annotations  @foo def hello = "world"
  Option
  Actors
  DSL
  For Comprehensions  for(i <- 1 to 5 if i % 2 == 0) yield i
  Implicit Conversion
  XML Literals  val node = <hello>world</hello>
  Reflections
  Macros (2.10)
  etc…



                                                                 81
Scala를 어떻게 공부하지?
object Hello {
  def main(args:Array[String]) {
     println(“Hello, World”)
  }
}
                                   82
Scala 공부의 정석




               83
Martin Ordersky 저자 강의




          https://www.coursera.org/course/progfun   84
Typesafe




           http://www.typesafe.com   85
Scala-Lang.org




                 http://www.scala-lang.org/   86
Scala 공부관련 참고자료
  스칼라 입문자료
  – Scala API Reference
  – A Tour of Scala : 스칼라 공식 블로그에 올라온 스칼라 강좌
  – Scala By Example : 마틴 오더스키가 작성한 100페이지짜리 스칼라 문서
  – Scala School : 트위터가 작성한 스칼라 강좌
  – Progammin in Scala 1st Edition : Programmin in Scala의 1st Edition은 온라인에서 무료로 볼
    수 있음.
  – Effective Scala : 트위터의 Marius Eriksen가 만든 스칼라 강좌

  스칼라 서적(영문)
  – Programming in Scala : 마틴 오더스키가 참여한 스칼라 서적. 강추!!, Scala 2.8 기준.
  – Programming Scala : 오렐리에서 나옴. 읽어본 사람 없어서 내용은 잘 모름.
  – Programming Scala : Pragmatic에서 나온 스칼라 서적. 읽어볼 만은 하지만 빠진 내용이 많아
    서 많이 추천하지는 않음.
  – Scala in Depth : 매닝에서 나온 서적. 읽어본 사람이 없어서 내용은 잘 모름.

  그외 참고 자료
  –   Style Guide for Scala : 스칼라 코드 작성에 대한 스타일가이드 발표자료
  –   Scala Puzzlers : 스칼라 문제를 풀어볼 수 있는 사이트
  –   Scala Cheat Sheet : 간단한 스칼라문법을 참고할 수 있는 치트시트
  –   S99 Scala Problems: 99 개의 스칼라 프로그래밍 학습 예제


                                                                                     87

Weitere ähnliche Inhalte

Was ist angesagt?

Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOPAnton Keks
 
An Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional ParadigmsAn Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional ParadigmsMiles Sabin
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: BasicsAnton Keks
 
Scala Reflection & Runtime MetaProgramming
Scala Reflection & Runtime MetaProgrammingScala Reflection & Runtime MetaProgramming
Scala Reflection & Runtime MetaProgrammingMeir Maor
 
[A4]de view2012 scala-michinisougu
[A4]de view2012 scala-michinisougu[A4]de view2012 scala-michinisougu
[A4]de view2012 scala-michinisouguNAVER D2
 
10 Things I Hate About Scala
10 Things I Hate About Scala10 Things I Hate About Scala
10 Things I Hate About ScalaMeir Maor
 
Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Mario Camou Riveroll
 
Clojure - An Introduction for Java Programmers
Clojure - An Introduction for Java ProgrammersClojure - An Introduction for Java Programmers
Clojure - An Introduction for Java Programmerselliando dias
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Yardena Meymann
 
Clojure talk at Münster JUG
Clojure talk at Münster JUGClojure talk at Münster JUG
Clojure talk at Münster JUGAlex Ott
 
Practical type mining in Scala
Practical type mining in ScalaPractical type mining in Scala
Practical type mining in ScalaRose Toomey
 

Was ist angesagt? (17)

Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOP
 
An Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional ParadigmsAn Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional Paradigms
 
Scala basic
Scala basicScala basic
Scala basic
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: Basics
 
Scala Reflection & Runtime MetaProgramming
Scala Reflection & Runtime MetaProgrammingScala Reflection & Runtime MetaProgramming
Scala Reflection & Runtime MetaProgramming
 
[A4]de view2012 scala-michinisougu
[A4]de view2012 scala-michinisougu[A4]de view2012 scala-michinisougu
[A4]de view2012 scala-michinisougu
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
10 Things I Hate About Scala
10 Things I Hate About Scala10 Things I Hate About Scala
10 Things I Hate About Scala
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Clojure - An Introduction for Java Programmers
Clojure - An Introduction for Java ProgrammersClojure - An Introduction for Java Programmers
Clojure - An Introduction for Java Programmers
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008
 
Clojure talk at Münster JUG
Clojure talk at Münster JUGClojure talk at Münster JUG
Clojure talk at Münster JUG
 
Practical type mining in Scala
Practical type mining in ScalaPractical type mining in Scala
Practical type mining in Scala
 
Runtime
RuntimeRuntime
Runtime
 

Andere mochten auch

Apache Spark Overview part1 (20161107)
Apache Spark Overview part1 (20161107)Apache Spark Overview part1 (20161107)
Apache Spark Overview part1 (20161107)Steve Min
 
Hadoop과 SQL-on-Hadoop (A short intro to Hadoop and SQL-on-Hadoop)
Hadoop과 SQL-on-Hadoop (A short intro to Hadoop and SQL-on-Hadoop)Hadoop과 SQL-on-Hadoop (A short intro to Hadoop and SQL-on-Hadoop)
Hadoop과 SQL-on-Hadoop (A short intro to Hadoop and SQL-on-Hadoop)Matthew (정재화)
 
[SSA] 04.sql on hadoop(2014.02.05)
[SSA] 04.sql on hadoop(2014.02.05)[SSA] 04.sql on hadoop(2014.02.05)
[SSA] 04.sql on hadoop(2014.02.05)Steve Min
 
NewSQL Database Overview
NewSQL Database OverviewNewSQL Database Overview
NewSQL Database OverviewSteve Min
 
Apache Spark Overview part2 (20161117)
Apache Spark Overview part2 (20161117)Apache Spark Overview part2 (20161117)
Apache Spark Overview part2 (20161117)Steve Min
 
Apache Htrace overview (20160520)
Apache Htrace overview (20160520)Apache Htrace overview (20160520)
Apache Htrace overview (20160520)Steve Min
 
빅데이터_ISP수업
빅데이터_ISP수업빅데이터_ISP수업
빅데이터_ISP수업jrim Choi
 
An introduction to hadoop
An introduction to hadoopAn introduction to hadoop
An introduction to hadoopMinJae Kang
 
Cloud Music v1.0
Cloud Music v1.0Cloud Music v1.0
Cloud Music v1.0Steve Min
 
Cloud Computing v1.0
Cloud Computing v1.0Cloud Computing v1.0
Cloud Computing v1.0Steve Min
 
[SSA] 03.newsql database (2014.02.05)
[SSA] 03.newsql database (2014.02.05)[SSA] 03.newsql database (2014.02.05)
[SSA] 03.newsql database (2014.02.05)Steve Min
 
Expanding Your Data Warehouse with Tajo
Expanding Your Data Warehouse with TajoExpanding Your Data Warehouse with Tajo
Expanding Your Data Warehouse with TajoMatthew (정재화)
 
[SSA] 01.bigdata database technology (2014.02.05)
[SSA] 01.bigdata database technology (2014.02.05)[SSA] 01.bigdata database technology (2014.02.05)
[SSA] 01.bigdata database technology (2014.02.05)Steve Min
 
Gruter TECHDAY 2014 MelOn BigData
Gruter TECHDAY 2014 MelOn BigDataGruter TECHDAY 2014 MelOn BigData
Gruter TECHDAY 2014 MelOn BigDataGruter
 

Andere mochten auch (18)

Apache Spark Overview part1 (20161107)
Apache Spark Overview part1 (20161107)Apache Spark Overview part1 (20161107)
Apache Spark Overview part1 (20161107)
 
vertica_tmp_4.5
vertica_tmp_4.5vertica_tmp_4.5
vertica_tmp_4.5
 
Hadoop과 SQL-on-Hadoop (A short intro to Hadoop and SQL-on-Hadoop)
Hadoop과 SQL-on-Hadoop (A short intro to Hadoop and SQL-on-Hadoop)Hadoop과 SQL-on-Hadoop (A short intro to Hadoop and SQL-on-Hadoop)
Hadoop과 SQL-on-Hadoop (A short intro to Hadoop and SQL-on-Hadoop)
 
[SSA] 04.sql on hadoop(2014.02.05)
[SSA] 04.sql on hadoop(2014.02.05)[SSA] 04.sql on hadoop(2014.02.05)
[SSA] 04.sql on hadoop(2014.02.05)
 
BigData, Hadoop과 Node.js
BigData, Hadoop과 Node.jsBigData, Hadoop과 Node.js
BigData, Hadoop과 Node.js
 
NewSQL Database Overview
NewSQL Database OverviewNewSQL Database Overview
NewSQL Database Overview
 
Apache Spark Overview part2 (20161117)
Apache Spark Overview part2 (20161117)Apache Spark Overview part2 (20161117)
Apache Spark Overview part2 (20161117)
 
Apache Htrace overview (20160520)
Apache Htrace overview (20160520)Apache Htrace overview (20160520)
Apache Htrace overview (20160520)
 
빅데이터_ISP수업
빅데이터_ISP수업빅데이터_ISP수업
빅데이터_ISP수업
 
An introduction to hadoop
An introduction to hadoopAn introduction to hadoop
An introduction to hadoop
 
Html5 video
Html5 videoHtml5 video
Html5 video
 
Cloud Music v1.0
Cloud Music v1.0Cloud Music v1.0
Cloud Music v1.0
 
Cloud Computing v1.0
Cloud Computing v1.0Cloud Computing v1.0
Cloud Computing v1.0
 
[SSA] 03.newsql database (2014.02.05)
[SSA] 03.newsql database (2014.02.05)[SSA] 03.newsql database (2014.02.05)
[SSA] 03.newsql database (2014.02.05)
 
RESTful API Design, Second Edition
RESTful API Design, Second EditionRESTful API Design, Second Edition
RESTful API Design, Second Edition
 
Expanding Your Data Warehouse with Tajo
Expanding Your Data Warehouse with TajoExpanding Your Data Warehouse with Tajo
Expanding Your Data Warehouse with Tajo
 
[SSA] 01.bigdata database technology (2014.02.05)
[SSA] 01.bigdata database technology (2014.02.05)[SSA] 01.bigdata database technology (2014.02.05)
[SSA] 01.bigdata database technology (2014.02.05)
 
Gruter TECHDAY 2014 MelOn BigData
Gruter TECHDAY 2014 MelOn BigDataGruter TECHDAY 2014 MelOn BigData
Gruter TECHDAY 2014 MelOn BigData
 

Ähnlich wie Scala overview

Scala, Akka, and Play: An Introduction on Heroku
Scala, Akka, and Play: An Introduction on HerokuScala, Akka, and Play: An Introduction on Heroku
Scala, Akka, and Play: An Introduction on HerokuHavoc Pennington
 
Martin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMartin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMarakana Inc.
 
Scala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistScala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistpmanvi
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With ScalaMeetu Maltiar
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaDerek Chen-Becker
 
Beginning scala 02 15
Beginning scala 02 15Beginning scala 02 15
Beginning scala 02 15lancegatlin
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdfHiroshi Ono
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Languageleague
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java ProgrammersEric Pederson
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersMiles Sabin
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvmIsaias Barroso
 
Scala Days San Francisco
Scala Days San FranciscoScala Days San Francisco
Scala Days San FranciscoMartin Odersky
 

Ähnlich wie Scala overview (20)

Scala, Akka, and Play: An Introduction on Heroku
Scala, Akka, and Play: An Introduction on HerokuScala, Akka, and Play: An Introduction on Heroku
Scala, Akka, and Play: An Introduction on Heroku
 
Martin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMartin Odersky: What's next for Scala
Martin Odersky: What's next for Scala
 
Scala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistScala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologist
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Scala
ScalaScala
Scala
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to Scala
 
Devoxx
DevoxxDevoxx
Devoxx
 
Beginning scala 02 15
Beginning scala 02 15Beginning scala 02 15
Beginning scala 02 15
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
scalaliftoff2009.pdf
scalaliftoff2009.pdfscalaliftoff2009.pdf
scalaliftoff2009.pdf
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Quick introduction to scala
Quick introduction to scalaQuick introduction to scala
Quick introduction to scala
 
Scala for Java Programmers
Scala for Java ProgrammersScala for Java Programmers
Scala for Java Programmers
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java Developers
 
Lecture1
Lecture1Lecture1
Lecture1
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
 
Scala Days San Francisco
Scala Days San FranciscoScala Days San Francisco
Scala Days San Francisco
 

Kürzlich hochgeladen

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Kürzlich hochgeladen (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Scala overview

  • 1. 민형기 S-Core Cloud Computing Lab Data Solution Part 2013. 2. 18.
  • 2. Contents I. Scala란? II. Scala는 간결하다. III. Scala는 객체지향 언어이다. IV. Scala는 함수형 언어이다. V. Scala는 동적 언어이다. VI. Scala는 정적으로 타입화된 언어이다. VII. Scala는 많은 좋은 기능이 있다. VIII.Scala를 어떻게 공부하지? 1
  • 3. Scala 란? object Hello { def main(args:Array[String]) { println(“Hello, World”) } } 2
  • 4. Scala 정의 Scala [skah-lah] = scalable language The language is so named because it was designed to grow with the demands of its users. -Programming In Scala, p39 Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. -scala-lang.com 3
  • 5. Scala에 대한 의견들 If I were to pick a language to use on the JVM today other than Java, it would be Scala. -James Gosling, creater of Java http://www.adam-bien.com/roller/abien/entry/java_net_javaone_which_programming My tip thought for the long term replacement of javac is Scala. I’m very impressed with it! I can honestly say if someone had shown me the Programming in Scala book […] back in 2003. I’d probably have never created Groovy. -James Strachan, creator of Groovy http://macstrac.blogspot.com/2009/04/scala-as-long-term-replacement-for.html Scala, it must be stated, is the current heir apparent to the Java throne. No other language on the JVM seems as capable of being a “replacement for Java” as Scala, and the momentum behind Scala is now uniquestionable. - Charlies Nutter, JRuby lead http://blog.headius.com/2009/04/future-part-one.html 4
  • 6. Scalability in Programming Language 어플리케이션이 사용자의 요구에 맞추기 위해 크기나 용량을 변 경해도, 그 기능이 계속하여 잘 동작 할 수 있는 능력 -Terms 문제 영역의 크기나 복잡도가 증가하여도 유사한 방법으로 문제 해결이 가능한 것. - 빌딩 블록의 수가 증가하면  추상화 / 축약 - 언어의 지원 / 라이브러리 / 프레임 워크 5
  • 7. 언어 확장의 예시 – 분수 (Rational) 규칙: (a, b) 단, b != 0 (a, b) + (c, d) = (ad+bc, bd) (a, b) * (c, d) = (ac, bd) *기타 - Big Integer - Complex number - Vector - Matrix … 6
  • 8. BigInteger vs. BigInt Java Integer와 public BigInteger factorial(BigInteger x) { 유사하지 않음 if ( x == BigInteger.ZERO ) return BigInteger.ONE; else return x.multiply( factorial (x.subtract(BigInteger.ONE))); } Scala def factorial(x: BigInt):BigInt = if (x == 0) 1 else x * factorial(x-1) 출처: http://www.artima.com/scalazine/articles/scalable-language.html 7
  • 12. Scala Best Practices Twitter – Kestrel: queuing – FlockDB(and Gizzard): social graph store – Hawkwind: people search – Hosebird: streaming API – Scalding: MapReduce Wrapper (build on top of Cascading) Guardian: Open Platform – 2009.11: java + guice + guice servlets + apache solr – 2010.01: Integration tests with ScalaTest / maven scala plugin – 2010.03: scala + guice + guice servlets + apache solr – 2010.06: maven -> sbt – 2011.06: scala + lift + apache solr ( 5k loc -> 3.5k) 출처: http://www.slideshare.net/mslinn/scala-adoption-by-enterprises 11
  • 13. Scala 언어 가계도 Simula Lisp objects Prolog functional C Smalltalk programming syntax pattern matching ML C++ Erlang Haskell Java Actors Scala 12
  • 14. Scala를 멀리서 보기 함수형 객체 지향 동적 프로그래밍 프로그래밍 프로그래밍 강화된 타입 시스템 JVM/.Net ※동적 프로그래밍: 스칼라는 엄밀히 말하자면 동적 언어는 아니지만, 동적 언어에서 제공하는 많은 특징을 가지고 있다. (간결함(Concise), REPL, 고수준함수, Duck Typing 등) 13
  • 15. Scala 특징 1/3 Scala는 함수형과 객체지향형의 성격을 다 가지고 있다. –Method를 포함한 모든 함수 (function)은 값(value)이다. –모든 값(value)는 객체(object)이다. Scala는 정적 타입시스템이다. – 강력한 타입 추론 시스템: in Java 1.5: Pair p = new Pair<Integer, String>(1, "Scala"); in Scala: val p = new MyPair(1, "scala"); 14
  • 16. Scala 특징 2/3  가벼운 문법(lightweight syntax) – Anonymous functions – Higher-order functions – Nested Functions – Currying  ML-style의 Pattern Matching  XML과 통합 – Scala 프로그램에서 XML을 직접 쓸 수 있다. – Scala 클래스 정의로 XML DTD로 변환할 수 있다.  정규식 패턴  DSLs  Concurrency via actors 15
  • 17. Scala 특징 3/3 – Scala는 실용적이다. Java를 대체해서 사용이 가능함  Java와 Scala를 혼합한 프로젝트가 가능 기존 Java 라이브러리를 활용가능 기존 Java 툴(Ant, Maven, Junit, etc…)을 활용가능 기존 IDE 환경을 지원 (Eclipse, NetBeans, IntelliJ) 16
  • 18. Scala compared to Java Scala adds Scala removes + a pure object system - static members + operator overloading - primitive Types + closures - break, continue + mixin composition with traits - special treatment of interfaces + existential types - wildcards + abstract types - raw types + pattern matching - enums Modeled in libiaries: assert, enums, properties, events, actors, using, queries, … 출처: http://www.slideshare.net/Odersky/fosdem-2009-1013261 17
  • 19. Scala는 간결하다. object Hello { def main(args:Array[String]) { println(“Hello, World”) } } 18
  • 20. Type Interface val sum = 1 + 2 + 3 >sum: Int = 6 val nums = List(1, 2, 3) >nums: List[Int] = List(1, 2, 3) val map = Map("abc" -> List(1,2,3)) >map: scala.collection.immutable.Map[java.la ng.String,List[Int]] = Map(abc -> List(1, 2, 3)) 19
  • 21. Explicit Types val sum: Int = 1 + 2 + 3 val nums: List[Int] = List(1, 2, 3) val map: Map[String, List[Int]] = ... 20
  • 22. Higher Level // Java – Check if string has uppercase character boolean hasUpperCase = false; for(int i = 0; i < name.length(); i++) { if(Character.isUpperCase(name.charAt(i))) { hasUpperCase = true; break; } } // Scala val hasUpperCase = name.exists(_.isUpperCase) 21
  • 23. Less Boilerplate Java Scala public class Person { class Person(var name: String, private String name; var age: Int) private int age; public Person(String name, int age) { this.name = name; this.age = age; class Person(var name: String, } private var _age: Int){ public String getName() { def age = _age // Getter return name; def age_=(newAge:Int) { // Setter } public int getAge() { _age = newAge return age; } } } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } } 22
  • 24. Variables and Values // variable var foo = "foo" foo = "bar" // okay // value val bar = "bar" bar = "foo" // nope 23
  • 25. Scala는 객체지향 언어이다. object Hello { def main(args:Array[String]) { println(“Hello, World”) } } 24
  • 27. Pure O.O. // Every value is an object 1.toString // Every operation is a method call 1 + 2 + 3  (1).+(2).+(3) // Can omit . and ( ) "abc" charAt 1  "abc".charAt(1) 26
  • 28. Classes // Classes (and abstract classes) like Java abstract class Language(val name:String) { override def toString = name } // Example implementations class Scala extends Language("Scala") // Anonymous class val scala = new Language("Scala") { /* empty */ } 27
  • 29. Case Classes case class Person(firstName: String = "Jamie", lastName: String = "Allen") val jamieDoe = Person(lastName = "Doe") // Person(Jamie,Doe) abstract class Tree case class Sum(l: Tree, r: Tree) extends Tree case class Var(n: String) extends Tree case class Const(v: Int) extends Tree … type Environment = String => Int def eval(t: Tree, env: Environment): Int = t match { case Sum(l, r) => eval(l, env) + eval(r, env) case Var(n) => env(n) case Const(v) => v }  Data Transfer Objects (DTOs) done right  By default, class arguments are immutable & public  Should never be extended  Provide equals(), copy(), hashCode() and toString() implementations  Don’t have to use new keyword to create instances  Named Parameters and Default arguments give us Builder pattern semantics 28
  • 30. Traits // Like interfaces in Java trait Language { val name:String // But allow implementation override def toString = name } 29
  • 31. Traits trait JVM { override def toString = super.toString+" runs on JVM" } trait Static { override def toString = super.toString+" is Static" } // Traits are stackable class Scala extends Language with JVM with Static { val name = "Scala" } println(new Scala)  "Scala runs on JVM is Static" 30
  • 32. Objects object Bootstrapper extends App { Person.createJamieAllen } object Person { def createJamieAllen = new Person("Jamie", "Allen") def createJamieDoe = new Person("Jamie", "Doe") val aConstantValue = "A constant value” } class Person(val firstName: String, val lastName: String) Singletons within a JVM process No private constructor histrionics Companion Objects, used for factories and constants 31
  • 33. Tuples def getNameAndAge: Tuple2[String, Int] = { val name = “Steve” val age = 30 (name, age) } val (name, age) = getNameAndAge println(“Name: “+ name) // Name: Steve println(“Age: “ + age) // Age: 30 val scalet = (“Scalet”, 27, “Seoul”) // scalet: (String, Int, String) = (“Scalet”, 27, “Seoul”) println(“Address: “ + scalet._3) // Address: Seoul Tuple is a generalization of that: a collection of two, three, four, and more values Tuple can hold objects with different types but they are also immutable 32
  • 34. Scala는 함수형 언어이다. object Hello { def main(args:Array[String]) { println(“Hello, World”) } } 33
  • 35. g(f(X)) 34
  • 36. Imperative Style vs. Functional Style Characteristic Imperative Functional What information is How to perform tasks desired and what Programmer focus (algorithms) and how to transformations are track changes in state. required. State Changes Important. Non-existent. Order of execution Important. Low importance. Loops, conditionals, and Function calls, including Primary flow control function (method) calls recursion. Functions as first-class Instances of structures or Primary manipulation unit objects and data classes. collections. 출처: http://msdn.microsoft.com/en-us/library/bb669144.aspx 35
  • 37. Function Type + Function Literal // Type (Int, Int)=>Int // Literal (x:Int, y:Int) => x + y 36
  • 38. First Class Functions // Lightweight anonymous functions (x:Int, y:Int) => x + y // Calling the anonymous function val add = (x:Int, y:Int) => x + y Function name Argument List Function expression val add : (Int, Int) => Int = (x:Int, y:Int) => x + y val add : (Int, Int) => Int = _ + _ val add = new Function2[Int, Int, Int] { def apply(x:Int, y:Int):Int = x + y } 어떤 함수가 다음 조건을 만족하면 일급 함수로 간주한다. add(5,1)  6 1. 변수나 데이터 구조안에 담을 수 있다. add.apply(5,1)  6 2. 3. 파라미터로 전달 할 수 있다. 반환값(return value)으로 사용할 수 있다. 4. 할당에 사용된 이름과 관계없이 고유한 구별이 가능하다. 출처: http://en.wikipedia.org/wiki/First-class_function 37
  • 39. Closures // plusFoo can reference any values/variables in scope var foo = 1 val plusFoo = (x:Int) => x + foo plusFoo(5)  6 // Changing foo changes the return value of plusFoo foo = 5 plusFoo(5)  10 38
  • 40. Pattern Matching def what(any:Any) = any match { case i:Int => "It's an Int" case s:String => "It's a String" case _ => "I don't know what it is" } what(123)  "It's an Int" what("hello")  "It's a String" what(false)  "I don't know what it is" 39
  • 41. Pattern Matching val nums = List(1,2,3) // Pattern matching to create 3 vals val List(a,b,c) = nums a  1 b  2 c  3 40
  • 42. Immutable Types // Immutable types by default var nums = Set(1,2,3) nums += 4  nums = nums.+(4) // Mutable types available import scala.collection.mutable._ val nums = Set(1,2,3) nums += 4  nums.+=(4) 41
  • 43. Referential Transparency // Transparent val example1 = "jamie".reverse val example2 = example1.reverse println(example1 + example2) // eimajjamie // Opaque val example1 = new StringBuffer("Jamie").reverse val example2 = example1.reverse println(example1 append example2) // jamiejamie  An expression is transparent if it can be replaced by its VALUE without changing the behavior of the program  In math, all functions are referentially transparent 42
  • 44. Scala Collections val myMap = Map(1 -> "one", 2 -> "two", 3 -> "three") val mySet = Set(1, 4, 2, 8) val myList = List(1, 2, 8, 3, 3, 4) val myVector = Vector(1, 2, 3...) You have the choice of mutable or immutable collection instances, immutable by default Rich implementations, extremely flexible 43
  • 45. Rich Collection Functionality val numbers = 1 to 20 // Range(1, 2, 3, ... 20) numbers.head // Int = 1 numbers.tail // Range(2, 3, 4, ... 20) numbers.take(5) // Range(1, 2, 3, 4, 5) numbers.drop(5) // Range(6, 7, 8, ... 20) There are many methods available to you in the Scala collections library Spend 5 minutes every day going over the ScalaDoc for one collection class 44
  • 49. Or Use Existing Java Collections java.util Apache Commons Collections fastutil Trove Google Collections scala.collection.JavaConversion available to convert to and from java.util Interfaces 48
  • 50. Higher Order Functions val plusOne = (x:Int) => x + 1 val nums = List(1,2,3) // map takes a function: Int => T nums.map(plusOne)  List(2,3,4) // Inline Anonymous nums.map(x => x + 1)  List(2,3,4) // Short form nums.map(_ + 1)  List(2,3,4) 49
  • 51. Higher Order Functions val nums = List(1,2,3,4) // A few more examples for List class nums.exists(_ == 2)  true nums.find(_ == 2)  Some(2) nums.indexWhere(_ == 2)  1 nums.reduceLeft(_ + _)  10 nums.foldLeft(100)(_ + _)  110 nums.filter(_ % 2 == 0)  List(2,4) // Many more in collections library flatMap, flatten, zip, fold, foldRight, reduce, reduceRight, groupBy partition, slice, take, takeRight, takeWhile, drop, dropRight, dropWhile, splitAt sortBy, sortWith, startsWith, endsWith, product, sum … 50
  • 52. Higher Order Functions // functions as parameters def call(f: Int => Int) = f(1) call(plusOne)  2 call(x => x + 1)  2 call(_ + 1)  2 51
  • 53. Higher Order Functions // functions as parameters def each(xs: List[Int], fun: Int => Unit) { if(!xs.isEmpty) { fun(xs.head) each(xs.tail, fun) } } each(List(1,2,3), println)  1  2  3 52
  • 54. Higher Order Functions class List[+A] … { def head: A def tail: List[A] … } // More complex example with generics & pattern matching @tailrec def each[T](xs: List[T], fun: T => Unit): Unit = xs match { case Nil => case head :: tail => fun(head); each(tail, fun) } each(List(1,2), println)  1  2 each(List("foo", "bar"), println)  foo  bar 53
  • 55. Parallel Collections scala> 1 to 1000000 res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3,... scala> res0.par res1: s.c.parallel.immutable.ParRange = ParRange(1, 2, 3,... scala> res1 map(_ + 1) res2: s.c.parallel.immutable.ParSeq[Int] = ParVector(2, 3, 4,... scala> res2.seq res3: s.c.immutable.Range = Range(2, 3, 4,...  You can easily parallelize the application of a function literal to your collection by calling the par() method on a collection instance  Uses JSR166 under the covers to fork/join for you  Use the seq() method on the parallel collection to return to a non-parallel instance 54
  • 56. Partial Functions val sample = 1 to 10 val isEven: PartialFunction[Int, String] = { case x if x % 2 == 0 => x+" is even" } val isEven= new PartialFunction[Int, String] { override def apply(x:Int):String = x+" is even" override def isDefinedAt(x:Int):Boolean = x % 2 == 0 } // the method collect can use isDefinedAt to select which members to collect val evenNumbers = sample collect isEven > evenNumbers = Vector(2 is even, 4 is even, 6 is even, 8 is even, 10 is even) val isOdd: PartialFunction[Int, String] = { case x if x % 2 == 1 => x+" is odd" } // the method orElse allows chaining another partial function to handle // input outside the declared domain val numbers = sample map (isEven orElse isOdd) > numbers = Vector(1 is odd, 2 is even, 3 is odd, 4 is even, 5 is odd, 6 is even, 7 is o dd, 8 is even, 9 is odd, 10 is even) 55
  • 57. Currying // multiple arguments def add(x:Int, y:Int) = x + y add(1, 2) // 3 add(7, 3) // 10 // after currying, single arguments def add(x:Int) = (y:Int) => x + y def add(x:Int)(y:Int) = x + y add(1)(2) // 3 add(7)(3) // 10  In computer science, currying, invented by Moses Schönfinkel and Gottlob Frege, is the technique of transforming a function that takes multiple arguments into a function that takes a single argument (the other arguments having been specified by the curry). 56
  • 58. Partially Applied Functions def justSum(a:Int,b:Int,c:Int) = a+b+c val partial1 = justSum _ val partial2 = justSum(1, _:Int, 8) def curriedSum(a:Int)(b:Int)(c:Int) = a+b+c println("Partial 1: "+partial1(5,6,7)) // Partial 1: 18 println("Partial 2: "+partial2(4)) // Partial 2: 13 57
  • 59. (엄밀히 말하자면 동적 언어는 아니지만, 동적 언어에서 제공하는 많은 특징을 가지고 있다.) Scala는 동적 언어이다. object Hello { def main(args:Array[String]) { println(“Hello, World”) } } 58
  • 60. Scriptable // HelloWorld.scala println("Hello World") bash$ scala HelloWorld.scala Hello World bash$ scala -e 'println("Hello World")' Hello World 59
  • 61. Read-Eval-Print Loop bash$ scala Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22). Type in expressions to have them evaluated. Type :help for more information. scala> class Foo { def bar = "baz" } defined class Foo scala> val f = new Foo f: Foo = Foo@51707653 scala> f.bar res2: java.lang.String = baz 60
  • 62. Structural Typing – Type safe Duck Typing // Duck Typing (other language) def test(o) log o.getName # Let's hope this will work end // Type safe Duck Typing def doTalk(any:{def talk:String}) { println(any.talk) } class Duck { def talk = "Quack" } class Dog { def talk = "Bark" } doTalk(new Duck)  "Quack" doTalk(new Dog)  "Bark" 출처: http://beust.com/weblog/2008/02/11/structural-typing-vs-duck-typing/ 61
  • 63. Implicit Conversions // Extend existing classes in a type safe way // Goal: Add isBlank method to String class class RichString(s:String) { def isBlank = null == s || "" == s.trim } implicit def toRichString(s:String) = new RichString(s) // Our isBlank method is now available on Strings " ".isBlank  true "foo".isBlank  false 62
  • 64. Implicit Conversions // Does not type check "abc".isBlank // Search in-scope implicits defs that take a // String & return a type with an isBlank method implicit def toRichString(s:String):RichString // Resulting code that type checks new RichString("abc").isBlank 63
  • 65. method_missing (Scala 2.9 Feature) // Dynamic is a marker trait used by the compiler class Foo extends Dynamic { def typed[T] = error("not implemented") def applyDynamic(name:String)(args:Any*) = { println("called: "+name+"("+args.mkString(",")+")") } } val f = new Foo f.helloWorld  called: helloWorld() f.hello("world")  called: hello(world) f.bar(1,2,3)  called: bar(1,2,3) 64
  • 66. Scala는 정적으로 타입화된 언어이다. object Hello { def main(args:Array[String]) { println(“Hello, World”) } } 65
  • 67. Generic Classes class Stack[T] { var elems: List[T] = Nil def push(x: T) { elems = x :: elems } def top: T = elems.head def pop() { elems = elems.tail } } object GenericsTest extends Application { val stack = new Stack[Int] stack.push(1) stack.push('a') println(stack.top) // 97 stack.pop() println(stack.top) // 1 } 출처: http://www.scala-lang.org/node/113 66
  • 68. Variances 구분 Scala Java Description Covariant +T ? extends T Covariant subclassing. E.g., List[Tsub] is a subtype of List[T]. Contravariant -T ? super T Contravariant subclassing. E.g., X[Tsup] is a subtype of X[T]. Invariant T T Invariant subclassing. E.g., Can’t substitute Y[Tsup]or Y[Tsub] for Y[T]. class Animal class Dog extends Animal class Vector[+T] { … } class Contravariant[-T] {…} class Array[T] {…} val aCovariantVector: Vector[Animal] = Vector.empty[Animal] // ok val aCovariantVector: Vector[Animal] = Vector.empty[Dog] // ok val aCovariantVector: Vector[Dog] = Vector.empty[Animal] // not ok val contravariantClass: Contravariant[Animal] = new Contravariant[Animal] // ok val contravariantClass: Contravariant[Dog] = new Contravariant[Animal] // ok val contravariantClass: Contravariant[Animal] = new Contravariant[Dog] // not ok val anInvariantArray: Array[Animal] = Array.empty[Animal] // ok val anInvariantArray: Array[Animal] = Array.empty[Dog] // not ok val anInvariantArray: Array[Dog] = Array.empty[Animal] // not ok 출처: http://www.scala-lang.org/node/129 67
  • 69. Type Bounds (Upper, Lower) 구분 Scala Java Description Upper Bounds A <: B A extends B A must be a subtype of B Lower Bounds A >: B - A must be a supertype of B object Array { ... def apply[A <: AnyRef](xs: A*): Array[A] = { // A must be a subtype of AnyRef val array = new Array[A](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } ... } class List[+A] { // A is covariant, List[+A] is subtype of List[A] ... def ::[B >: A](x : B) : List[B] = new scala.::(x, this) // B must be a supertype of A ... } val languages = List("Scala", "Java", "Ruby", "C#", "C++", "Python") // languages:List[String] = List(…) val list = 3.14 :: languages // list:List[Any] = List (…) println(list) // List(3.14, “Scala”, “Java”, “Ruby”, “C#”, “C++”, “Python”) 출처: http://www.scala-lang.org/node/136 68
  • 70. Views Implicit parameters and methods can also define implicit conversions called views. The term view conveys the sense of having a view from one type (A) to another type (B). A view is applied in two circumstances.  When a type A is used in a context where another type B is expected and there is a view in scope that can convert A to B.  When a non-existent member m of a type A is referenced, but there is an in-scope view that can convert A to a B that has the m member. def m [A <% B](arglist): R = ... def m [A](arglist)(implicit viewAB: A => B): R = ... class Container[A <% Int] { def addIt(x: A) = 123 + x } (new Container[String]).addIt("123") // ok, 246 (new Container[Int]).addIt(123) // ok, 246 (new Container[Float]).addIt(123.2F) // not ok 구분 Scala Java Description View Bounds A <% B - A can be converted to B using a view 출처: http://www.scala-lang.org/node/130 69
  • 71. Inner Classes class Graph { Use Companion Objects class Node { var connectedNodes: List[Node] = Nil object Graph { def connectTo(node: Node) { class Node { if (connectedNodes.find(node.equals).isEmpty) { var connectedNodes: List[Node] = Nil connectedNodes = node :: connectedNodes def connectTo(node: Node) { } if (connectedNodes.find(node.equals).isEmpty) { } connectedNodes = node :: connectedNodes } } var nodes: List[Node] = Nil } def newNode: Node = { } val res = new Node } nodes = res :: nodes class Graph { res var nodes: List[Graph.Node] = Nil } def newNode: Node = { } val res = new Node nodes = res :: nodes object IllegalGraphTest extends Application { res val g: Graph = new Graph } val n1: g.Node = g.newNode } val n2: g.Node = g.newNode n1.connectTo(n2) // legal val h: Graph = new Graph val n3: h.Node = h.newNode Use Type Projection n1.connectTo(n3) // illegal! class Graph { } class Node { var connectedNodes: List[Graph#Node] = Nil def connectTo(node: Graph#Node) { if (connectedNodes.find(node.equals).isEmpty) { connectedNodes = node :: connectedNodes } } } 1. Use Companion Objects: object Graph for class Graph … 2. Use Type Projection: Graph#Node is “a Node of any Graph” } 출처: http://www.scala-lang.org/node/115, http://weblogs.java.net/blog/cayhorstmann/archive/2011/08/05/inner-classes-scala-and-java 70
  • 72. Abstract Types abstract class Buffer[+T] { val element: T } abstract class SeqBuffer[U, +T <: Seq[U]] extends Buffer[T] { def length = element.length } object AbstractTypeTest2 extends Application { def newIntSeqBuf(e1: Int, e2: Int): SeqBuffer[Int, Seq[Int]] = new SeqBuffer[Int, List[Int]] { val element = List(e1, e2) } val buf = newIntSeqBuf(7, 8) println("length = " + buf.length) // length = 2 println("content = " + buf.element) // content = List(7, 8) } 출처: http://www.scala-lang.org/node/105 71
  • 73. Explicitly Typed Self References abstract class SubjectObserver { type S <: Subject // S must be a subtype of Subject type O <: Observer // O must be a subtype of Observer trait Subject { self: S => // #1. val self: SubjectObserver.this.S private var observers = List[O]() def addObserver(observer: O) = observers ::= observer def notifyObservers = observers foreach (_.receiveUpdate(self)) // #2 } trait Observer { def receiveUpdate(subject: S) } } - Comment #1 shows the self-type annotation, self: S =>. We can now use self as an alias for this, but whenever it appears, the type will be assumed to be S, not Subject. - Comment #2 shows that we pass self instead of this to receiveUpdate. 출처: http://www.scala-lang.org/node/124 72
  • 74. Polymorphic Methods object PolyTest extends Application { def dup[T](x: T, n: Int): List[T] = if (n == 0) Nil else x :: dup(x, n - 1) println(dup[Int](3, 4)) // required parameters println(dup("three", 3)) // not required to give actual type parameters explicitly } Method dup in object PolyTest is parameterized with type T and with the value parameters x: T and n: Int 출처: http://www.scala-lang.org/node/121 73
  • 75. Scala는 많은 좋은 기능이 있다. object Hello { def main(args:Array[String]) { println(“Hello, World”) } } 74
  • 76. Default Parameter Values def hello(foo:Int = 0, bar:Int = 0) { println("foo: "+foo+" bar: "+bar) } hello()  foo: 0 bar: 0 hello(1)  foo: 1 bar: 0 hello(1,2)  foo: 1 bar: 2 75
  • 77. Named Parameters def hello(foo:Int = 0, bar:Int = 0) { println("foo: "+foo+" bar: "+bar) } hello(bar=6)  foo: 0 bar: 6 hello(foo=7)  foo: 7 bar: 0 hello(foo=8,bar=9)  foo: 8 bar: 9 76
  • 78. Everything Returns a Value val a = if(true) "yes" else "no" val b = try{ "foo" } catch { case _ => "error" } val c = { println("hello") "foo" } 77
  • 79. Lazy Definitions // initialized on first access lazy val foo = { println("init") "bar" } foo  init foo  foo  78
  • 80. Nested Functions // Can nest multiple levels of functions def outer() { var msg = "foo" def one() { def two() { def three() { println(msg) } three() } two() } one() } 79
  • 81. By-Name Parameters // msg parameter automatically wrapped in closure def log(doLog:Boolean, msg: => String) { if(doLog) { msg // evaluates msg msg // evaluates msg again! } } def foo:String = { println("in foo"); "Foo" } log(true, foo+" Bar") // foo called twice  in foo  in foo log(false, foo+" Bar") // foo never called 80
  • 82. Many More Features  Annotations  @foo def hello = "world"  Option  Actors  DSL  For Comprehensions  for(i <- 1 to 5 if i % 2 == 0) yield i  Implicit Conversion  XML Literals  val node = <hello>world</hello>  Reflections  Macros (2.10)  etc… 81
  • 83. Scala를 어떻게 공부하지? object Hello { def main(args:Array[String]) { println(“Hello, World”) } } 82
  • 85. Martin Ordersky 저자 강의 https://www.coursera.org/course/progfun 84
  • 86. Typesafe http://www.typesafe.com 85
  • 87. Scala-Lang.org http://www.scala-lang.org/ 86
  • 88. Scala 공부관련 참고자료  스칼라 입문자료 – Scala API Reference – A Tour of Scala : 스칼라 공식 블로그에 올라온 스칼라 강좌 – Scala By Example : 마틴 오더스키가 작성한 100페이지짜리 스칼라 문서 – Scala School : 트위터가 작성한 스칼라 강좌 – Progammin in Scala 1st Edition : Programmin in Scala의 1st Edition은 온라인에서 무료로 볼 수 있음. – Effective Scala : 트위터의 Marius Eriksen가 만든 스칼라 강좌  스칼라 서적(영문) – Programming in Scala : 마틴 오더스키가 참여한 스칼라 서적. 강추!!, Scala 2.8 기준. – Programming Scala : 오렐리에서 나옴. 읽어본 사람 없어서 내용은 잘 모름. – Programming Scala : Pragmatic에서 나온 스칼라 서적. 읽어볼 만은 하지만 빠진 내용이 많아 서 많이 추천하지는 않음. – Scala in Depth : 매닝에서 나온 서적. 읽어본 사람이 없어서 내용은 잘 모름.  그외 참고 자료 – Style Guide for Scala : 스칼라 코드 작성에 대한 스타일가이드 발표자료 – Scala Puzzlers : 스칼라 문제를 풀어볼 수 있는 사이트 – Scala Cheat Sheet : 간단한 스칼라문법을 참고할 수 있는 치트시트 – S99 Scala Problems: 99 개의 스칼라 프로그래밍 학습 예제 87