## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
@arosien
i may have it wrong
i thought your example had the state as the Int counter
Andrew Valencik
@valencik
Well either way, n <- State.get[Int] is getting the State as an Int right? There's no State.xxx method that would get the current result, right?
@arosien
oh, you had a String as the state, sorry
that is correct. if you want the current state, you use State.get[S] for whatever state type S is
the "result" is whatever you yield
def incrOne(x: Int): State[String, Int] = for { _ <- State.modify(_ + s"incrOne called with $x") } yield x + 1 (which if you unsugar it, becomes what @christopherdavenport wrote ) but you can add additional expressions to the for, to play with State.get and State.set, etc. Andrew Valencik @valencik very cool. thanks a ton! RAJKUMAR NATARAJAN @rajcspsg Hi I need a help I have instance of MonadError[F,A] and f: A -> B how can I get MonadError[F,B]? Rob Norris @tpolecat It's invariant in A so you would need A => B and B => A … try to write it and see, there's nothing in cats to do that as far as I know. Harmeet Singh(Taara) @harmeetsingh0013 @harmeetsingh0013 Hey Team, I am stuck in simple problem using type system for buidling examples, below code give me error class Store[+T <: Burger] { def orderYourFastFood(fastFood: String): T = new Burger } Error Error:(12, 54) type mismatch; found : Burger required: T def orderYourFastFood(fastFood: String): T = new Burger I defined the relation of T and Burger, but still giving this error Luka Jacobowitz @LukaJCB T is a subtype of burger, so a burger is not necessarily a T You should make T a Supertype for that to work Harmeet Singh(Taara) @harmeetsingh0013 Oh.. got it, Covariance position define super type not sub-type, am I right ? trepidacious @trepidacious @harmeetsingh0013 The + for covariance and the <: for sub type are independent, you can have one or the other, and they mean separate things class Store[+T] means "a Store[A] is a Store[B] when A is a B", this is covariance. This is often associated with "getting" or "reading" information - if Store[A] is a "source" of something, you know it always produces instances of A. So if A <: B, all A's are also B's. So it's sensible to say that a Store[A] can safely be treated as a Store[B] - a Store[A] will always produce A's, and so will always produce B's as well, making it a Store[B] trepidacious @trepidacious Contravariance is the other way around, say you have a class that accepts instances of A - in that case if it can accept any A, and B <: A, it can accept any B as well (because every B is an A). So we have Target[A] is a Target[B] if B <: A. Now you have Store[T <: Burger] - that says nothing about the variance (co or contra) of Store at all - it just says that you can only define a Store[T] if T <: Burger, i.e. for some type T that is a Burger. Your original definition class Store[+T <: Burger] means "A store has a type parameter T, which must be some kind of Burger, and a Store[A] can be treated as a Store[B] as long as A is a B". So for example a Store[SuperFancyBurger] is still a Store[Burger], but we do need to know that SuperFancyBurger <: Burger. However you can't make a Store[Pizza] because Pizza is not a type of Burger. Wow those explanations were rambling and terrible, I apologise. Hopefully some of that makes sense. RAJKUMAR NATARAJAN @rajcspsg Hi I'm trying to create Show instance for my custom config I'm trying to use import cats.derived.semi from "org.typelevel" %% "kittens" % "1.2.0" The complete code is  import enumeratum.{Enum, EnumEntry} sealed abstract class AppEnvironment extends EnumEntry object AppEnvironment extends Enum[AppEnvironment] { case object Local extends AppEnvironment case object Testing extends AppEnvironment case object Production extends AppEnvironment override val values: Vector[AppEnvironment] = findValues.toVector } import java.net.InetAddress import scala.concurrent.duration.Duration final case class ApiConfig(host: InetAddress, port: Int, apiKey: String, timeout: Duration) import java.net.InetAddress import cats.Show import cats.derived.semi import ciris.config.loader.AppEnvironment.{Local, Production, Testing} import enumeratum.EnumEntry import eu.timepit.refined.auto._ import eu.timepit.refined.types.string.NonEmptyString import scala.concurrent.duration._ final case class Config(appName: NonEmptyString, environment: AppEnvironment, api: ApiConfig) object Config { implicit val showConfig: Show[Config] = { implicit val showDuration: Show[Duration] = Show.fromToString implicit val showInetAddress: Show[InetAddress] = Show.fromToString implicit def showEnumEntry[E <: EnumEntry]: Show[E] = Show.show(_.entryName) // Show.show[Config](x => s"api =${x.api} appName = ${x.appName} environment${x.environment}")
semi.show
}
}
The compiler error I'm getting is -
        [error] /Users/rajkumar.natarajan/Documents/Coding/kafka_demo/circe-demo/src/main/scala/ciris/config/loader/Config.scala:32:5: ambiguous implicit values:
[error]  both value emptyProductDerivedShow in trait MkShowDerivation of type => cats.derived.MkShow[shapeless.HNil]
[error]  and method emptyCoproductDerivedShow in trait MkShowDerivation of type => cats.derived.MkShow[shapeless.CNil]
[error]  match expected type cats.derived.MkShow[A]
[error]     show
[error]     ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error]
RAJKUMAR NATARAJAN
@rajcspsg
Any idea how can I resolve this error ?
charlesres
@charlesres
Try deleting the implicit for showDuration. I believe an instance was added with this github issue (typelevel/cats#2216), so you might have two instances. I was able to compile the code, so let me know if not.
@rajcspsg
Forgot to mention to add import cats.implicits._
Abhishek Srivastava
Hello, How do I convert a Map[String, ConnectionIO[List[Foo]]] into ConnectionIO[Map[String, List[Foo]]]. Here connection IO is a monad used by doobie.
Rob Norris
@tpolecat
Probably the easiest way is to add alleycats-core and import alleycats.std.map._ and then you can say .sequence
It's not strictly lawful because map traversal isn't congruent with equality and you can observe this with the Traverse instance. But it usually doesn't matter.
Abhishek Srivastava
great @tpolecat let me try this out. didn't know about alleycats :)
Minh Thai
@mt40
hi, anyone know where can I buy a physical copy of the book Scala with Cats?
Rob Norris
@tpolecat
@noelwelsh and/or @davegurnell ^^
• I don't see how F ~> G comes into place in this specific scenario