scala
-
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..
-
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
공식문서: https://docs.scala-lang.org/scala3/reference/contextual/type-classes.html#(import 된 given 의 내부에 들어있는 extension method 는 자동으로 꺼내져 적용된다?)예제1case class Rect(width: Double, height: Double)case class Circle(radius: Double)trait Shape[A] { extension (a: A) { def area(): Double }}object Shape { // given listLast[T](using ...): Last[List[T]] = ... 형태로 사용도 가능 (generics 경우) given Shape[Rect] ..