분류 전체보기
-
type lambdasscala/scala3 2024. 4. 23. 14:55
object Test { type MyList[A] = List[A] type MyListV2 = [A] =>> List[A] type MyMap[K, V] = Map[K, V] type MyMapV2 = [K, V] =>> Map[K, V] class Functor[F[_]] type MyFunctor[F[_]] = Functor[F] type MyFunctorV2 = [F[_]] =>> Functor[F] trait Monad[M[_]] { def flatMap[A, B](fa: M[A])(f: A => M[B]): M[B] } class ZIO[R, E, A] class ZIOMonad[R, E] extends Monad[[A] =>> ZIO[R, E, A]] { override def flatMa..
-
Multi Stagedocker 2024. 4. 17. 18:40
참고: https://nesoy.github.io/articles/2020-11/Docker-multi-stage-build 예제 FROM golang:1.7.3 AS builder WORKDIR /go/src/github.com/alexellis/href-counter/ RUN go get -d -v golang.org/x/net/html COPY app.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /root/ COPY --from=builder /go/src/github.com/alexellis/h..
-
Function Contextscala/scala3 2024. 4. 12. 17:00
https://blog.softwaremill.com/context-is-king-20f533474cb3 에 자세히 설명되어 있다. 아래는 내가 만들어 본 예제: function context 를 이용한 dependency injection import scala.concurrent.{Await, ExecutionContext, Future} case class Nami(x: Int) object ContextFunction { def f()(using executionContext: ExecutionContext): Future[Int] = Future { 1 * 2 } def g(y: Int)(using nami:Nami, executionContext: ExecutionContext): Future..
-
Helidon Nima Serverscala/tapir 2024. 4. 8. 13:15
GitHub Code https://github.com/windbird123/scala3-tapir-helidon Gzip compression (Helidon Nima) 아래 dependency 를 추가하면 자동으로 적용되는 듯 하다. (build.sbt) libraryDependencies += "io.helidon.http.encoding" % "helidon-http-encoding-gzip" % "4.0.7" libraryDependencies += "io.helidon.http.encoding" % "helidon-http-encoding-deflate" % "4.0.7" HOCON config (Helidon Nima) dependency (build.sbt) libraryDependenci..
-
1.7 꼬리 재귀scala/functional programming 2024. 4. 4. 20:53
tail recursion: If a function calls itself as its last action, the function's stack frame can be reused. Exercise factorial() 을 꼬리재귀 버전으로 구현하라. import scala.annotation.tailrec def factorial(n: Long): Long = { @tailrec def fact(n: Long, acc: Long): Long = if (n == 0) acc else fact(n - 1, n * acc) fact(n, 1) } @main def Main(): Unit = { val r = factorial(10) println(r) }
-
1.6 블록과 어휘 범위scala/functional programming 2024. 4. 4. 19:58
예제: 뉴턴의 방법 의 코드 문제는 너무 많은 function 이 있다는 점이다. sqrt() function 안으로 관련 코드를 넣고, 기존에 중복으로 사용된 x 값을 조정하면 아래처럼 개선할 수 있다. def abs(x: Double): Double = if x > 0 then x else -x def sqrt(x: Double): Double = { def isGoodEnough(guess: Double): Boolean = abs(guess * guess - x) < 0.001 def improve(guess: Double): Double = (guess + x / guess) / 2 def sqrtIter(guess: Double): Double = if isGoodEnough(guess) th..
-
1.5 예제: 뉴턴의 방법을 사용한 제곱근scala/functional programming 2024. 4. 4. 19:48
뉴턴의 방법 알고리즘 sqrt(x) = y 를 구하기 위해 1. 초기 예측값 선정 (y=1 로 선택) 2. 다음 예측값 = mean(y, x/y) 을 반복하다 보면 점점 정확해 짐 예제 sqrt(2) 를 계산해 보자. EstimationQuotientMean Estimation Quotient Mean 1 2 / 1 1.5 1.5 2 / 1.5 = 1.333 1.4167 1.4167 2 / 1.4167 1.4142 1.4142 ... ... 코드 def abs(x: Double): Double = if x > 0 then x else -x def isGoodEnough(x: Double, guess: Double): Boolean = abs(guess * guess - x) < 0.001 def impr..
-
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..