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