scala-cats

Kittens - ambiguous imports (again)

柔情痞子 提交于 2019-12-25 00:52:33
问题 This answer https://stackoverflow.com/a/56366311/2682459 shows how it is possible to use an object to provide a custom implementation of a typeclass when using Kitten. Applying the same principle to the following code though doesn't work: package com.xxx.yyy.zzz import cats._, cats.derived._, cats.implicits._ object Test extends App { case class Inner(double: Double) case class Outer(inner: Inner, s: String) implicit object doubleEq extends Eq[Double] { override def eqv(x: Double, y: Double):

Traversing Either in Scala

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 19:46:33
问题 I wrote the following simple code: import cats.effect.IO import cats.instances.either._ import cats.syntax.TraverseSyntax object Test extends App with TraverseSyntax{ val e: Either[String, IO[Int]] = Right(IO(2)) e.sequence //error here } Unfortunately it refuses to compile with the Error:(25, 94) value sequence is not a member of scala.util.Either Can you please explain why? I imported either instances which include Traverse[Either[A, ?]] . What's wrong? 回答1: Traverse[F] is defined as a

Defining a Semigroup instance that depends on itself

試著忘記壹切 提交于 2019-12-24 07:59:40
问题 ... or mishaps of a Haskell programmer that has to code Scala, part 5. I have the following structure in Scala: case class ResourceTree( resources: Map[String, ResourceTree] ) And, using Cats, I would like to define a Semigroup instance of it. object ResourceTreeInstances { implicit val semigroupInstance = new Semigroup[ResourceTree] { override def combine(x: ResourceTree, y: ResourceTree): ResourceTree = { ResourceTree( x.resources |+| y.resources ) } } This will result in the following

How to export Scala Transformation to Java

 ̄綄美尐妖づ 提交于 2019-12-24 00:56:47
问题 I'm trying to implement a Scala trait by Java, the trait has a generic container type within another container, which couldn't be solved automatically by java import, the Scala code is below: import cats.data.{EitherNel, Kleisli, NonEmptyList} import cats.implicits._ package Export_To_Java { package object types { type Valid[A] = EitherNel[String, A] type ValidOperation[A, B] = Kleisli[Valid, A, B] type Amount = BigDecimal } trait InterestService[Account] { import types._ type

Tail recursive algorithm for generating all topological orderings in a graph

有些话、适合烂在心里 提交于 2019-12-23 15:14:23
问题 Given a graph i need to generate all topological orderings. For instance, given the following graph: i want to generate all topological orderings, which are: 2 4 7 5 2 7 4 5 2 4 5 7 Because many topological orderings may exist, I need to generate them lazily. Currently, I have a working implementation that is recursive and works on top of the scala-graph library: import scalax.collection.Graph import scalax.collection.GraphPredef._ import scalax.collection.GraphEdge._ import scala.collection

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

孤街醉人 提交于 2019-12-22 04:01:23
问题 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

Stop for-comprehension mid-flow when using stacked monads of State and IO

狂风中的少年 提交于 2019-12-21 05:46:13
问题 In this Scala example I need to stop when the result is StopNow , I need to do this after calling decisionStep . How can I do that? case class BusinessState() trait BusinessResult case object KeepGoing extends BusinessResult case object StopNow extends BusinessResult type IOState[S, A] = StateT[IO, S, A] type BusinessIOState[A] = IOState[BusinessState, A] trait SomeSteps { def step1:BusinessIOState[Unit] def step2:BusinessIOState[BusinessState] def decisionStep:BusinessIOState[BusinessResult]

Scala - How to Combine EitherT with Either in For Comprehension

孤街醉人 提交于 2019-12-19 11:52:11
问题 Suppose I have the following setting: def foo: Either[Error, A] = ??? def bar: EitherT[Future, Error, B] = ??? case class Baz(a: A, b: B) How can I use for comprehension to instantiate the class Baz ? I tried with: val res = for { a <- foo b <- bar } yield Baz(a, b) but, the result has type Either[Error, Nothing] . I don't know what is the right return type in this case, but obviously I don't want Nothing ... What is the right way to combine Either and EitherT in for comprehension? 回答1: Use

How to inject dependencies through Scala Reader from Java code

北城以北 提交于 2019-12-12 16:16:56
问题 Here is a dependency service: public class Service1 {} Scala code that uses it via reader: object TupleEx { type FailFast[A] = Either[List[String], A] type Env[A] = ReaderT[FailFast, Service1, A] import cats.syntax.applicative._ import cats.instances.either._ def f:Env[Int] = 10.pure[Env] } Java test where I try to inject Service1: @Test public void testf() { Service1 s = new Service1(); TupleEx.f().run(s); } I am getting an exception: Error:(10, 16) java: method run in class cats.data

How to use Scala Cats Validated the correct way?

╄→гoц情女王★ 提交于 2019-12-12 08:25:01
问题 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] = {