Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • May 09 21:03
    joroKr21 synchronize #1240
  • Apr 07 16:55

    joroKr21 on main

    Update sbt-mima-plugin to 1.1.0 Merge pull request #1258 from s… (compare)

  • Apr 07 16:55
    joroKr21 closed #1258
  • Apr 07 16:47
    scala-steward opened #1258
  • Apr 05 06:02

    joroKr21 on main

    Update sbt-scalajs, scalajs-com… Merge pull request #1257 from s… (compare)

  • Apr 05 06:02
    joroKr21 closed #1257
  • Apr 05 02:57
    scala-steward opened #1257
  • Mar 27 15:42

    joroKr21 on v2.3.9

    (compare)

  • Mar 26 20:08
    joroKr21 closed #1254
  • Mar 26 20:08
    joroKr21 commented #1254
  • Mar 26 20:05

    joroKr21 on 2.3

    When case class has custom unap… Merge pull request #1256 from j… (compare)

  • Mar 26 20:05
    joroKr21 closed #1256
  • Mar 26 19:29
    joroKr21 synchronize #1256
  • Mar 26 18:51
    joroKr21 labeled #1256
  • Mar 26 18:51
    joroKr21 labeled #1256
  • Mar 26 18:51
    joroKr21 assigned #1256
  • Mar 26 18:51
    joroKr21 opened #1256
  • Mar 26 17:45

    joroKr21 on main

    When case class has custom unap… Merge pull request #1255 from j… (compare)

  • Mar 26 17:45
    joroKr21 closed #1255
  • Mar 26 17:45
    joroKr21 closed #1059
Fabio Labella
@SystemFw
:+1:
Ben Kyrlach
@bkyrlach
I still have much to learn. Shapeless feels a little like magic.
Thanks for the help!
Fabio Labella
@SystemFw
try writing your own selector
if you have no idea how to start that, I'd recommend to read (or reread) the type astronaut guide to Shapeless
Ben Kyrlach
@bkyrlach
I found that book last night, but I don't think my brain was functioning all that well at 12:30am. I'll try again when the coffee kicks in.
:D
etienne
@crakjie
@bkyrlach If you fin a solution I would be interested to see the implementation
Ben Kyrlach
@bkyrlach
@crakjie So I cheated a little bit. In my case, I'm not passing the list in as a parameter. If that works in your case, here's the code I ended up with.
import shapeless.ops.hlist.Selector
import shapeless.{::, HNil}

object Program {

  case class Cat(name: String)
  case class Dog(name: String)
  case class Fish(name: String)

  val animals = Cat("silver") :: Dog("daisy") :: Fish("nemo") :: HNil

  def main(args: Array[String]): Unit = {
    animalHandling(catNoise)
  }

  def catNoise(c: Cat): Unit = println("meow")

  def animalHandling[A,B](f: A => B)(implicit ev: Selector[Cat :: Dog :: Fish :: HNil,A]): B = f(animals.select[A])
}
Actually, I was able to make it more generic.
import shapeless.ops.hlist.Selector
import shapeless.{::, HList, HNil}

object Program {

  case class Cat(name: String)
  case class Dog(name: String)
  case class Fish(name: String)

  val animals = Cat("silver") :: Dog("daisy") :: Fish("nemo") :: HNil

  def main(args: Array[String]): Unit = {
    animalHandling(catNoise)(animals)
  }

  def catNoise(c: Cat): Unit = println("meow")

  def animalHandling[A,B, H <: HList](f: A => B)(xs: H)(implicit ev: Selector[H,A]): B = f(xs.select[A])
}
Fabio Labella
@SystemFw
^^ that's it
etienne
@crakjie
Ok, I was trying (without sucess ) with CollectFirst
Ben Kyrlach
@bkyrlach
It works magically.
Or at least mathemagically. :D
Ethan
@esuntag
Hey, I'm trying to verify that for a given Coproduct and a given Seq, the Seq contains at least one element of every member in the Coproduct. Currently, it's using union.Keys and ops.hlist.ToTraversable to string compare the symbols to the class SimpleNames. It feels like there should be a better way of doing this. Is there a more correct/canonical approach to solving that problem?
def check[A, C <: Coproduct, K <: HList](data: Seq[A])(
    implicit
    gen: LabelledGeneric.Aux[A,C]
    keys: ops.union.Keys.Aux[C,K]
    toList: ops.hlist.ToTraversable.Aux[K,List,Symbol]
): List[(String,Boolean)] = {
    val classes: List[Symbol] = toList(keys())
    classes.map(sym => (sym.name, data.exists(_.getClass.getSimpleName == sym.name)))
}
Ethan
@esuntag
It does what I need it to, but feels dirty
rquinlivan
@rquinlivan
Hey guys, I'm trying to use shapeless's HMap class, and I'm running into trouble. The docs say that it supports "an arbitrary relation between the key type and the corresponding value type", but it looks like you cannot reuse the key type. I.e., you cannot define both a Int -> Double and Int -> String. Is this correct? Or am I using the library incorrectly?
Fabio Labella
@SystemFw
@rquinlivan yeah the point of HMap is to encode a unique relationship
if you have both Rel[Int, Double] and Rel[Int, String], and you do get(1), how will the typesystem know whether the value is of type Double or String?
you might also want to look at the vault library for a similar abstraction with different constraints
in that case the keys are represented by a value of type Key[A], and then you can get the corresponding value
it's not quite a Map though, more like a store
Cary Robbins
@carymrobbins
Maybe you could newtype the Int?
and just not use Int in the HMap
although it does seem like you might be able to do this if you specify the return type as well (not sure if that would work with HMap, but some sort of Poly wrapper around a Map should do it)
Ben Kyrlach
@bkyrlach
Say I have two HLists, H and I.
I can use implicit ev: SelectAll[H,I] to ensure all members of I are in H.
And I can use implicit ev: IsHCons[I] to ensure that I is not empty.
But if I combine the two like so...
def foo[H <: Hlist, I <: HList](xs: H, ys: I)(implicit ev: SelectAll[H,I], ev2: IsHCons[I]): Unit = {
  println(xs.select[ev2.H])
}
It says that there's no proof that ev2.H is in H.
Gunnar Lilleaasen
@heksesang
@bkyrlach Because ev2 contains proof that I has a head, but not that H has a head, I think.
Gunnar Lilleaasen
@heksesang
And you try to do a select without having a selector.
SelectAll does not guarantee order, so SelectAll[H, I] does not provide proof that IsHCons[I] is equal to IsHCons[H].
Gunnar Lilleaasen
@heksesang
So you cannot do ev2.head(xs) because while they have the same types, it’s not given that the head of H is the same type as head of I. It could be a type not existing in I, or one of the types in the tail of I. And if you wanna do xs.select, you need to provide a Selector instance for it.
Ben Kyrlach
@bkyrlach
I need to digest that a little.
But it looks way better than what I finally arrived at last night.
rquinlivan
@rquinlivan
To me the point of HMap would be to be able to get("key") and get back a strongly typed value instead of Any. If you cannot have polymorphic key mapping then HMap is useless.
Fabio Labella
@SystemFw
but that's what HMap does
or rather, there are two different approaches to this problem: HMap and Vault
rquinlivan
@rquinlivan
No, it gives you a mapping from A -> B and C -> D. Not A ->B and A -> C
Fabio Labella
@SystemFw
HMap is type driven
if you have A -> B and A -> C then by definition it's not a mapping
what does A ->map to, at the type level?
B or C?
rquinlivan
@rquinlivan
As far as practical usefulness, the way HMap is implemented is not useful.
Fabio Labella
@SystemFw
that's untrue, it's just not a good fit (maybe) for your current use case