Created
July 1, 2018 09:24
-
-
Save mrbkdad/16cb82e20121489a6ba79ba8b26c6f7d to your computer and use it in GitHub Desktop.
learning scala chapter 1, 2
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": [ | |
"## 1. 기본\n", | |
"- 환경\n", | |
"- var, val\n", | |
"- 숫자 타입\n", | |
"- 연산자와 함수\n", | |
"- Scaladoc" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 환경\n", | |
"- scala 설치\n", | |
" - sbt 설치\n", | |
" - IDE : Intellij\n", | |
"- spark : stand alone(cluster, hadoop+yarn(mesos))\n", | |
" - 개발환경에서는 spark을 stand alone으로 설치하여 사용 가능\n", | |
" - 분산처리는 실제 이루어 지지않고 로컬 파일 시스템을 이용\n", | |
" - rdd, dataframe 테스트는 가능\n", | |
"#### spark 개발 환경\n", | |
"- IDE\n", | |
"- Jeppline\n", | |
"- Jupyter notebook + Apache Toree" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### val, var\n", | |
"- val : const(node), immutable\n", | |
"- var : var(node), mutable\n", | |
"- type : Byte, Char, Short, Int, Long, Float, Double, Boolean, String\n", | |
" - 모든 타입은 클래스\n", | |
" - RichByte, RichChar, ..., StringOps\n", | |
" - BigInt, BigDecimal : java.math.BigInteger, BigDecimal, 수학 연산자 사용 가능" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"int" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"42" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val answer = 8 * 5 + 2\n", | |
"print(answer.getClass)\n", | |
"answer" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Name: Compile Error\n", | |
"Message: <console>:19: error: reassignment to val\n", | |
" answer = 10\n", | |
" ^\n", | |
"StackTrace: " | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"answer = 10" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"int" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"0" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"var count = 0\n", | |
"print(count.getClass)\n", | |
"count" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"10" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"count = 10\n", | |
"count" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"val greeting: String = null\n", | |
"val greeting: Any = \"Hello\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"(100,100)" | |
] | |
} | |
], | |
"source": [ | |
"val xmax,ymax : Int = 100\n", | |
"print(xmax,ymax)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"class java.lang.String" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"count.toString.getClass" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 연산자\n", | |
"- \"+, -, *, /, %\"\n", | |
"- \"&, |, ^, >>, <<\"\n", | |
"- 스칼라에서는 연산자도 메소드 : https://www.scala-lang.org/api/current/scala/Int.html" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"20" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val a, b = 10\n", | |
"a + b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"20" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a.+(b)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### 스칼라에서는 특수문자를 사용하여 함수 정의 가능\n", | |
"#### 함수 호출시 인자가 없는 경우 () 생략 가능\n", | |
"#### 인자가 하나만 있는 경우 연산자 처럼 사용 가능\n", | |
"- a fun b = a.fun(b)\n", | |
"#### ++, -- 연산자 없음, +=, -= 사용" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 함수 와 메소드\n", | |
"- 스칼라에서는 함수 클래스가 존재" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"// _ 자바의 *, scala 생략 가능 - import math._, math.sqrt(2)\n", | |
"import scala.math._" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1.4142135623730951" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"math.sqrt(2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"16.0" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"pow(2,4)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3.0" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"min(3,Pi)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### apply 메소드\n", | |
"- company object apply 정의\n", | |
"- 예) String" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"t" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val s = \"test\"\n", | |
"s(0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"t" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"s.apply(0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Array(1, 2, 3)" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Array(1,2,3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Array(1, 2, 3)" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"Array.apply(1,2,3)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Scaladoc\n", | |
"- https://www.scala-lang.org/api/current/index.html\n", | |
"- 기본 타입들은 Rich 클래스 참조\n", | |
"- String은 StringOps 참조\n", | |
"- 수학함수는 scala.math 참조\n", | |
"- unary_- : -x 연산자 정의\n", | |
"- implicit : 자동변환\n", | |
"- 함수도 메소드의 인자로 사용 가능\n", | |
"- 가능할 필요한 메소드를 먼저 찾기\n", | |
"- 이해 안되는 인자는 implicit 고려" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 2. 제어 구조와 함수\n", | |
"- if\n", | |
"- 블럭\n", | |
"- for\n", | |
"- ; 생략 가능 ( java script )\n", | |
"- Unit : java의 void 와 유사\n", | |
"- = 없는 함수정의\n", | |
"- exception, catch 문 pattern matching" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### if\n", | |
"- if 문이나 else 문의 값이 return\n", | |
"- 삼항 연산으로 활용" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val x = 10\n", | |
"val s = if( x > 0 ) 1 else -1\n", | |
"s" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"()" | |
] | |
}, | |
"execution_count": 19, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"if(x < 0) 1 // Unit type - else ()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"20" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"if ( x > 0 ) print(x); x * 2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"12" | |
] | |
}, | |
"execution_count": 21, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"if ( x > 0 ){\n", | |
" print(x)\n", | |
" x+ 2\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 블럭\n", | |
"- {}\n", | |
"- 마지막 식의 값 리턴\n", | |
"- 할당은 Unit" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3.605551275463989" | |
] | |
}, | |
"execution_count": 22, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val (x0,y0) = (1,1)\n", | |
"val (x,y) = (3, 4)\n", | |
"val distance = {\n", | |
" val dx = x - x0; val dy = y - y0\n", | |
" math.sqrt(dx*dx + dy*dy)\n", | |
"}\n", | |
"distance" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"var r = 2\n", | |
"var n = 1\n", | |
"{ r = r * 10 ; n -= 1}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"200" | |
] | |
}, | |
"execution_count": 24, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"{ r * 10 }" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Name: Compile Error\n", | |
"Message: <console>:24: error: type mismatch;\n", | |
" found : Unit\n", | |
" required: Int\n", | |
" r = n = 2\n", | |
" ^\n", | |
"StackTrace: " | |
] | |
}, | |
"execution_count": 25, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"r = n = 2" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 입력과 출력\n", | |
"- print, println\n", | |
"- readLine(\"Your name: \")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"23" | |
] | |
} | |
], | |
"source": [ | |
"println(1);print(2);print(3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"23" | |
] | |
} | |
], | |
"source": [ | |
"print(\"1\\n\" + 23)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Hello, Fred! You are 42 years old.\n" | |
] | |
} | |
], | |
"source": [ | |
"printf(\"Hello, %s! You are %d years old.\\n\",\"Fred\",42)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 루프\n", | |
"- while loop\n", | |
"- do loop\n", | |
"- for loop" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"6" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"var (r,n) = (1,3)\n", | |
"while(n > 0){\n", | |
" r = r * n\n", | |
" n -= 1\n", | |
"}\n", | |
"r" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"6" | |
] | |
}, | |
"execution_count": 30, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"var (r,n) = (1,3)\n", | |
"for(i <- 1 to n) r = r * i\n", | |
"// for loop 안의 변수에 var/val 없음, collection의 원소 type\n", | |
"r" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)" | |
] | |
}, | |
"execution_count": 31, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"1 to 10" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Range(1, 2, 3, 4, 5, 6, 7, 8, 9)" | |
] | |
}, | |
"execution_count": 32, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"1 until 10" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"500" | |
] | |
}, | |
"execution_count": 33, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val str = \"Hello\"\n", | |
"var sum = 0\n", | |
"for(i <- 0 until str.length) sum += str(i)\n", | |
"sum" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"500" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"var sum = 0\n", | |
"for(c <- \"Hello\") sum += c\n", | |
"sum" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"500" | |
] | |
}, | |
"execution_count": 35, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"{for(c<-\"Hello\") yield c}.sum.toInt" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"500" | |
] | |
}, | |
"execution_count": 36, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\"Hello\".sum.toInt" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 고급 for 루프와 for 컴프리헨션\n", | |
"- 이중 루프 표현\n", | |
"- 가드 설정\n", | |
"- yield : 콜렉션 생성 컴프리헨션, 첫번째 루프(제너레이터)에 따라 타입 결정" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"12 13 21 23 31 32 " | |
] | |
} | |
], | |
"source": [ | |
"for(i <- 1 to 3;j <- 1 to 3 if i != j) print((10*i + j) + \" \")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"13 22 23 31 32 33 " | |
] | |
} | |
], | |
"source": [ | |
"for(i <- 1 to 3; from = 4 - i; j <- from to 3) print((10*i + j) + \" \")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Vector(1, 2, 0, 1, 2, 0, 1, 2, 0, 1)" | |
] | |
}, | |
"execution_count": 39, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"for(i <- 1 to 10) yield i % 3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"HIeflmlmop" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"for(c <- \"Hello\"; i <- 0 to 1) yield(c+i).toChar" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Vector(H, e, l, l, o, I, f, m, m, p)" | |
] | |
}, | |
"execution_count": 41, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"for(i <- 0 to 1; c <- \"Hello\") yield(c + i).toChar" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 함수\n", | |
"- 메소드 : 오브젝트 위에서 동작\n", | |
"- 함수 : 클래스 - 자바 정적 메소드 활용\n", | |
"- 재귀함수인 경우 반드시 타입 지정" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"// def abs(x: Double):Double = if (x >= 0) x else -x\n", | |
"def abs(x: Double) = if (x >= 0) x else -x" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"10.0" | |
] | |
}, | |
"execution_count": 43, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"abs(-10)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def fac(n:Int) = {\n", | |
" var r = 1\n", | |
" for(i <- 1 to n) r = r * i\n", | |
" r\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"24" | |
] | |
}, | |
"execution_count": 45, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"fac(4)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Name: Compile Error\n", | |
"Message: <console>:23: error: recursive method rfac needs result type\n", | |
" def rfac(n:Int) = if(n<=0) 1 else n*rfac(n-1)\n", | |
" ^\n", | |
"StackTrace: " | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"// 재귀함수 타입 지정\n", | |
"def rfac(n:Int) = if(n<=0) 1 else n*rfac(n-1)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 기본 인자와 이름 인자" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def decorate(str:String, left:String = \"[\", right:String = \"]\") = left + str + right" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[hello]" | |
] | |
}, | |
"execution_count": 48, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"decorate(\"hello\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<<<hello]" | |
] | |
}, | |
"execution_count": 49, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"// 앞에서 부터 적용\n", | |
"decorate(\"hello\",\"<<<\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[hello<<<" | |
] | |
}, | |
"execution_count": 50, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"decorate(\"hello\",right=\"<<<\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"==hello]" | |
] | |
}, | |
"execution_count": 51, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"decorate(left=\"==\",str=\"hello\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 가변 인자\n", | |
"- name: Type*\n", | |
"- Seq Type\n", | |
"- collection type -> Seq parameter : (: _*)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def sum(args: Int*) = {\n", | |
" var result = 0\n", | |
" for(i <- args) result += i\n", | |
" result\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def sum(args: Int*) = {for(i <- args) yield i}.sum" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 54, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def sum(args: Int*) = args.asInstanceOf[Seq[Int]].sum" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"10" | |
] | |
}, | |
"execution_count": 55, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sum(1,2,3,4)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"execution_count": 56, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sum(1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Name: Compile Error\n", | |
"Message: <console>:23: error: type mismatch;\n", | |
" found : scala.collection.immutable.Range.Inclusive\n", | |
" required: Int\n", | |
" sum(1 to 5)\n", | |
" ^\n", | |
"StackTrace: " | |
] | |
}, | |
"execution_count": 57, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sum(1 to 5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"15" | |
] | |
}, | |
"execution_count": 58, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sum(1 to 5:_*) // Seq parameter" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"7" | |
] | |
}, | |
"execution_count": 59, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sum(Array(1,2,4):_*) // Seq Parameter" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def rsum(args: Int*): Int = {\n", | |
" if(args.length == 0) 0\n", | |
" else args.head + rsum(args.tail:_*)\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"15" | |
] | |
}, | |
"execution_count": 61, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"rsum(1 to 5:_*)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 프로시저\n", | |
"- return type : Unit\n", | |
"- = 사용하지 않음\n", | |
"- side effect 용\n", | |
"- Unit를 명시적으로 사용을 권장하기도 함" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 62, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def box(s:String){\n", | |
" val border = \"-\" * s.length + \"--\\n\"\n", | |
" println(border + \"|\" + s + \"|\\n\" + border)\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 63, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"------\n", | |
"|test|\n", | |
"------\n", | |
"\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"void" | |
] | |
}, | |
"execution_count": 63, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val a = box(\"test\")\n", | |
"a.getClass" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 64, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def ubox(s:String):Unit = {\n", | |
" val border = \"-\" * s.length + \"--\\n\"\n", | |
" println(border + \"|\" + s + \"|\\n\" + border)\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 65, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"------\n", | |
"|test|\n", | |
"------\n", | |
"\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"void" | |
] | |
}, | |
"execution_count": 65, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"val a = box(\"test\")\n", | |
"a.getClass" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 레이지\n", | |
"- lazy val name : 최초 접근시 초기화\n", | |
"- val 과 def 의 중간 형태\n", | |
"- 레이지 값을 접근할 때마다 쓰레드 세이프하게 초기화 여부 메소드 호출(비용 발생)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 66, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"val" | |
] | |
} | |
], | |
"source": [ | |
"val words1 = {\n", | |
" print(\"val\")\n", | |
" scala.io.Source.fromFile(\"test.txt\").mkString\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 67, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"lazy val words2 = {\n", | |
" print(\"lazy\")\n", | |
" scala.io.Source.fromFile(\"test.txt\").mkString\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 68, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def words3 = {\n", | |
" print(\"def\")\n", | |
" scala.io.Source.fromFile(\"test.txt\").mkString\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 69, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"test" | |
] | |
}, | |
"execution_count": 69, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"words2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 70, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"def" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"test" | |
] | |
}, | |
"execution_count": 70, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"words3" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 71, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"def" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"test" | |
] | |
}, | |
"execution_count": 71, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"words3" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 예외\n", | |
"- 자바나 C++과 같은 방법으로 동작\n", | |
"- java.lang.Throwable 서브 클래스\n", | |
"- 체크 예외가 없음 : 함수나 메소드에서 선언할 필요 없음\n", | |
"- 자바에서는 체크 예외는 컴파일 타임에 체크됨\n", | |
"- 스칼라에서는 컴파일 타임에 체크가 항상 좋은 것이 아님을 반영\n", | |
"- catch 문에 패턴 매칭 문법 사용\n", | |
"- throw : Nothing 타입\n", | |
"- finally : 예외 여부와 상관없이 마지막에 실행\n", | |
" - 아래 형태로 사용되나 유용하지 않음\n", | |
" - try{...} catch{...} finally{...}\n", | |
" - try{ try{...}catch{...} } finally{...}\n", | |
"<pre>\n", | |
"ex)\n", | |
"val x = -1\n", | |
"if(x >= 0){ sqrt(x)\n", | |
"}else throw new IllegalArgumentException(\"x should not be negative\")\n", | |
"\n", | |
"try{\n", | |
" process(new URL(\"http://horstmann.com/fred-tiny.gif\"))\n", | |
"}catch{\n", | |
" case url: MalformedURLException => println(\"Bad URL: \" + url)\n", | |
" case ex: IOException => ex.printStackTrace()\n", | |
" case _: println(\"Exception occured\")\n", | |
"}\n", | |
"</pre>" | |
] | |
}, | |
{ | |
"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