Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Marko Dimjašević
@mdimjasevic
@LukaJCB: do you use the library?
Luka Jacobowitz
@LukaJCB
@felixmulder yes!
@mdimjasevic yes as well, it's a great library!
Marko Dimjašević
@mdimjasevic
I'm looking at the library. I wonder what happens with values that are known only at runtime.
Luka Jacobowitz
@LukaJCB
You'll get an Either
Marko Dimjašević
@mdimjasevic
That sounds like what I was looking for! Probably that's the return value of smart constructors or such
Luka Jacobowitz
@LukaJCB
Yep!
Felix Mulder
@felixmulder
@LukaJCB - hmm, don't I need to lift into Arrow for that?
Luka Jacobowitz
@LukaJCB
Arrow is a typeclass that Kleisli has an instance for ☺️
Felix Mulder
@felixmulder
yeah
but
import cats.implicits._, cats.syntax.all._, cats.data.Kleisli._
val k1: Kleisli[Option, Int, Int] = Kleisli(x => Option(x))
val k2 = k1

k1 >>> k2 // no can compiley yoooooooooooooooooo
I'm not sure where the operator comes from in this case
Luka Jacobowitz
@LukaJCB
Hmm
Partial unification is on? :smile:
Fabio Labella
@SystemFw
@felixmulder don't import both implicits._ and syntax.all._
you'll import the same implicit twice
so the compiler finds two
and very helpfully tells you "cannot find implicit"
Felix Mulder
@felixmulder
yeah, that was my last ditch attempt to get it to work ^^
Fabio Labella
@SystemFw
partial unification on yeah?
Felix Mulder
@felixmulder
it's on, maybe my sbt-config is borked
@SystemFw , @LukaJCB - yeup worked fine in the project, but not in the sbt-console :sweat_smile: :gun:
harrydevnull
@harrydevnull

i have a naive question

case class TeacherId(id:Int)
case class StudentId(id:Int)
case class EClasse(teacherId: TeacherId,students:List[StudentId]=List())

val o: Seq[EClasse] = List(
  EClasse(TeacherId(1),List(StudentId(1))),
  EClasse(TeacherId(1),List(StudentId(2))),
  EClasse(TeacherId(1),List(StudentId(3))),
  EClasse(TeacherId(2),List(StudentId(8))),
  EClasse(TeacherId(2),List(StudentId(3))),
  EClasse(TeacherId(2),List(StudentId(5)))
)

how can i combine all of them into

List(
  EClasse(TeacherId(1),List(StudentId(1),StudentId(2),StudentId(3))),
  EClasse(TeacherId(2),List(StudentId(5),StudentId(3),StudentId(8)))
)

using cats?

Luka Jacobowitz
@LukaJCB
You could define a Semigroup for Eclasse and use a fold
Or reduce
felher
@felher
How would you combine EClasses with different TeacherIds?
harrydevnull
@harrydevnull
@felher that's where I got confused
Fabio Labella
@SystemFw
@harrydevnull if you're ok going through a Map, that'll do it
  val res =
    o.foldMap(ec => Map(ec.teacherId -> ec.students)).map { case (k, v) => EClasse(k, v) }.toList
make sure your o is typed as List[EClasse], and not Seq
the trick here is to find the right monoid
I can explain what's going on more verbosely, if necessary
harrydevnull
@harrydevnull
foldMap doesn't exist ?
for List?
Fabio Labella
@SystemFw
???
what do you mean?
harrydevnull
@harrydevnull
ah I had to import
import cats.implicits.
import cats.instances.list.
sorry
Fabio Labella
@SystemFw
no
just import cats.implicits._
harrydevnull
@harrydevnull
thanks ! can you please explain what's going on here?
Fabio Labella
@SystemFw
sure
are you familiar with Monoids at all? (no problem if you're not)
harrydevnull
@harrydevnull
a little yes
Fabio Labella
@SystemFw
and have you seen foldMap before?
harrydevnull
@harrydevnull
nope
Fabio Labella
@SystemFw
right
so, Monoids give you a way of combining two As into a single A, and an identity element of type A