Slide for talk "Effective Scala" at SpringCamp 2013 explaining how to use Scala in Scala (or more broadly functional language) style. This talk is for Scala novices who are usually ignorant of functional language paradigm but familiar with OOP.
83. 생략합니다
sealed abstract class Fruit
case object Mango extends Fruit
case object Apple extends Fruit
case object Orange extends Fruit
trait FruitDealer {
! def getPrice(f: Fruit) : Double =
! ! f match {
! ! ! case Mango = 3.0
! ! ! case Apple = 1.1
! ! ! case Orange = 1.6
! ! }
}
187. 말합니다
!
!
!
var y = 8
def add2(x: Int) : Int = x + 2
val ten = add2(0) + y
!
!
!
var y = 8
def add2(x: Int) : Int = {y += 1; x + 2}
val ten = add2(0) + y
add2는
287. 불변성
!
!
!
sealed abstract class Tree
case class Leaf[A](x: A) extends Tree
case class Branch(left: Tree, right: Tree) extends Tree
!
!
!
val stringTree = Branch(Branch(Leaf(Fus),Leaf(Ro)),Leaf(Dah))
val intTree = Branch(Branch(Leaf(1),Leaf(2)),Leaf(3))
val hTree = Branch(Branch(Leaf(Fus),Leaf(2)),Leaf(Dah))
Fus
Ro
stringTree
Dah
1
2
intTree
3
Fus
2
hTree
Dah
576. 더한다면
!
!
!
!
!
!
def addOne(list: List[Int]) : List[Int] = {
! list match {
! ! case Nil = Nil
! ! case head::tail = (head + 1)::addOne(tail)
! }
}
!
!
val col = (0 to 1000000).toList
val result = addOne(col)
java.lang.StackOverflowError
! at java.lang.Integer.intValue(Integer.java:701)
! at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java..
! at Immutability$.addOne(Immutability.scala:6)
! at Immutability$.addOne(Immutability.scala:6)
655. 더한다면
!
!
!
!
!
!
!
!
import scala.annotation.tailrec
@tailrec
def addOne(list: List[Int]) : List[Int] = {
! list match {
! ! case Nil = Nil
! ! case head::tail = (head + 1)::addOne(tail)
! }
}
Immutability.scala:5: error: could not optimize @tailrec
annotated method addOne: it contains a recursive call not in
tail position
! ! list match {
^
one error found
821. 구성성
!
!
!
!
!
!
case class CellPhoneMaker(name: String) {
! abstract class Model
! case object SmartPhone3 extends Model
! case object SmartPhone4 extends Model
! case object SmartPad extends Model
}
!
!
!
!
val apple = CellPhoneMaker(Apple)
val apple2 = CellPhoneMaker(Apple)!
println(apple == apple2)
println(apple.SuperPhone1 == apple2.SuperPhone1)
823. 구성성
!
!
!
!
!
!
case class CellPhoneMaker(name: String) {
! abstract class Model
! case object SmartPhone3 extends Model
! case object SmartPhone4 extends Model
! case object SmartPad extends Model
}
!
!
!
!
val apple = CellPhoneMaker(Apple)
val apple2 = CellPhoneMaker(Apple)!
println(apple == apple2)
println(apple.SuperPhone1 == apple2.SuperPhone1)
true
false
831. 만듭시다
!
!
val apple = new CellPhoneMaker(Apple, Cupertino)
val lg = new CellPhoneMaker(LG, Seoul)
!
!
val phoneFromApple = apple.produce()
val phoneFromLG = lg.produce()
!
apple.fix(phoneFromLG)
Composability.scala:30: error: type mismatch;
found
: Composability.lg.Model
required: Composability.apple.Model
!
apple.fix(phoneFromLG)
^
one error found
879. 만듭시다
!
!
val apple = new CellPhoneMaker(Apple, Cupertino)
val lg = new CellPhoneMaker(LG, Seoul)
!
!
val phoneFromApple = apple.produce()
val phoneFromLG = lg.produce()
!
apple.fix(phoneFromLG)
#
925. 어떨까요?
!
!
!
!
class BikeMaker(val name: String, val address: String) extends Maker {
!
case class AluminiumBike1(serial: Int, working: Boolean) extends Model
!
case class AluminiumBike2(serial: Int, working: Boolean) extends Model
!
case class TitaniumBike(serial: Int, working: Boolean) extends Model
!
!
!
}
def defaultModel(serial: Int) = TitaniumBike(serial,true)
•자동차는요?
!
!
!
!
class CarMaker(val name: String, val address: String) extends Maker {
!
case class SmallCar(serial: Int, working: Boolean) extends Model
!
case class MediumCar(serial: Int, working: Boolean) extends Model
!
case class LargeCar(serial: Int, working: Boolean) extends Model
!
!
!
}
def defaultModel(serial: Int) = MediumCar(serial,true)