FPPiS Week 2 - codeport/scala GitHub Wiki

SBT

SBT์—์„œ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

  1. ์›ํ•˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ
  2. ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™ ํ›„ 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

http://www.scalatest.org/

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))

4. Scala Syntax Summary

5. Functions and Data

6. More Fun With Rationals

7. Evaluation and Operators

โš ๏ธ **GitHub.com Fallback** โš ๏ธ