scala
-
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..
-
method 이름은 같은데 param, return 타입을 다르게 구현하기scala/basic 2024. 3. 7. 17:06
case class GoogleRaw(content: String, crawlId: String) case class NaverRaw(content: String) case class GoogleParsed(sections: Seq[String], crawlId: String) case class NaverParsed(sections: Seq[String]) trait Cralwer { type Raw type Parsed def fetch(url: String): Raw def parse(raw: Raw): Parsed def save(raw: Raw, parsed: Parsed): Int } object GoogleCrawler extends Cralwer { type Raw = GoogleRaw t..
-
기본 사용법scala/scala-cli 2024. 1. 27. 20:29
https://scala-cli.virtuslab.org/ 사용법 소개 (scala 2.13 을 사용 중인데, scala 3 survey 에 scala-cli 를 활용해 보았다.) Run scripts hello.sc def helloMessage(names: Seq[String]) = names match case Nil => "Hello!" case names => names.mkString("Hello: ", ", ", "!") println(helloMessage(args.toSeq)) println(os.pwd) 실행 scala-cli \ --jvm 21 \ --dep com.lihaoyi::os-lib:0.9.0 \ hello.sc -- James Candy Run scala code Test..
-
Eval and Trampoliningscala/cats2 2023. 7. 29. 11:43
scala with cats 에 나오는 Eval.defer 를 이용한 factorial 구현 def factorial(n: BigInt): Eval[BigInt] = if(n == 1) { Eval.now(n) } else { Eval.defer(factorial(n - 1).map(_ * n)) } factorial(50000).value 내가 생각해본 Eval.defer 를 사용하지 않고 factorial 구현 def factorial(n: BigInt): Eval[BigInt] = { if (n == 1) Eval.now(n) else { for { x
-
Request & URIscala/sttp 2023. 7. 18. 15:09
sttp 사용법 소개한다. 공식 메뉴얼 대신에 scala toolkit 에 나오는 간단한 예제를 옮겨봤다. libraryDependencies += "com.softwaremill.sttp.client4" %% "core" % "4.0.0-M2" object SttpMain { def main(args: Array[String]): Unit = { import sttp.client4.Response import sttp.client4.quick._ val response: Response[String] = quickRequest .get(uri"https://httpbin.org/get") .send() println(response.code) println(response.body) //////////..
-
airframe-control: Parallelscala/airframe 2023. 7. 6. 16:39
airframe-control 의 Parallel 을 테스트 해 본다. 코드 import wvlet.airframe.control.Parallel import java.text.SimpleDateFormat import java.util.Date object AirframeParallel { def now(): String = { val pattern = "HH:mm:ss.SSSZ" val simpleDateFormat = new SimpleDateFormat(pattern) simpleDateFormat.format(new Date()) } def main(args: Array[String]): Unit = { val source: Seq[Int] = Seq(1, 2, 3, 4, 5) println(s..