Fppis week 2 - codeport/scala GitHub Wiki
SBT
SBT์์ ํ๋ก์ ํธ ์์ฑ
- ์ํ๋ ๋๋ ํ ๋ฆฌ ์์ฑ
- ๋๋ ํ ๋ฆฌ๋ก ์ด๋ ํ
build.sbt
ํ์ผ ์์ฑ
build.sbt
sbt ํ๋ก์ ํธ ์ค์ ํ์ผ
scalaVersion := "2.10.2"
name := "testSbt"
version := "0.1"
- scalaVersion : ์ค์นผ๋ผ์ ๋ฒ์
- name : ํ๋ก์ ํธ์ ์ด๋ฆ
- version : ํ๋ก์ ํธ ๋ฒ์
Dependencies ์ถ๊ฐ
๊ฐ๊ฐ์ dependency๋ฅผ ํ ๋ผ์ธ์ฉ ์ถ๊ฐ(์ฃผ์ : ๊ฐ๊ฐ์๋ ํ ๋ผ์ธ์ฉ ๊ณต๋ฐฑ์ด ์กด์ฌํด์ผ ํจ)
libraryDependencies += "org.scalatest" %% "scalatest" % "1.9.1" % "test"
libraryDependencies += "junit" % "junit" % "4.10" % "test"
๊ฐ๊ฐ์ ๋ผ์ธ ์ฌ์ด์๋ ๊ณต๋ฐฑ์ด ์กด์ฌํด์ผ ํจ. ๋จ ๋ค์๊ณผ ๊ฐ์ด ๋ถ์ฌ ์ธ ์ ์์(์ฃผ์ : ํ๋ผ์ธ์ฉ ์์ฑํ ๋ +=
์ ์ฌ์ฉํ๋๋ฐ ๋ฐํด, ++=
๋ฅผ ์ฌ์ฉํจ์ ์ ์ํ๋ค)
์ถ๊ฐํ์
- SBT๋ dependency ๊ด๋ฆฌ๋ก Apache Ivy๋ฅผ ์ฌ์ฉ
libraryDependencies += groupID % artifactID % revision % configuration
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "1.9.1" % "test",
"junit" % "junit" % "4.10" % "test"
)
%%
libraryDependencies += "org.scala-tools" % "scala-stm_2.9.1" % "0.3"
๋ค์ ์ฝ๋์ %% "scala-stm
์ scala-stm_2.9.0
๋ ์ธ์ํจ
libraryDependencies += "org.scala-tools" %% "scala-stm" % "0.3"
ํ๋ฌ๊ทธ์ธ ์ถ๊ฐ
/project/plugins.sbt
์ ๋ค์์ ์ถ๊ฐ(์ดํด๋ฆฝ์ค ํ๋ฌ๊ทธ์ธ)
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.2.0")
ScalaTest, JUnit์ด ์ถ๊ฐ ๋ build.sbt ์์
SBT command
compile
: ์ปดํ์ผrun
: ์คํtest
: ํ ์คํธ ์ํtest-only
: ์ง์ ํ ํ ์คํธ๋ง์ ์ํupdate
: ์์กด์ฑ ์ ๋ฐ์ดํธ~
: ์์ค๋ณ๊ฒฝ ๊ฐ์- ์๋ฅผ ๋ค์ด
~ run
์ด๋ผ๊ณ ์ ๋ ฅํ๊ณ ์์ค๋ฅผ ์์ ํ๋ฉด, ์ด๋ฅผ ๊ฐ์งํ๊ณ ์คํ
์ฐธ๊ณ
Scala Test
FPPiS Week 2 - Higher Order Functions
1. Higher-Order Functions
// TODO
2. Currying
// TODO
3. Example: Finding Fixed Points
x, f(x), f(f(x)), f(f(f(x))),... ํํ๋ก ์งํ ๋ ๊ฒฝ์ฐ, ํน์ ๊ฐ์ ๊ทผ์ ํ๊ฒ ๋๋ค. ์ด ์๋ฅผ fixed point๋ผ๊ณ ํ๋ค. ์๋ฅผ ๋ค์ด cos ๊ฐ์ ์์์ ์๋ฅผ ๋ฃ์ ํ ๊ณ์ ๊ณ์ cos์ ์ ์ฉํ๋ฉด 0.73908... ์ ๊ทผ์ ํ๊ฒ ๋๋ค. ์ฐธ๊ณ : ์ํคํผ๋์
def isClosedEnough(x: Double, y: Double) = Math.abs((x - y) / x) / x < 0.00001
def fixedPoint(f: Double => Double) = {
def iterate(guess: Double): Double = {
val next = f(guess)
if (isClosedEnough(guess, next)) next else iterate(next)
}
iterate(1)
}
def sqrt(x: Double) = fixedPoint(y => (y + x / y) / 2)
ํ๋ฆฌ๋ฏธํฐ ํจ์์ / 2
์ ์ฃผ์ด damping ํ์ง ์๋ ๊ฒฝ์ฐ๋ ๊ฐ์ ๊ฐ์ผ๋ก ์ง๋ํ๊ฒ ๋๋ค. ์ด ๋ถ๋ถ์ ๋ค์๊ณผ ๊ฐ์ด ์ถ์ํ ํ ์ ์๋ค.
def averageDamp(f: Double => Double)(x: Double) = (f(x) + x) / 2
def sqrt(x: Double) => fixedPoint(averageDamp(y => x / y))