KoreanFoodie's Study
Scala 6 - Datatypes 본문
스칼라 튜토리얼, scala의 datatypes에 대해 알아보자
Types in general
-
Introdection : How to construct elements
-
Elimination : How to use elements of the type
-
Primitive types :
Int, Boolean, Double, String...
Intro for Int: ...,-2,-1,0,1,2
Elim for Int: +,-,*,/...
-Function types :
Int => Int, (Int => Int)=>(Int => Int)
Intro: (x:T)=>e
Elim: f(v)
Tuples
- Intro :
(1, 2, 3) : (Int, Int, Int)
(1, "a") : (Int, String)
- Elim :
(1, "a", 10)._1 = 1
(1, "a", 10)._2 = "a"
Only up to length 22
If you use tuple types, each has its own closure!
def bar : (x:Int) = x + 1
val a = (3+1, foo)
// def a = (4+1, bar) -> Nope!
Structural types (a.k.a Record Types)
val foo = new { //or, Object foo
val a = 3
def b = a+1
def f(x:Int) = b+x
def f(x:String) = "hello"+x
}
foo.b
foo.f(3)
foo.f("gil")
def g(x: {val a: Int; def b: Int;
def f(x:Int): Int;
def f(x:String): String}) =
x.f(3)
g(foo)
Structural types always have closure within it.
def bar(x: Int) = x+1
object foo {
def g: Int => Int = bar _
}
Structural Types : Scope and Type Alias
You can use type
as typedef
in C.
val gn = 0
object foo {
val a = 3
def b = a + 1
def f(x: Int) = b + x + gn
}
foo.f(3)
type Foo = {val a: Int; def b: Int; def f(x:Int):Int}
def g(x: Foo) = {
val gn = 10
x.f(3)
}
g(foo)
Algebraic Datatypes
- Ideas
T = C of T * … * T
| C of T * … * T
| …
| C of T * … * T
- E.g.
Attr = Name of String
| Age of Int
| DOB of Int * Int * Int
| Height of Double
- Intro:
Name(“Chulsoo Kim”), Name(“Younghee Lee”), Age(16),
DOB(2000,3,10), Height(171.5), …
Recursive ADT
- E.g.
IList = INil
| ICons of Int * IList
- Intro:
INil(),
ICons(3,INil()),
ICons(2, ICons(3,INil())),
ICons(1, ICons(2, ICons(3,INil()))),
Algebraic Datatypes In Scala
- Attr
sealed abstract class Attr
case class Name(name:String) extends Attr
case class Age(name:Int) extends Attr
case class DOB(year: Int, month: Int,
day: Int) extends Attr
case class Height(height: Double) extends Attr
val a: Name = Name("Chulsoo Kim")
val b: Attr = DOB(2000, 3, 10)
If we look at the DOB, It is good for readability when we indicate 3 variables with its name(year, month, day).
- IList
sealed abstract class IList
case class INil() extends IList
case class ICons(hd: Int, tl: IList) extends IList
val x: IList = ICons(2, ICons(2, INil()))
def gen(n: Int) : IList = {
if (n <= 0) INil()
else ICons(n, gen(n-1))
}
Exercise
Define IOption and BTree algebraic datatypes.
IOption = INone
| ISome of Int
BTree = Leaf
| Node of Int * BTree * BTree
- Example:
sealed abstract class IList
case class INil() extends IList
case class ICons(hd: Int, tl: IList) extends IList
def x : IList = ICons(2, ICons(1, INil()))
Solution :
sealed abstract class IOption
case class INone() extends IOption
case class ISome(some: Int) extends IOption
sealed abstract class BTree
case class Leaf() extends BTree
case class Node(value: Int, left: BTree, right: BTree) extends BTree
val t = Node(3, Node(2, Leaf(), Leaf()), Leaf())
If we express t
as BTree, It looks like this:
3
/ \
2 Leaf
/ \
Leaf Leaf
'Tutorials > Scala' 카테고리의 다른 글
Scala 8 - Parametric Polymorphism (0) | 2019.04.23 |
---|---|
Scala 7 - Datatypes2 (0) | 2019.04.23 |
Scala 5 - Currying (0) | 2019.04.23 |
Scala 4 - Closures, revisted (0) | 2019.04.23 |
Scala 3 - Tail Recursion & Higher-Order Functions 1 (0) | 2019.04.23 |