Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Georgi Krastev
    @joroKr21
    @rzeigler this works:
      import cats.Show
      import cats.instances.all._
      import shapeless.Typeable
    
      case class Foo[I[_]](a: String, i: I[String])
      object Foo {
        implicit def show[I[_]](implicit show: Show[I[String]], typ: Typeable[I[String]]): Show[Foo[I]] =
          semiauto.show[Foo[I]]
      }
    Ryan Zeigler
    @rzeigler
    nifty
    thanks
    Lena Brüder
    @lenalebt
    Hey together, while looking for replacements for a lib that does "equals ignoring some fields" I was looking into cats Eq, which pointed to kittens. Is it easily doable to derive an Eq instance that takes care of all but one field? In my specific use-case, it's a very large case class with about 25 fields, and I'd like to avoid the error-prone boilerplate... I need to compare all but the database ID :D
    Miles Sabin
    @milessabin
    It's doable, but if you can I'd recommend wrapping your db id in a newtype with the desired (universal) equality semantics.
    Lena Brüder
    @lenalebt
    Oh, thank you - I think I can work something out that applies for the case at hand. Thank you!
    Paul Snively
    @paul-snively
    So this seems like something Kittens can likely help with.
    Let's say I have an HList of values of some typeclass that forms a Monad, so it has some means of combining or sequencing. In my concrete case, it's Atto Parsers.
    Atto Parsers can be sequenced with ~, for example, to yield a Parser that yields a pair of the types of the combined Parsers.
    Is it possible to type-safely, say, "reduce" an HList of Parser[T] to get a single Parser with (an unfortunately horribly nested) Tuple?
    Georgi Krastev
    @joroKr21
    You can traverse with a Poly or sequence
    That's in the cats.sequence package
    But you will not get a tuple
    You will get Parser[HList]
    You can convert it to a tuple easily though
      test("sequencing Either")(check {
        forAll { (x: Either[String, Int], y: Either[String, String], z: Either[String, Float]) =>
          val expected = (x, y, z) mapN (_ :: _ :: _ :: HNil)
          (x :: y :: z :: HNil).sequence == expected
        }
      })
    Paul Snively
    @paul-snively
    Right. I need to combine elements with ~, and the result should be a Parser[T], where T is some nested Tuple because ~ gives you a Parser of a Pair.
    So, reduce.
    Georgi Krastev
    @joroKr21
    x ~ y should be just (x, y).tupled or am I missing something?
    Paul Snively
    @paul-snively
    @joroKr21: I think it's an alias for flatMap. It sequences two Atto Parsers.
    @joroKr21: Parser[A] ~ Parser[B] => Parser[(A, B)].
    Georgi Krastev
    @joroKr21
    That doesn't look like flatMap
    Anyway you can use hlistOfParsers.sequence.map(_.tupled)
    Provided there is Applicative[Parser]
    Paul Snively
    @paul-snively
      implicit class Command[P <: HList : IsHCons : *->*[Parser]#λ](parsers: P) {
        def command[InF, InL <: HList, R, O <: Product, OL <: HList](f: InF)
          (implicit
            fntop: FnToProduct.Aux[InF, InL => R],
            pgood: Comapped.Aux[P, Parser, InL]
          ): String => Either[Throwable, R] = { s =>
            val tough  = parsers.sequence
            val parser = tough.map(flatten).map(_.productElements)
            val args   = parser.parseOnly(s).either.leftMap(s => new RuntimeException(s))
            args.map(a => fntop(f)(a))
          }
      }
    This complains that it can't construct a Sequencer.
    "!I seq (cannot construct sequencer, make sure that every item of your hlist P is an Apply): Sequencer[P]"
    Georgi Krastev
    @joroKr21
    So do you have an Apply[Parser] in scope?
    Paul Snively
    @paul-snively
    Well, every element is a Parser; every Parser is an Applicative, and every Applicative is an Apply.
    Georgi Krastev
    @joroKr21
    But perhaps the issue is in the generics here
    Paul Snively
    @paul-snively
    Hmm. Maybe I need another import?
    Georgi Krastev
    @joroKr21
    I've never tried to use sequence in a generic context
    Paul Snively
    @paul-snively
    Right. I'm trying to 1) ensure my HList is a stable value, 2) give whatever evidence I need.
    Georgi Krastev
    @joroKr21
    I guess you just need an implicit Sequencer[P] { type F[x] = Parser[x] }
    Ah wait
    Sequencer.Aux[P, Parser, InL]
    Paul Snively
    @paul-snively
    That goes right back to not finding a Sequencer...
    Georgi Krastev
    @joroKr21
    Can you put it in scastie?
    Paul Snively
    @paul-snively
    Oh wait... I added it to the implicit stanza and I think it worked!
    Georgi Krastev
    @joroKr21
    Yes, that's what I meant you need it as implicit
    Paul Snively
    @paul-snively
    Right, of course. Thanks!
    Georgi Krastev
    @joroKr21
    :thumbsup:
    Simão Martins
    @Lasering

    Hi!
    In the following code:

    import cats.Show
    import cats.derived
    import com.comcast.ip4s.{Cidr, IpAddress}
    
    case class Route[+IP <: IpAddress](destination: Cidr[IP], nexthop: IP)
    object Route {
      implicit def show[IP <: IpAddress]: Show[Route[IP]] = {
        implicit val ipShow: Show[IP] = IpAddress.show
        implicit val cidrShow: Show[Cidr[IP]] = Cidr.show
        derived.semiauto.show
      }
    }

    I'm getting the following error:

    [error] /home/simon/DSI/openstack/scala-neutron-client/src/main/scala/pt/tecnico/dsi/openstack/neutron/models/Route.scala:30:22: Could not derive an instance of Show[A] where A = pt.tecnico.dsi.openstack.neutron.models.Route[IP].
    [error] Make sure that A has a Typeable instance and satisfies one of the following conditions:
    [error]   * it is a case class where all fields have a Show instance
    [error]   * it is a sealed trait where all subclasses have a Show instance
    [error]     derived.semiauto.show
    [error]                      ^

    Which makes no sense, is this a bug, or am I missing something?

    Paul Snively
    @paul-snively
    I wonder whether not using the IP type variable would help?
    Simão Martins
    @Lasering
    typelevel/kittens#267 seems the problem is caused by the lack of an instance of Typeable[Cidr[IP]], I'm trying to implement one
    Paul Snively
    @paul-snively
    :+1:
    Georgi Krastev
    @joroKr21
    Kittens 2.2.2 with Scala Native support is out: https://github.com/typelevel/kittens/releases/tag/v2.2.2 :tada:
    Miles Sabin
    @milessabin
    :tada:
    Georgi Krastev
    @joroKr21