scala
-
kittens examplescala/cats2 2025. 3. 5. 20:56
https://github.com/typelevel/kittens "org.typelevel" %% "kittens" % "3.4.0" Example1import cats.Showimport cats.derived.semiautoimport cats.implicits.*case class Name(value: String)case class Person(name: Name, age: Int)object Person { given Show[Person] = semiauto.show}object Test { def main(args: Array[String]): Unit = { val person = Person(Name("KJM"), 20) println(person.show) // Per..
-
Tagless Finalscala/basic 2024. 11. 19. 15:09
https://www.reddit.com/r/scala/comments/s6ih9p/comment/htagml4/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button trait Expr[T] { def num(n: Int): T def add(l: T, r: T): T}implicit def eval: Expr[Int] = new Expr[Int] { def num(i: Int): Int = i def add(l: Int, r: Int): Int = l + r}implicit def show: Expr[String] = new Expr[String] { def num(i:..
-
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..
-
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..