{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Some과 Option\n", "\n", "* 기존 /\n", "a = 1 (값이 있음), a = null (값이 없음)\n", "\n", "* Option 사용 / \n", "a = Some(1) (값이 있음), a = None (값이 없음)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[36ma\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m1\u001b[39m" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val a = 1" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "cmd1.sc:3: an expression of type Null is ineligible for implicit conversion\n", "val res1_1 = a = null\n", " ^" ] }, { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "Compilation Failed" ] } ], "source": [ "var a = 1\n", "// scala는 a에 null을 대입할 수 없음.\n", "a = null" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[36ma\u001b[39m: \u001b[32mSome\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mSome\u001b[39m(\u001b[32m1\u001b[39m)\n", "\u001b[36mres1_1\u001b[39m: \u001b[32mOption\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mSome\u001b[39m(\u001b[32m2\u001b[39m)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val a = Some(1)\n", "// if(a.isEmpty) null else a.get + 1 안좋은 코드 예시\n", "a.map(_ + 1)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[36mres2\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m1\u001b[39m" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.getOrElse(-1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[36ma\u001b[39m: \u001b[32mNone\u001b[39m.type = None" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val a = None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Functional Literal" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[36mlist\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m, \u001b[32m4\u001b[39m)\n", "\u001b[36mres4_1\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m)\n", "\u001b[36mtestNumber1\u001b[39m: \u001b[32mInt\u001b[39m => \u001b[32mBoolean\u001b[39m = \n", "\u001b[36mres4_3\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m)\n", "\u001b[36mres4_4\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m)\n", "\u001b[36mres4_5\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m)\n", "defined \u001b[32mfunction\u001b[39m \u001b[36mtestNumber2\u001b[39m\n", "\u001b[36mres4_7\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m)\n", "\u001b[36mres4_8\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m)\n", "\u001b[36mres4_9\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m)\n", "\u001b[36mres4_10\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val list = List(1, 2, 3, 4)\n", "list.filter((x: Int) => x < 3)\n", "val testNumber1 = (x: Int) => x < 3 // function as a 1st-class object! list.filter(testNumber1)\n", "list.filter((x) => x < 3) // target typing\n", "list.filter(x => x < 3)\n", "list.filter(_ < 3) // placeholder\n", "def testNumber2(x: Int) = x < 3 // function\n", "list.filter(x => testNumber2(x))\n", "list.filter(testNumber2(_))\n", "list.filter(testNumber2 _)\n", "list.filter(testNumber2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Pattern Matching & Case Class\n", "• Java의 switch ~ case 와 비슷하지만, 훨씬 강력한 도구" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[36ma\u001b[39m: \u001b[32mList\u001b[39m[(\u001b[32mInt\u001b[39m, (\u001b[32mInt\u001b[39m, \u001b[32mInt\u001b[39m))] = \u001b[33mList\u001b[39m((\u001b[32m1\u001b[39m, (\u001b[32m0\u001b[39m, \u001b[32m4\u001b[39m)), (\u001b[32m2\u001b[39m, (\u001b[32m0\u001b[39m, \u001b[32m5\u001b[39m)))" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val a = List((1,(0,4)), (2,(0,5)))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "5\n" ] } ], "source": [ "a.map(_._2._2) foreach println // 튜플의 key의 right 원소를 뽑고 싶음. 보기 불편함." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "5\n" ] } ], "source": [ "a.map{case(key,(left,right)) => right} foreach println" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Implicit Conversion\n", "• 기능의 확장을 편하게 하고싶을때\n", "\n", "• 예상되는 타입으로 변환하는 함수를 정의해놓고, 자동으로 적용" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] }, { "data": { "text/plain": [ "defined \u001b[32mfunction\u001b[39m \u001b[36mstringToInt\u001b[39m\n", "defined \u001b[32mfunction\u001b[39m \u001b[36mprintNumber\u001b[39m" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "implicit def stringToInt(number: String): Int = { \n", " number match {\n", " case \"one\" => 1\n", " case \"two\" => 2 \n", " }\n", "}\n", "def printNumber(n: Int) = println(n)\n", "printNumber(\"one\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Implicit Conversion 활용 \n", "더 직관적이고 일관성 있는 코드를 만들 수 있다!" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[36mres9_0\u001b[39m: \u001b[32mjava\u001b[39m.\u001b[32mutil\u001b[39m.\u001b[32mDate\u001b[39m = Tue May 29 20:47:26 KST 2018\n", "\u001b[36mres9_1\u001b[39m: \u001b[32mjava\u001b[39m.\u001b[32mutil\u001b[39m.\u001b[32mDate\u001b[39m = Tue May 29 20:47:26 KST 2018\n", "defined \u001b[32mobject\u001b[39m \u001b[36mDateParser\u001b[39m\n", "\u001b[36mres9_3\u001b[39m: \u001b[32mjava\u001b[39m.\u001b[32mutil\u001b[39m.\u001b[32mDate\u001b[39m = Tue May 29 20:47:26 KST 2018\n", "defined \u001b[32mclass\u001b[39m \u001b[36mDateConverter\u001b[39m\n", "defined \u001b[32mfunction\u001b[39m \u001b[36mstring2DateConverter\u001b[39m\n", "\u001b[36mres9_6\u001b[39m: \u001b[32mjava\u001b[39m.\u001b[32mutil\u001b[39m.\u001b[32mDate\u001b[39m = Tue May 29 20:47:26 KST 2018" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DateParser.parse(\"2017-07-29\") // java style\n", "\"2017-07-29\".toDateTime // better solution using implicit conversion\n", "object DateParser {\n", " def parse(dateString :String) = new java.util.Date\n", "}\n", "DateParser.parse(\"2017-07-29\")\n", "class DateConverter(val s: String) {\n", " def toDateTime = DateParser.parse(s)\n", "}\n", "implicit def string2DateConverter(s: String) = new DateConverter(s)\n", "\"2017-07-29\".toDateTime" ] } ], "metadata": { "kernelspec": { "display_name": "Scala", "language": "scala", "name": "scala" }, "language_info": { "codemirror_mode": "text/x-scala", "file_extension": ".scala", "mimetype": "text/x-scala", "name": "scala211", "nbconvert_exporter": "script", "pygments_lexer": "scala", "version": "2.11.11" } }, "nbformat": 4, "nbformat_minor": 2 }