๋ชฉ๋ก2019/04 (45)
KoreanFoodie's Study
์ค์นผ๋ผ ํํ ๋ฆฌ์ผ, scala์ sub types์ ๋ํด ์์๋ณด์ Sub Type Polymorphism (Concept) Let's assume that we want this: object tom { val name = "Tom" val home = "02-880-1234" } object bob { val name = "Bob" val home = "02-123-1234" } def greeting(r: ???) = "Hi " + r.name + ", How are you?" greeting(tom) greeting(bob) // Note that we have tom: {val name: String; val home: String} bob: {val name: String; val mobile: ..
์ค์นผ๋ผ ํํ ๋ฆฌ์ผ, scala์ parametric polymophism์ ๋ํด ์์๋ณด์ Parametric Polymorphism: Functions Problem def id1(x: Int): Int = x def id2(x: Double): Double = x Can we avoid DRY(Do not Reapeat Yourself)? Parametric Polymorphism(a.k.a. For-all Types) def id[A](x: A): A = x // type for definition val f = (x:Int) => x // type for value The type of id is [A](x: A)A. id is a parametric expression. Only definition..
์ค์นผ๋ผ ํํ ๋ฆฌ์ผ, scala datatypes part2 Pattern Matching A way to use algebraic datatypes Syntax: e match { case C1(...) => e1 ... case Cn(...) => en } 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())) def length(xs: IList) : Int = xs match { case INil() => 0 case ICons(x, tl) => 1 + length(t..
์ค์นผ๋ผ ํํ ๋ฆฌ์ผ, 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) ..
์ค์นผ๋ผ ํํ ๋ฆฌ์ผ, scala์์ currying์ ๋ํด ์์๋ณด์ Currying Why we use currying? Let's look at the code, simplified by currying. def sum(f: Int=>Int, a: Int, b: Int): Int = if (a Int): (Int, Int) => Int = { def sumF(a: Int, b: Int): Int = if (a Int)(a: Int, b: Int): Int = if (a T can be turned into one of type T1 => (T2 => (... Tn =>T) This is called 'currying' named after Haskell Brooks Curry. The opposite di..
์ค์นผ๋ผ ํํ ๋ฆฌ์ผ, scala์ closure์ ๋ํด ์์๋ณด์ Parameterized expression vs. values Functions defined using โdefโ are not values but parameterized expressions. โข Anonymous functions are values. โข But, parameterized expressions are implicitly converted to values. โข Explicit conversion: f _ โข Anonymous functions can be seen as syntactic sugar: (x:T)=>e is equivalent to { def \_\_noname(x:T) = e; \_\_noname \_ }..
์ค์นผ๋ผ ํํ ๋ฆฌ์ผ, scala์ tail recursion์ ์์๋ด ์๋ค Recursion needs care Let's look at typical summation fuction. def sum(n: Int) : Int = if (n x*x*x, n) // or def sumLinear(n: Int) = sum((x)=>x, n) def sumSquare(n: Int) = sum((x)=>x*x, n) def sumCubes(n: Int) = sum((x)=>x*x*x, n) Excercise def sum(f: Int => Int, a: Int, b: Int): Int = if (a Int, a: Int, b: Int): Int = if (a Int, inival: Int, f: Int => Int, ..
์ค์นผ๋ผ ํํ ๋ฆฌ์ผ, scala์์ lazy val์ ๋ํด ์์๋ณด์ Blocks and Name Scoping Blocks in scala Block is an expression. It also allow nested name binding. Block allows arbitraty order of "def"s, but not "val"s(think about why). Scope of names Block example : val t = 0 def f(x: Int) = t + g(x) def g(x: Int) = x * x val x = f(5) val r = { val t = 10 val s = f(5) t + s } val y = t + r A definition inside a block is on..
์ค์นผ๋ผ์์ call by value์ call by name์ ์ฐจ์ด๋ฅผ ์์๋ณด์ There is two strategy to evaluate values. Call-by-value Evaluate the arguments first, then apply the function to them Call-by-name Just apply the function to its arguments, without evaluating them Let's see the example. def square (x: Int) = x * x [cbv] square(1+1) ~ square(2) ~ 2 * 2 ~ 4 [cbn] square(1+1) ~ (1+1) * (1+1) ~ 2 * (1+1) ~ 2 * 2 ~ 4 So wha..
ํ์ด์ฌ์ผ๋ก ๋ง๋ ๊ฐ๋จํ ํฌ๋กค๋ฌ ์ฐธ๊ณ ์๋ฃ : ์น ํฌ๋กค๋ฌ ๋ง๋ค๊ธฐ, Implicit vs. Explict wait ๋น๊ตํ๊ธฐ ์ ํฌ๋ธ์์ ์์ ๋ค์ด๋ก๋ํ๋ ์์ ์๋ํ ํ๊ธฐ ์น๊ตฌ๊ฐ ์ ํฌ๋ธ๋ฅผ ํตํด ์์ ์ ๋ณํํด์ ๋ค์ด๋ก๋ ๋ฐ๋ ๊ฒ์ ๋ณด๊ณ , ์ด๋ฅผ ๊ฐํธํ๊ฒ ํด์ฃผ๊ณ ์ ํ์ด์ฌ selenium module, chromedriver์ ์ด์ฉํด ์คํํค(Win+R)๋ฅผ ํตํด ์ฝ๊ฒ ์์ ์ ๋ค์ด๋ก๋ํ๋ ์ฝ๋๋ฅผ ์์ฑํด ๋ณด์์ต๋๋ค. ์, ์ฐธ๊ณ ๋ก mp3juices.cc ๋ผ๋ ์ฌ์ดํธ๋ก๋ถํฐ ์์ ์ ๋ณํํด์ ๋ค์ด์ ํ๋๋ฐ, ์ฌ๊ธฐ๊ฐ ์ ์ผ ๊น๋ํ ๊ฒ ๊ฐ์์(์ ๊ธฐ์ค) Prerequsite : Python3 (Latest version is recommemded) Installing 3rd party modules (pyperclip, seleniu..