scala-cats

False errors when using cats library in IntelliJ

微笑、不失礼 提交于 2019-12-09 08:46:19
问题 I am using the cats Scala library and the IntelliJ IDE seems to be struggling with the use of implicits: Here is a simple example: import cats.std.all._ import cats.Traverse.ops._ def useSequence[A](ls : List[Option[A]]) : Option[List[A]] = { ls.sequence } In IntelliJ, this code is highlighted red. But I can build just fine using Make Project or the command line. Right now the error is: Expression of type Nothing[List[Nothing]] doesn't conform to expected type Option[List[A]] Other times the

Scala cats, traverse Seq

我与影子孤独终老i 提交于 2019-12-08 19:10:40
问题 I know I can traverse List s import cats.instances.list._ import cats.syntax.traverse._ def doMagic(item: A): M[B] = ??? val list: List[A] = ??? val result: M[List[B]] = list.traverse(doMagic) And I can convert a Seq back and forth to List val seq: Seq[A] = ??? val result: M[Seq[B]] = seq.toList.traverse(doMagic).map(_.toSeq) But can I also traverse Seq without the boilerplate? val seq: Seq[A] = ??? val result: M[Seq[B]] = seq.traverse(doMagic) Or what's an easy way to get an instance of

Replace elements in List by condition

假装没事ソ 提交于 2019-12-08 12:29:02
问题 I have a pretty much large val s: List[Int] = //... , a function f: Int => Boolean and a function transform: Int => Int . The problem : I want to create another List[Int] such that all elements e: Int of the s: List[Int] such that f(e) = true are replaced with transform(e) . I looked at cats-mtl FunctorEmpty (to adhere to functional programming style), but it does not seem to work in my case. Maybe some cats / scalaz data structures can be useful here? Or any other way? 回答1: s.map{ e => if(f

How to call a function which uses MTL and Parallel?

早过忘川 提交于 2019-12-08 07:51:18
问题 I am using Parallel type class in order to collect all validation errors: def getNonEmptyStr[M[_]](key: String)( implicit E: MonadError[M, Errors], A: ApplicativeAsk[M, Params], W: FunctorTell[M, List[String]] ): M[String] = ??? def getInt[M[_]](key: String)( implicit E: MonadError[M, Errors], A: ApplicativeAsk[M, Params], W: FunctorTell[M, List[String]] ): M[Int] = ??? def getUser[M[_], F[_]]( implicit E: MonadError[M, Errors], R: ApplicativeAsk[M, Params], W: FunctorTell[M, List[String]], P

Scala and cats: Implicit conversion to identity monad

ぐ巨炮叔叔 提交于 2019-12-07 08:10:22
问题 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[

Reduce / fold over list of monoid but reducer returns Either

☆樱花仙子☆ 提交于 2019-12-07 06:49:29
问题 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

What is the ? type?

给你一囗甜甜゛ 提交于 2019-12-07 03:03:56
问题 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

Understanding of Parallel typeclass from Cats

跟風遠走 提交于 2019-12-06 11:32:57
There is a typeclass called Parallel in Cats . The purpose of this class is to provide parallel computations for some monads that don't support parallel computations out of the box like Either for example. I know that Monad is used for dependent computations and thus requires sequential execution. Applicative is used for independent computations, so such computations can be parallelized. It's also known that each Monad is Applicative (monad is applicative functor). So now I can't put together all concepts from the theory. If all monads are also applicatives, why I can't use applicative nature

How to transform disjunction of Future to Future of disjunction

╄→尐↘猪︶ㄣ 提交于 2019-12-06 06:30:29
问题 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 回答1: Just as

How to implement Functor[Dataset]

被刻印的时光 ゝ 提交于 2019-12-06 06:03:23
问题 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