scala-cats

Scala and cats: Implicit conversion to identity monad

房东的猫 提交于 2019-12-05 19:04:50
I have a function that computes sum of squares for numeric types as shown below. import cats.syntax.functor._ import cats.syntax.applicative._ import cats.{Id, Monad} import scala.language.higherKinds object PowerOfMonads { /** * Ultimate sum of squares method * * @param x First value in context * @param y Second value in context * @tparam F Monadic context * @tparam T Type parameter in the Monad * @return Sum of squares of first and second values in the Monadic context */ def sumOfSquares[F[_]: Monad, A, T >: A](x: F[A], y: F[A])(implicit num: Numeric[T]) : F[T] = { def square(value:T): T =

Reduce / fold over list of monoid but reducer returns Either

淺唱寂寞╮ 提交于 2019-12-05 12:25:49
Ive found myself in the situation a couple of times where i have a reducer / combine fn like so: def combiner(a: String, b: String): Either[String, String] = { (a + b).asRight[String] } Its a dummy implementation but the fn can fail so it returns an either. I then I have a list of values I want to pass through this with reduce / fold. The best I can come up with (assuming the List's type is a monoid) is this: def combine(items: Vector[String]) = { items.foldLeft(Monoid[String].empty.asRight[String]) { case (acc, value) => acc.flatMap( accStr => combiner(accStr, value)) } } Its a bit clumsy and

What is the ? type?

大城市里の小女人 提交于 2019-12-05 06:16:58
I am trying to implement a cats Monad instance for a type that has multiple type parameters. I looked at the cats Either instance to see how it was done there. Part of the Either Monad instance code from cats is copied below: import cats.Monad object EitherMonad { implicit def instance[A]: Monad[Either[A, ?]] = new Monad[Either[A, ?]] { def pure[B](b: B): Either[A, B] = Right(b) def flatMap[B, C](fa: Either[A, B])(f: B => Either[A, C]): Either[A, C] = fa.right.flatMap(f) } } It fails to compile with the error: error: not found: type ? What is the ? type and how can I use it when creating

How to transform Either[Future[A], Future[B]] to Future[Either[A, B]]

ぐ巨炮叔叔 提交于 2019-12-05 03:25:53
I have an instance of Either[Future[A], Future[B]] and I would like to transform it to Future[Either[A, B]] . Since my previous question , cats 0.8.1 has been released, changing the structure of the library and dropping Xor in favor of Either , which is right-biased in 2.12. Thus the method described in the previous accepted answer does not work anymore. I have tried to find the appropriate imports but failed. cats.instances.either._ cats.implicits._ cats.syntax.bitraverse._ looked plausible but unfortunately does not work. Compilation still fails with value bisequence is not a member of

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

二次信任 提交于 2019-12-04 21:30:26
问题 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

How to transform disjunction of Future to Future of disjunction

a 夏天 提交于 2019-12-04 15:07:33
I have the result of a method: val res: Future[Int] Xor Future[String] = getResult(x) and would like to transform it and use it as Future[Int Xor String] I could not extrapolate my use case from the herding cats blog and am not sure whether a monad transformer would be the right tool here, perhaps rather some form of traverse ? Xor from cats stands in for any disjunction. Scalaz \/ or stdlib Either would be fine as well (though I would prefer a biased disjunction). Thank you Just as sequence allows you to turn a F[G[A]] into a G[F[A]] when F has a Traverse instance and G is applicative,

How to implement Functor[Dataset]

痞子三分冷 提交于 2019-12-04 10:45:10
I am struggling on how to create an instance of Functor[Dataset] ... the problem is that when you map from A to B the Encoder[B] must be in the implicit scope but I am not sure how to do it. implicit val datasetFunctor: Functor[Dataset] = new Functor[Dataset] { override def map[A, B](fa: Dataset[A])(f: A => B): Dataset[B] = fa.map(f) } Of course this code is throwing a compilation error since Encoder[B] is not available but I can't add Encoder[B] as an implicit parameter because it would change the map method signature, how can I solve this? You cannot apply f right away, because you are

Scala - how to use foreach loop in for comprehension block?

走远了吗. 提交于 2019-12-04 06:08:41
问题 I have a simple code: override def createContributorsList(url: String, params: String): F[List[Contributor]] = getContributorsFromClient(url, params).fold[List[Contributor]](_ => List(), res => res) override def createReposList(organization: String, params: String): F[List[GitRepository]] = getReposFromClient(organization, params).fold[List[GitRepository]](_ => List(), res => res) This code return list of repositories from github and list of contributions. But now I need to call

How to use Scala Cats Validated the correct way?

给你一囗甜甜゛ 提交于 2019-12-04 01:52:10
Following is my use case I am using Cats for validation of my config. My config file is in json. I deserialize my config file to my case class Config using lift-json and then validate it using Cats. I am using this as a guide. My motive for using Cats is to collect all errors iff present at time of validation. My problem is the examples given in the guide, are of the type case class Person(name: String, age: Int) def validatePerson(name: String, age: Int): ValidationResult[Person] = { (validateName(name),validate(age)).mapN(Person) } But in my case I already deserialized my config into my case

Bounds for type parameter of FunctionK

偶尔善良 提交于 2019-12-04 01:40:28
问题 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