SBE스터디정리 - codeport/scala GitHub Wiki

스터디 4차 과제 (2010/12/12)

  • 10.1 N-QUeens Problem (p80)
  • 10.3.1 flatten Excercise (p84)
  • 11.2 whileLoop로 power구현하기 (p91)
  • 11.3 Discrete Event Simulation (p92~)

스터디 5차 내용 요약(2010/1/15)

스터디 6차 내용 요약(2010/1/22)

  • 15장 Implicit Parameters And Conversions
    1. Implicit Parameters 관련 글

      • 괄호 생략을 명확히 할 필요가 있음

          scala> def log(headAndTail:String, p:() => Unit):Unit = {
               |   println(headAndTail);
               |    
               | {
               | }}
          log: (headAndTail: String,p: () => Unit)Unit
          
          scala> p
          res8: () => Unit = <function0>
          
          scala> p()
          ME!
        
      • 인자 1개인 경우 괄호 생략?

          scala> def p = (x:Int) => { println ("x= " + x); }
          p: (Int) => Unit
          
          scala> p 1
          <console>:1: error: ';' expected but integer literal found.
                 p 1
                   ^
        
      • 함수 정의 형태에 따른 차이, Function0... 객체의 관계를 명확히 하자

         scala> def p:()=>Unit = () => { println("##"); } 
         p: () => Unit
         
         scala> def p2():Unit = {println("@@@"); }      
         p2: ()Unit
         
         scala> val x:()=>Unit = p2
         x: () => Unit = <function0>
         
         scala> x
         res32: () => Unit = <function0>
         
         scala> x()
         @@@
        
    2. 참고: Default Parameter and Named Parameter

    3. View Bounds의 정의

       View bounds <% are weaker than plain bounds <:: 
       A view bounded type parameter clause [A <% T] only specifies 
       that the bounded type A must be convertible to the bound type T, 
       using an **implicit conversion**.
        
       -- Scala By Example, p55 --
      
    4. A Tour of Scala: Views

    5. View Bounds < Upper Bounds ?

       scala> class Parent
       defined class Parent
       
       scala> class Child extends Parent
       defined class Child
       
       scala> def printf[A <% Parent](obj:A):Unit = {
            |   println( obj + "!!!" );              
            | }
       printf: [A](obj: A)(implicit evidence$1: (A) => Parent)Unit
      
    6. Trait

      • PIS 12, 20장 참고

      • Abstract vals (PIS 20.3)

          scala> trait TestLinearization {
               |   val message:String
               |   def print():Unit = {
               |     println(message)
               |   }
               | }
          defined trait TestLinearization
          
          scala> object Test extends TestLinearization {
               |   val message = "hi"
               | }
          defined module Test
          
          scala> Test.print  
          hi
        
      • override def with val?

      • Abstract vars (PIS 20.4)

    7. DisjointType ('Int or String' Type) - Infix Types

           // This
           type Foo[Bar,Baz]
           // is the same as 
           type Bar Foo Baz
      
    8. 과제 Persistent DSL 만들고 공유하기

      • SBT

스터디 6차 후 내용 추가

  • def, val 차이

      scala> def a:Int = { println("A"); 3 } 
      a: Int
    
      scala> a
      A
      res11: Int = 3
    
      scala> a
      A
      res12: Int = 3
    
      scala> val a = { println("A");3}
      A
      a: Int = 3
    
      scala> a
      res23: Int = 3
    
      scala> a
      res24: Int = 3
    
  • 함수 정리

      scala> val f1:(Int,Int)=>String = (a,b)=> { a + b + "!" }
      f1: (Int, Int) => String = <function2>
      
      scala> val f2:Function2[Int,Int,String] = f1
      f2: (Int, Int) => String = <function2>
      
      scala> f2(3,4)
      res39: String = 7!
      
      scala> val f3 = new Function2[Int,Int,String] {              
          |   def apply(x:Int, y:Int) = { x + y + "!" } 
          | }
      f3: java.lang.Object with (Int, Int) => String = <function2>
      
      scala> f3(3,4) 
      res40: String = 7!
    
    • (A)=>B == Function1[A,B]
    • 함수도 결국 (객체의) 인스턴스
    • 함수를 실행하려면 ()사용
  • Scala로 control structure를 정의해 JDBC loop 구현 예

    • Beginning Scala p109 참고

스터디 7차 관련 내용 추가

⚠️ **GitHub.com Fallback** ⚠️