scala
-
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
-
Future[Try[A]] <=> Future[A] 상호간 변환하기scala/basic 2023. 1. 16. 12:58
Future[Try[A]] Future[A] 변환 Future[Try[A]] => Future[A]import scala.concurrent.ExecutionContext.Implicits.globalimport scala.concurrent.Futureimport scala.util.{Success, Try}object FutureTryTest { def main(args: Array[String]): Unit = { val futureTry: Future[Try[Int]] = Future(Success(1)) // https://github.com/monix/monix/blob/series/3.x/monix-execution/shared/src/main/scala/monix/e..
-
Error Modeling (with Exception)scala/basic 2023. 1. 16. 10:55
import play.api.libs.json.{Json, OFormat} sealed class HttpError(val status: Int, val message: String) extends Throwable(message) object HttpError { implicit val jsonFormat: OFormat[HttpError] = Json.format[HttpError] def apply(status: Int, message: String): HttpError = new HttpError(status, message) def unapply(err: Throwable): Option[(Int, String)] = err match { case err: HttpError => Option((..
-
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 } }
-
Statescala/cats2 2023. 1. 10. 09:49
문제 https://wefree.tistory.com/44 의 문제를 cats State Monad 를 이용해 구현해 본다. 코드 import cats._ import cats.data._ import cats.syntax.all._ object GolfState { def swing(n: Int): State[Int, Int] = State(s => (s+n, s+n)) def main(args: Array[String]): Unit = { val states: Seq[State[Int, Int]] = List(swing(20), swing(10), swing(15)) val finalState: State[Int, Int] = states.reduce(_ *> _) val (s,a) = finalSt..