scala-cats

Scala Cats Effects - IO Async Shift - How Does it Work?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 12:42:35
Here is some Scala cats code using the IO Monad : import java.util.concurrent.{ExecutorService, Executors} import cats.effect.IO import scala.concurrent.{ExecutionContext, ExecutionContextExecutor} import scala.util.control.NonFatal object Program extends App { type CallbackType = (Either[Throwable, Unit]) => Unit // IO.async[Unit] is like a Future that returns Unit on completion. // Unlike a regular Future, it doesn't start to run until unsafeRunSync is called. def forkAsync(toRun: () => Unit)(executor: ExecutorService): IO[Unit] = IO.async[Unit] { callback: CallbackType => // "callback" is a

Doobie and DB access composition within 1 transaction

怎甘沉沦 提交于 2019-12-03 08:26:42
问题 Doobie book says that it's a good practice to return ConnectionIO from your repository layer. It gives an ability to chain calls and perform them in one transaction. Nice and clear. Now let's imagine we are working on REST API service and our scenario is: Find an object in database Perform some async manipulation (using cats.effect.IO or monix.eval.Task) with this object. Store the object in database. And we want to perform all these steps inside 1 transaction. The problem is that without

Cats-effect and asynchronous IO specifics

房东的猫 提交于 2019-12-03 03:56:47
问题 For few days I have been wrapping my head around cats-effect and IO. And I feel I have some misconceptions about this effect or simply I missed its point. First of all - if IO can replace Scala's Future, how can we create an async IO task? Using IO.shift ? Using IO.async ? Is IO.delay sync or async? Can we make a generic async task with code like this Async[F].delay(...) ? Or async happens when we call IO with unsafeToAsync or unsafeToFuture ? What's the point of Async and Concurrent in cats

Doobie and DB access composition within 1 transaction

耗尽温柔 提交于 2019-12-02 23:51:16
Doobie book says that it's a good practice to return ConnectionIO from your repository layer. It gives an ability to chain calls and perform them in one transaction. Nice and clear. Now let's imagine we are working on REST API service and our scenario is: Find an object in database Perform some async manipulation (using cats.effect.IO or monix.eval.Task) with this object. Store the object in database. And we want to perform all these steps inside 1 transaction. The problem is that without natural transformation which is given for us by transactor.trans() we are working inside 2 monads - Task

Simple way to apply a list of functions to a value

三世轮回 提交于 2019-12-01 09:40:00
Suppose I've got a list of functions List[A => B] and need a function that returns List[B] for a given value of type A : def foo[A, B](fs: List[A => B]): A => List[B] = a => fs.map(_.apply(a)) Is there any simpler (maybe with cats ) way to write List[A => B] => A => List[B] ? As @Oleg points out, you can use Applicative to generate the function: import cats.implicits._ def foo[A, B](fs: List[A => B]): A => List[B] = a => fs ap List(a) Although I don't think it makes much of a difference in this particular case. 来源: https://stackoverflow.com/questions/45160406/simple-way-to-apply-a-list-of

Bounds for type parameter of FunctionK

孤街醉人 提交于 2019-12-01 07:44:05
I'm using cats FreeMonad . Here's a simplified version of the algebra: sealed trait Op[A] object Op { final case class Get[T](name: String) extends Op[T] type OpF[A] = Free[Op, A] def get[T](name: String): OpF[T] = liftF[Op, T](Get[T](name)) } One of the interpreters will be a wrapper around a third-party library, called Client here which its get method's signature is similar to: class Client { def get[O <: Resource](name: String) (implicit f: Format[O], d: Definition[O]): Future[O] = ??? } My question is how can I encode that requirement in my implementation? class FutureOp extends (Op ~>

Simple way to apply a list of functions to a value

寵の児 提交于 2019-12-01 07:34:26
问题 Suppose I've got a list of functions List[A => B] and need a function that returns List[B] for a given value of type A : def foo[A, B](fs: List[A => B]): A => List[B] = a => fs.map(_.apply(a)) Is there any simpler (maybe with cats ) way to write List[A => B] => A => List[B] ? 回答1: As @Oleg points out, you can use Applicative to generate the function: import cats.implicits._ def foo[A, B](fs: List[A => B]): A => List[B] = a => fs ap List(a) Although I don't think it makes much of a difference

Calling generic function with Functor using subclass (cats/scalaz)

跟風遠走 提交于 2019-12-01 06:55:57
I've been messing around with some basic examples of Cats/Scalaz and also walking through tutorials to get a feel and I've hit a case which I'm sure there is a solution to. Is it possible to call a generalized function that takes a contextualized value ( F[A] ) with a Functor view ( F[_] : Functor ) with a context that is <: F ? I'm aware that Functor is invariant on type F[_] , and I'm also aware of the existence of Functor.widen , but it seems strange that there is no way to implicitly widen my type for use in a general function. An example in Cats (a similar example with Scalaz exists as

Calling generic function with Functor using subclass (cats/scalaz)

北战南征 提交于 2019-12-01 05:24:42
问题 I've been messing around with some basic examples of Cats/Scalaz and also walking through tutorials to get a feel and I've hit a case which I'm sure there is a solution to. Is it possible to call a generalized function that takes a contextualized value ( F[A] ) with a Functor view ( F[_] : Functor ) with a context that is <: F ? I'm aware that Functor is invariant on type F[_] , and I'm also aware of the existence of Functor.widen , but it seems strange that there is no way to implicitly

Convert List[Either[A, B]] to Either[List[A], List[B]]

≡放荡痞女 提交于 2019-12-01 01:13:52
问题 How to convert List[Either[String, Int]] to Either[List[String], List[Int]] using a method similar to cats sequence? For example, xs.sequence in the following code import cats.implicits._ val xs: List[Either[String, Int]] = List(Left("error1"), Left("error2")) xs.sequence returns Left(error1) instead of required Left(List(error1, error2)) . KevinWrights' answer suggests val lefts = xs collect {case Left(x) => x } def rights = xs collect {case Right(x) => x} if(lefts.isEmpty) Right(rights)