scala/cats2
-
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
-
Future Traversescala/cats2 2023. 2. 13. 12:39
https://github.com/typelevel/cats/issues/4176 에서 처럼, cats-2.7 이후 부터는 더 이상 traverse 로 Future 가 동시에 수행되지 않는다. import cats.syntax.all._ import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration import scala.concurrent.{Await, Future} object TraverseTest { def main(args: Array[String]): Unit = { def f(n: Int): Future[Unit] = Future { Thread.sleep(1000L) prin..
-
Iorscala/cats2 2023. 1. 29. 16:55
문제 Join two lists of different sizes 코드 import cats.data._ import cats.syntax.all._ object IorExample { def main(args: Array[String]): Unit = { val list1: List[String] = List("x", "y") val list2: List[Int] = List(1, 2, 3, 4) val zip = list1.alignWith(list2) { case Ior.Both(a, b) => (a, b) case Ior.Left(a) => (a, 0) case Ior.Right(b) => ("z", b) } println(zip) // List((x,1), (y,2), (z,3), (z,4)) ..
-
Cats2 Exercisescala/cats2 2023. 1. 22. 20:35
https://www.geekabyte.io/2018/09/easing-into-cats-and-case-for-category.html 에 나오는 문제의 답안이 scalafiddle 사이트로 연결되지 않아 볼 수가 없다. cats 로 내가 생각한 대로 구현해 본다. import cats._ import cats.data._ import cats.syntax.all._ import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.duration.Duration import scala.concurrent.{Await, ExecutionContext, Future} import scala.util.{Failure, Succ..
-
Validatedscala/cats2 2023. 1. 19. 21:36
Validated 예제 import cats.data._ import cats.syntax.all._ object ValidatedTest { def main(args: Array[String]): Unit = { val x: ValidatedNel[String, Int] = 1.validNel[String] // 1.valid[NonEmptyList[String]] val y: ValidatedNel[String, Int] = 2.validNel[String] // 2.valid[NonEmptyList[String]] val e1: ValidatedNel[String, Int] = "error1".invalidNel[Int] // NonEmptyList.of("error1").invalid[Int] v..
-
Writerscala/cats2 2023. 1. 19. 20:52
Writer Monad 예제 import cats.data._ import cats.syntax.all._ object WriterTest { def main(args: Array[String]): Unit = { val x: Writer[List[String], Int] = 1.writer(List("one")) val y: Writer[List[String], Int] = 23.writer(List("two", "three")) val z: Writer[List[String], Int] = for { a
-
OptionTscala/cats2 2023. 1. 16. 10:34
cats OptionT example import cats.data._ import cats.syntax.all._ import scala.util.{Success, Try} object OptionTExample { def main(args: Array[String]): Unit = { val a: Try[Option[Int]] = Success(1.some) val b: Try[Int] = Success(2) val c: Option[Int] = 3.some val d: OptionT[Try, Int] = OptionT.some(4) // OptionT[Try].some(4), OptionT.some is an alias for OptionT.pure val e: OptionT[Try, Int] = ..
-
Nestedscala/cats2 2023. 1. 16. 10:07
cats Nested 예제 import cats.data._ import cats.syntax.all._ import scala.util.{Success, Try} object NestedExample { def main(args: Array[String]): Unit = { val someValue: Try[Option[Int]] = Success(2.some) val basicScala: Try[Option[Int]] = someValue.map(_.map(_ * 3)) val catsScala: Try[Option[Int]] = Nested(someValue).map(_ * 3).value } }