Created
July 1, 2018 09:26
-
-
Save mrbkdad/85f6584ca4d0588ab6bd247b9246641f to your computer and use it in GitHub Desktop.
learning scala chapter 3, 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 3. 배열 사용하기\n", | |
"- 고정길이 Array, 가변길이 ArrayBuffer\n", | |
"- 초기값과 생성시 new 사용 않음\n", | |
"- [] (X) -> () (O)\n", | |
"- for(elem <- arr) 사용\n", | |
"- for(elem <- arr if ...) ... yield ... : 배열 변환 ( Map ? )\n", | |
"- 자바와의 호환 : ArrayBuffer, scala.collection.JavaConversion 사용" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 고정 길이 배열\n", | |
"- Array 사용\n", | |
"- JVM 내부에서 자바 배열로\n", | |
" - Array(\"a\",\"b\") -> java.loang.String[]\n", | |
" - Array(1,2) -> int[]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10\n", | |
"0\n", | |
"null\n" | |
] | |
} | |
], | |
"source": [ | |
"val nums = new Array[Int](10)\n", | |
"val strs = new Array[String](10)\n", | |
"println(nums.length)\n", | |
"println(nums(0))\n", | |
"println(strs(0))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Array(Hello, World)" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val strs = Array(\"Hello\",\"World\")\n", | |
"strs" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 가변 길이 배열\n", | |
"- ArrayBuffer 사용 : 자바 ArrayList, C++ vector" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"ArrayBuffer(1, 1, 2, 3, 5, 8, 13, 21)" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import scala.collection.mutable.ArrayBuffer\n", | |
"val arrb = ArrayBuffer[Int]() // new ArrayBuffer[Int]()\n", | |
"arrb += 1\n", | |
"arrb += (1,2,3,5)\n", | |
"arrb ++= Array(8,13,21)\n", | |
"arrb" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"ArrayBuffer(1, 1, 2)" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arrb.trimEnd(5)\n", | |
"arrb" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"ArrayBuffer(1, 1, 6, 2)" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arrb.insert(2,6)\n", | |
"arrb" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"ArrayBuffer(1, 1, 7, 8, 9, 6, 2)" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arrb.insert(2,7,8,9)\n", | |
"arrb" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"ArrayBuffer(1, 1, 8, 9, 6, 2)" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arrb.remove(2)\n", | |
"arrb" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"ArrayBuffer(1, 1, 2)" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arrb.remove(2,3)\n", | |
"arrb" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Array(1, 1, 2)" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arrb.toArray" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### for 루프 이용" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0:1\n", | |
"1:1\n", | |
"2:2\n" | |
] | |
} | |
], | |
"source": [ | |
"for(i <- 0 until arrb.length) println(i + \":\" + arrb(i))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0:1\n", | |
"2:2\n" | |
] | |
} | |
], | |
"source": [ | |
"for(i <- 0 until arrb.length by 2) println(i + \":\" + arrb(i))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0:1\n", | |
"2:2\n" | |
] | |
} | |
], | |
"source": [ | |
"//RichInt 메쏘드 형태\n", | |
"for(i <- 0.until(arrb.length,2)) println(i + \":\" + arrb(i))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2:2\n", | |
"1:1\n", | |
"0:1\n" | |
] | |
} | |
], | |
"source": [ | |
"for(i <- arrb.length - 1 to 0 by -1 ) println(i + \":\" + arrb(i))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Range(2, 1, 0)" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(0 until arrb.length).reverse" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"1\n", | |
"2\n" | |
] | |
} | |
], | |
"source": [ | |
"for(e <- arrb) println(e)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 배열 변환\n", | |
"- for(...) yield\n", | |
"- 기존 콜렉션과 동일한 타입의 콜렉션 생성\n", | |
"- 매 루프 반목 마다 yield 뒤에 나오는 식을 포함\n", | |
"- 가드 사용하여 조건 처리\n", | |
"- map, filter 사용하여 하여 같은 결과를 생성 가능, 자신에 맞게 사용" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"ArrayBuffer(4, 8, 12, 16)" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val arrb = ArrayBuffer(1,2,3,4,5,6,7,8)\n", | |
"for(e <- arrb if e%2 == 0) yield 2*e" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"ArrayBuffer(4, 8, 12, 16)" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arrb.filter(_ % 2 == 0).map(_ * 2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 배열 기능\n", | |
"- sum : 숫자 자료형에 대한 합\n", | |
"- min/max : 최소, 최대 값\n", | |
"- sorted : 정렬된 배열 생성\n", | |
"- sortBy : 하나의 원소 사용, sortWith : 두개의 원소 사용\n", | |
"- scala.util.Sorting.quickSort : 배열 소트\n", | |
"- mkString : python join\n", | |
"- min,max,quickSort : 비교 연산을 가지고 있는 경우에 가능(숫자, 문자열, Ordered 트레이)\n", | |
"- scala doc ArrayOps 참고" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"10" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Array(1,2,3,4).sum" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"little" | |
] | |
}, | |
"execution_count": 19, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ArrayBuffer(\"Mary\",\"had\",\"a\",\"little\",\"lamb\").max" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"ArrayBuffer(1, 2, 7, 9)" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val arrb = ArrayBuffer(1,7,2,9)\n", | |
"val arrc = arrb.sorted\n", | |
"arrc" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"ArrayBuffer(9, 1, 7, 2)" | |
] | |
}, | |
"execution_count": 21, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arrb.sortBy(_ % 3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Array(AAA, bbb, ccc, DDD)" | |
] | |
}, | |
"execution_count": 22, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val strs = Array(\"AAA\",\"bbb\",\"DDD\",\"ccc\")\n", | |
"strs.sortBy(_.charAt(0).toUpper)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"ArrayBuffer(9, 7, 2, 1)" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"arrb.sortWith((a,b) => a > b)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"AAA,bbb,DDD,ccc" | |
] | |
}, | |
"execution_count": 24, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"strs.mkString(\",\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[Ljava.lang.String;@2a8a79ba" | |
] | |
}, | |
"execution_count": 25, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"//JVM : Array 해당 타입의 배열\n", | |
"strs.toString" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"ArrayBuffer(1, 7, 2, 9)" | |
] | |
}, | |
"execution_count": 26, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"//ArrayBuffer scala\n", | |
"ArrayBuffer(1,7,2,9).toString" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 다차원 배열\n", | |
"- ofDim" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Array(Array(0.0, 0.0, 0.0, 0.0), Array(0.0, 0.0, 0.0, 0.0), Array(0.0, 0.0, 0.0, 0.0))" | |
] | |
}, | |
"execution_count": 27, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val matrix = Array.ofDim[Double](3,4)\n", | |
"matrix" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0.0" | |
] | |
}, | |
"execution_count": 28, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"matrix(1)(0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Array(Array(0), Array(0, 0), Array(0, 0, 0), Array(0, 0, 0, 0), Array(0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0))" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val triangle = new Array[Array[Int]](10)\n", | |
"for(i <- 0 until triangle.length) triangle(i) = new Array[Int](i+1)\n", | |
"triangle" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 자바 연동\n", | |
"- scala.collection.JavaConversions 암묵적 변환 메소드 임포트\n", | |
"- 자바 java.util.List를 사용하는 메소드 호출시 scala Buffer 사용 가능\n", | |
"- 자바 메소드의 결과가 java.util.List 일경우 scala의 Buffer 로 변환\n", | |
"- ava.lang.ProcessBuilder -> https://docs.oracle.com/javase/8/docs/api/" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"// 자바 메소드 사용\n", | |
"import scala.collection.JavaConversions.bufferAsJavaList\n", | |
"import scala.collection.mutable.ArrayBuffer\n", | |
"val command = ArrayBuffer(\"ls\",\"-al\",\"~\")\n", | |
"val pb = new ProcessBuilder(command)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"ArrayBuffer(ls, -al, ~)" | |
] | |
}, | |
"execution_count": 31, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"// 자바 메소드 리턴 값 사용\n", | |
"import scala.collection.JavaConversions.asScalaBuffer\n", | |
"import scala.collection.mutable.Buffer\n", | |
"val cmd: Buffer[String] = pb.command()\n", | |
"cmd" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 4. 맵과 튜플\n", | |
"- 맵 : 키/값 쌍의 모음\n", | |
"- 튜플 : 같은 타입이 아니어도 되는 n개 오브젝트 집합\n", | |
"- 맵 생성, 쿼리, 방문\n", | |
"- mutable map, immutable map\n", | |
"- hashmap : default, treemap 사용 가능\n", | |
"- 값들을 뭉치기 위한 튜플\n", | |
"- 값들을 묶어서 처리 하기 위해 zip 메소드 사용" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 맵 생성, 접근\n", | |
"- Map[String, Int] : immutable map\n", | |
"- scala.collection.mutable.Map : mutable map\n", | |
"- scala.collection.mutable.HashMap : hash map\n", | |
"- 초기화 하며 생성시 -> 사용 혹은 n=2 튜플 사용\n", | |
"- 값 접근 () 함수 표기법 사용\n", | |
"- 키 값 여부 확인 : contains\n", | |
"- getOrElse(key,default value) : key 에 해당 하는 값 리턴, 없으면 default value\n", | |
"- get : None 혹은 Option 오브젝트 리턴" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"10" | |
] | |
}, | |
"execution_count": 32, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val scores = Map(\"Alice\"->10,\"Bob\"->3,\"Cindy\"->8)\n", | |
"// val scores = Map((\"Alice\",10),(\"Bob\",3),(\"Cindy\",8)) // -> 쌍 연산자, 쌍 n=2 인 튜플 \n", | |
"scores(\"Alice\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Name: Compile Error\n", | |
"Message: <console>:49: error: value update is not a member of scala.collection.immutable.Map[String,Int]\n", | |
" scores(\"Alice\") = 9\n", | |
" ^\n", | |
"StackTrace: " | |
] | |
}, | |
"execution_count": 33, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"// 수정 불가능\n", | |
"scores(\"Alice\") = 9" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"10" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val scores = scala.collection.mutable.Map(\"Alice\"->10,\"Bob\"->3,\"Cindy\"->8)\n", | |
"scores(\"Alice\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(Bob -> 3, Alice -> 9, Cindy -> 8)" | |
] | |
}, | |
"execution_count": 35, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"scores(\"Alice\") = 9\n", | |
"scores" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"val scores = new scala.collection.mutable.HashMap[String,Int]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 37, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val scores = Map(\"Alice\"->10,\"Bob\"->3,\"Cindy\"->8)\n", | |
"val bobsScore = scores(\"Bob\")\n", | |
"bobsScore" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 38, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val bobsScore = if(scores.contains(\"Bob\")) scores(\"Bob\") else 0\n", | |
"bobsScore" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0" | |
] | |
}, | |
"execution_count": 39, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val bobsScore = scores.getOrElse(\"Bobs\",0)\n", | |
"bobsScore" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Some(3)" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"scores.get(\"Bob\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"None" | |
] | |
}, | |
"execution_count": 41, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"scores.get(\"Bobs\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 맵 값 갱신\n", | |
"- mutable map 에서 값 수정\n", | |
" - (key) : 해당 키/값 수정\n", | |
" - \"+=\" : 여러 키/값 수정 및 추가\n", | |
" - \"-=\" : 해당 키값 삭제\n", | |
"- immutable map 에서 값 수정\n", | |
" - \"+\" : 추가 혹은 해당 키에 대한 값 수정이 반영된 새로운 맵 생성\n", | |
" - \"-\" : 해당 키값 삭제된 새로운 맵 생성" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(Bob -> 3, Alice -> 10, Cindy -> 8)" | |
] | |
}, | |
"execution_count": 42, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val scores = scala.collection.mutable.Map(\"Alice\"->10,\"Bob\"->3,\"Cindy\"->8)\n", | |
"scores" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(Bob -> 10, Alice -> 10, Cindy -> 8)" | |
] | |
}, | |
"execution_count": 43, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"scores(\"Bob\") = 10\n", | |
"scores" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(Bob -> 12, Fred -> 7, Alice -> 10, Cindy -> 8)" | |
] | |
}, | |
"execution_count": 44, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"scores += (\"Bob\"->12,\"Fred\"->7)\n", | |
"scores" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(Bob -> 12, Fred -> 7, Cindy -> 8)" | |
] | |
}, | |
"execution_count": 45, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"scores -= \"Alice\"\n", | |
"scores" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(Alice -> 10, Bob -> 3, Cindy -> 8)" | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val scores = Map(\"Alice\"->10,\"Bob\"->3,\"Cindy\"->8)\n", | |
"scores" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(Alice -> 10, Bob -> 3, Cindy -> 8)" | |
] | |
}, | |
"execution_count": 47, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val newScores = scores + (\"Bob\"->12,\"Fred\"->7)\n", | |
"scores" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(Alice -> 10, Bob -> 12, Cindy -> 8, Fred -> 7)" | |
] | |
}, | |
"execution_count": 48, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"newScores" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(Bob -> 3, Cindy -> 8)" | |
] | |
}, | |
"execution_count": 49, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"scores - \"Alice\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(Alice -> 10, Bob -> 12, Cindy -> 8, Fred -> 7)" | |
] | |
}, | |
"execution_count": 50, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"// 새로운 맵을 계속 생성하더라도 이전 맵과 새 맵의 구조 적인 정보들이 공유 된다.\n", | |
"var scores = Map(\"Alice\"->10,\"Bob\"->3,\"Cindy\"->8)\n", | |
"scores = scores + (\"Bob\"->12,\"Fred\"->7)\n", | |
"scores" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 맵 반복과 정렬\n", | |
"- 반복\n", | |
" - for((k,v) <- map) ...\n", | |
" - keySet : key set\n", | |
" - values : value Iterator\n", | |
" - 맵의 키와 값을 값과 키 형태로 변경 : for((k,v) <- map) yield(v,k)\n", | |
"- 정렬\n", | |
" - 스칼라는 디폴트로 Hash Table을 생성한다.\n", | |
" - 키를 정렬된 순서로 방문해야 하거나 키를 위한 적당한 해시 함수가 없다면 트리 맵을 사용할 수 있다.\n", | |
" - scala.collection.immutable.SortedMap 사용(mutable Tree Map은 없음, 자바의 TreeMap 참조)\n", | |
" - 삽입한 순서대로 키를 방문하려면 scala.collection.mutable.LinkedHashMap 사용" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Alice -> 10\n", | |
"Bob -> 3\n", | |
"Cindy -> 8\n" | |
] | |
} | |
], | |
"source": [ | |
"val scores = Map(\"Alice\"->10,\"Bob\"->3,\"Cindy\"->8)\n", | |
"for((k,v) <- scores) printf(\"%s -> %s\\n\",k,v)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Set(Alice, Bob, Cindy)" | |
] | |
}, | |
"execution_count": 52, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"scores.keySet" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"MapLike(10, 3, 8)" | |
] | |
}, | |
"execution_count": 53, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"scores.values" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 54, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10\n", | |
"3\n", | |
"8\n" | |
] | |
} | |
], | |
"source": [ | |
"for(v <- scores.values) println(v)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(10 -> Alice, 3 -> Bob, 8 -> Cindy)" | |
] | |
}, | |
"execution_count": 55, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"for((k,v) <- scores) yield(v,k)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(Alice -> 10, Bob -> 3, Cindy -> 8, Fred -> 7)" | |
] | |
}, | |
"execution_count": 56, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val scores = scala.collection.immutable.SortedMap(\"Alice\"->10,\"Fred\"->7,\"Bob\"->3,\"Cindy\"->8)\n", | |
"scores" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(January -> 1, February -> 2, March -> 3, April -> 4, May -> 5)" | |
] | |
}, | |
"execution_count": 57, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val months = scala.collection.mutable.LinkedHashMap(\"January\"->1,\"February\"->2,\"March\"->3,\"April\"->4)\n", | |
"months += (\"May\"->5)\n", | |
"months" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(January -> 1, February -> 2, May -> 5, April -> 4, March -> 3)" | |
] | |
}, | |
"execution_count": 58, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val months = Map(\"January\"->1,\"February\"->2,\"March\"->3,\"April\"->4)\n", | |
"months + (\"May\"->5)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 자바 연동\n", | |
"- 자바 맵을 스칼라 맵으로 변환 사용\n", | |
" - scala.collection.JavaConversions.mapAsScalaMap : java.util.TreeMap(mutable tree map) 사용\n", | |
" - scala.collection.JavaConversions.propertiesAsScalaMap : java.util.Properties 사용\n", | |
"- 스칼라 맵을 자바 맵으로 변환 사용\n", | |
" - scala.collection.JavaConversions.mapAsJavaMap" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map()" | |
] | |
}, | |
"execution_count": 59, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import scala.collection.JavaConversions.mapAsScalaMap\n", | |
"val scores: scala.collection.mutable.Map[String,Int] = new java.util.TreeMap[String,Int]\n", | |
"scores" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(test -> 1)" | |
] | |
}, | |
"execution_count": 60, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"scores += (\"test\"->1)\n", | |
"scores" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(java.runtime.name -> OpenJDK Runtime Environment, sun.boot.library.path -> /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64, java.vm.version -> 25.171-b11, java.vm.vendor -> Oracle Corporation, java.vendor.url -> http://java.oracle.com/, path.separator -> :, java.vm.name -> OpenJDK 64-Bit Server VM, file.encoding.pkg -> sun.io, user.country -> US, sun.java.launcher -> SUN_STANDARD, sun.os.patch.level -> unknown, java.vm.specification.name -> Java Virtual Machine Specification, user.dir -> /opt/mynotebook/scala, java.runtime.version -> 1.8.0_171-8u171-b11-0ubuntu0.16.04.1-b11, SPARK_SUBMIT -> true, java.awt.graphicsenv -> sun.awt.X11GraphicsEnvironment, java.endorsed.dirs -> /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/endorsed, os.arch -> am..." | |
] | |
}, | |
"execution_count": 61, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import scala.collection.JavaConversions.propertiesAsScalaMap\n", | |
"val props: scala.collection.Map[String,String] = System.getProperties()\n", | |
"props" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 62, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"java.awt.Font[family=Serif,name=Serif,style=plain,size=12]" | |
] | |
}, | |
"execution_count": 62, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import scala.collection.JavaConversions.mapAsJavaMap\n", | |
"import java.awt.font.TextAttribute._\n", | |
"val attrs = Map(FAMILY->\"Serif\",SIZE->12)\n", | |
"val font = new java.awt.Font(attrs)\n", | |
"font" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 튜플\n", | |
"- 키/값 쌍의 모음\n", | |
"- 쌍은 튜플의 가장 간단한 사례(n = 2)\n", | |
"- () 로 생성\n", | |
"- _1, _2, ... : 요소 접근, 1부터 시작 한다는 것에 주의\n", | |
"- 패턴 매칭 사용 요소 접근\n", | |
"- 하나 이상의 값을 리턴 하는 경우 사용" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 63, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"class scala.Tuple3" | |
] | |
}, | |
"execution_count": 63, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val tup = (1,3.14,\"Fred\")\n", | |
"tup.getClass" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3.14" | |
] | |
}, | |
"execution_count": 64, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"tup._2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 65, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"execution_count": 65, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val t1 = tup _1\n", | |
"t1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 66, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Fred" | |
] | |
}, | |
"execution_count": 66, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val (first, second, third) = tup\n", | |
"third" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 67, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3.14" | |
] | |
}, | |
"execution_count": 67, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val (_, second, _) = tup\n", | |
"second" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 68, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(NY,ew ork)" | |
] | |
}, | |
"execution_count": 68, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"New York\".partition(_.isUpper)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Zipping\n", | |
"- zip 메쏘드 : 값들을 묶어서 사용할때 사용\n", | |
"- toMap : Zipping 한 값들을 맵으로 변환" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 69, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Array((<,2), (-,10), (>,2))" | |
] | |
}, | |
"execution_count": 69, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val symbols = Array(\"<\",\"-\",\">\")\n", | |
"val counts = Array(2,10,2)\n", | |
"val pairs = symbols.zip(counts)\n", | |
"pairs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 70, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<<---------->>" | |
] | |
} | |
], | |
"source": [ | |
"for((s,n) <- symbols zip counts) print(s * n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 71, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Map(< -> 2, - -> 10, > -> 2)" | |
] | |
}, | |
"execution_count": 71, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"symbols.zip(counts).toMap" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Apache Toree - Scala", | |
"language": "scala", | |
"name": "apache_toree_scala" | |
}, | |
"language_info": { | |
"file_extension": ".scala", | |
"name": "scala", | |
"version": "2.11.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment