scala/scala3
-
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..
-
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..
-
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 덕택에 이렇게 사용 가능 }
-
opaque typescala/scala3 2024. 3. 22. 14:21
object Company { opaque type Name = String object Name { def apply(s: String): Name = s } extension (name: Name) def length: Int = name.length // inside: Name String def fullName(first: Name, second: Name): Name = { first.concat(second) // String 의 method 를 사용 가능 } } @main def Main(): Unit = { import Company.* // Outside, Name & String are NOT related val first: Name = "First" // NOT allowed, Er..
-
Function value signature for generics, context functionsscala/scala3 2024. 3. 22. 01:01
import scala.concurrent.duration.* import scala.concurrent.{Await, ExecutionContext, Future} def processOption[A](opt: Option[A]): String = opt match { case Some(v) => s"[$v]" case None => "[]" } @main def main(): Unit = { /** Generics in functions */ val processOptionFunction: [A] => Option[A] => String = { [A] => (opt: Option[A]) => processOption(opt) } processOptionFunction(Some(1)) /** Conte..
-
typeclassscala/scala3 2024. 3. 22. 00:21
OLD but Good 방법trait Shape[A] { def area(a: A): Double}object Shape { def area[A](a: A)(using x: Shape[A]): Double = x.area(a) given Shape[Rect] = new Shape[Rect]{ override def area(a: Rect): Double = a.width * a.height } given Shape[Circle] = new Shape[Circle] { override def area(a: Circle): Double = 3.14 * a.radius * a.radius }}object ShapeSyntax { extension [A](a: A) { def are..