본문 바로가기
Language/Scala

(기초) 스칼라 데이터 구조(컬렉션)

by 태하팍 2016. 3. 25.
반응형




2016/03/25 - [Language/Scala] - (기초) 스칼라 문법

참고 사이트 : https://twitter.github.io/scala_school/ko/collections.html


scala> val numbers = List(1,2,3,4,5)

numbers: List[Int] = List(1, 2, 3, 4, 5)


scala> Set(1,2,2)

res20: scala.collection.immutable.Set[Int] = Set(1, 2)


scala> val hostPort = ("localhsot", 8080)

hostPort: (String, Int) = (localhsot,8080)


scala> hostPort._1

res21: String = localhsot


scala> hostPort._2

res22: Int = 8080


scala> 1->2

res23: (Int, Int) = (1,2)


scala> ("D", (1,2,3))

res25: (String, (Int, Int, Int)) = (D,(1,2,3))


scala> ("H", (4,5,6))

res26: (String, (Int, Int, Int)) = (H,(4,5,6))


scala> ("D", (1,2,3)) -> ("H", (4,5,6))

res27: ((String, (Int, Int, Int)), (String, (Int, Int, Int))) = ((D,(1,2,3)),(H,(4,5,6)))


scala> Map(1 -> 2)

res28: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2)


scala> Map("foo" -> "bar")

res29: scala.collection.immutable.Map[String,String] = Map(foo -> bar)


scala> Map(1-> Map("foo" -> "bar"))

res31: scala.collection.immutable.Map[Int,scala.collection.immutable.Map[String,String]] = Map(1 -> Map(foo -> bar))


scala> numbers.map((i:Int) => i *2)

res33: List[Int] = List(2, 4, 6, 8, 10)


scala> numbers.foreach((i: Int) => i * 3)


scala> val value = numbers.foreach((i:Int) => i * 7)

value: Unit = ()


scala> def isEven(i:Int): Boolean = i % 2 == 0

isEven: (i: Int)Boolean


scala> numbers.filter(isEven _)

res37: List[Int] = List(2, 4)


scala> val numbers = List(1,2,3,4,5,6,7,8)

numbers: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)


scala> numbers.partition(_ % 2 == 0)

res39: (List[Int], List[Int]) = (List(2, 4, 6, 8),List(1, 3, 5, 7))


scala> numbers.find((i: Int) => i > 5)

res40: Option[Int] = Some(6)


scala> numbers.drop(5)

res41: List[Int] = List(6, 7, 8)


scala> numbers.dropWhile(_ %2 != 0)

res42: List[Int] = List(2, 3, 4, 5, 6, 7, 8)






반응형

'Language > Scala' 카테고리의 다른 글

couchbase와 같은 키/벨류의 key design??  (0) 2016.04.15
(기초) 스칼라 문법  (0) 2016.03.25