분류 전체보기
-
1.4 조건과 값 정의scala/functional programming 2024. 4. 4. 19:47
Short circuit evaluation 표현식을 평가하지 않아도 결과를 알 수 있는 것을 단락평가(short circuit evaluation) 이라고 한다. 다음 예를 보자. false && e // false (e 의 평가와 관계 없이) true || e // true (e의 평과와 관계 없이) Value definitions def loop: Boolean = loop val x = loop // infinite loop def x = loop // OK 다음 function 을 if 를 사용해 구현해 보자. and(x, y) == x && y call by value 구현 def and(x: Boolean, y: Boolean): Boolean = if x then y else false d..
-
1.3 평가 전략 및 종료scala/functional programming 2024. 4. 4. 19:47
평가(Evaluation) 종료 call by value 로 expression 이 종료되면, call by name 으로도 expression 이 종료된다. 그러나 call by name 으로 expression 이 종료된다고 해서, 항상 call by value 로 expression 이 종료되는 것은 아니다. 다음 예를 살펴보자 def loop: Int = loop def first(x: Int, y: Int) = x call by value first(1, loop) x=1 과 loop 가 evaluation 되어 expression 이 종료되지 않는다. call by name first(1, loop) x=1 만 evaluation 되어 (loop 는 evaluation 되지 않고) expres..
-
1.2 프로그래밍의 요소scala/functional programming 2024. 4. 4. 19:47
치환 모델 (substitution model) substitution model: evaluation 을 통해 expression 을 value 로 reduce 하는 과정 그러면 모든 expression 은 value 로 reduce 될 수 있을까? 다음 예를 보면 아니라는 것을 알 수 있다. def loop: Int = loop loop Call by value vs Call by name def test(x:Int, y:Int) = x * x 위의 코드에서 call by name 과 call by value 중 어느것이 더 유리할까? 연산 횟수를 비교해 보자 code #steps of call by value #steps of call by value test(2,3) 1번 1번 test(3+4, ..
-
Either using cats and oxscala/scala3 2024. 4. 4. 19:06
cats 와 ox 를 활용해 Either 를 강력하게 사용하기 Cats Either Helper.asRight .asLeft.leftMap .mapEither.catchNonFatal, Either.catchOnlyEither.fromTry, Either.fromOptionOx Either Helperboundary break 코드 libraryDependencies += "org.typelevel" %% "cats-core" % "2.10.0", libraryDependencies += "com.softwaremill.ox" %% "core" % "0.1.0"import cats.*import cats.syntax.all.*import scala.util.{Fail..
-
play-json 에서 scala3 enum 사용하기scala/scala3 2024. 3. 29. 17:31
import play.api.libs.json.{Format, JsValue, Json} enum Age { case Old, Young } case class Person(name: String, age: Option[Age]) object Person { given Format[Age] = summon[Format[String]].bimap[Age](s => Age.valueOf(s), age => age.toString) given Format[Person] = Json.format[Person] } @main def Main(): Unit = { import Age.* val person = Person("KIM", Some(Young)) val js: JsValue = Json.toJson(pe..
-
container 간 통신docker 2024. 3. 28. 17:35
(naive) container IP 로 직접 통신 아래 명령 실행 docker inspect ... 여기서 NetworkSettings > Networks > IPAddress 로 알아내 IP 로 직접 접근 더 나은 방식 1. docker network 생성 docker network create fav-net docker network ls 2-1. DB container 를 fav-net 에서 실행 docker run -d --name mongo_db --network fav-net mongo 2-2. web application 실행 web application 코드에서 주소 부분을 아래처럼 수정 & fav-app 도커 이미지 빌드 mongodb://mongo_db:27017/fav # mongo..
-
Exportsscala/scala3 2024. 3. 22. 14:30
class Calculator { val SPEED_OF_LIGHT = 299792458 def energy(mass: Double): Double = mass * SPEED_OF_LIGHT * SPEED_OF_LIGHT } object MyApp { val calculator = new Calculator export calculator.energy def fusionReactor(): Unit = println(energy(0.001)) // export 덕택에 energy 직접 호출 가능 } @main def Main(): Unit = { val mass = 10 MyApp.energy(mass) // export 덕택에 이렇게 사용 가능 }