Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Harmeet Singh(Taara)
@harmeetsingh0013
Hey channel, I am new to scala-cats, I have one quick question, why we require Foldable even scala core collections have method fold, foldLeft and foldRight?
Loránd Szakács
@lorandszakacs

folks, if my main "method" is ultimately described as a Task[Unit] it is a sane thing to just mainTask.runSyncUnsafe(Duration.Inf), at the end of the world right? Or is there a better way of running your final app?

Until recently I had cats.effect.IO, but for various reasons wound up changing it to monix Task, and w/ IO I just did the mainIO.unsafeRunSync — worked fine.

Adelbert Chang
@adelbertc
yeah
you generally want to just do it once at the "end of the world"
which is main
Luka Jacobowitz
@LukaJCB
@harmeetsingh0013 can you rephrase your question? I'm not sure I understand (:
Matthias Sperl
@m-sp
@lorandszakacs If you are already using monix you might just use the TaskApp as well?
Loránd Szakács
@lorandszakacs
@m-sp because I didn't notice it before :D perfect, thank you!
Harmeet Singh(Taara)
@harmeetsingh0013
@LukaJCB according to Foldable, it helps us to use list of monoids into single value, but we have also achive this, by scala foldLeft, fold and foldRight functions, why we should use scala-cats Foldable rather than core scala library fold?
Luka Jacobowitz
@LukaJCB
Monoids are a lot more composable than foldLeft for one
Also Foldable allows you to use abstract types instead of specifying a specific Scala collection itself
Harmeet Singh(Taara)
@harmeetsingh0013
@LukaJCB thanks, If possible, can you give any example on this "Monoids are a lot more composable than foldLeft for one"?
Luka Jacobowitz
@LukaJCB
Right sure
So for example a tuple of two monoids is also a monoid
An either of two monoids is also a monoid
A function returning a monoid is also a monoid
An option containing a monoid is also a monoid
Fabio Labella
@SystemFw
@harmeetsingh0013 nice example, groupBy
in stdlib is weird
foldMap(t => Map(t.k -> List(t.v)))
beautiful
imagine t beingUser(k: Age, v: Name)
Luka Jacobowitz
@LukaJCB
Yup!
Harmeet Singh(Taara)
@harmeetsingh0013
Thanks @SystemFw , but foldMap is not used with monoids, if i am not worng?
Fabio Labella
@SystemFw
you are :)
Harmeet Singh(Taara)
@harmeetsingh0013
@LukaJCB If possible, can you please give code snippet?
Fabio Labella
@SystemFw
foldMap[F[_]: Foldable, A, B: Monoid](fa: F[A])(f: A => B): B
Harmeet Singh(Taara)
@harmeetsingh0013
@SystemFw :P
Luka Jacobowitz
@LukaJCB
There are some great examples here https://typelevel.org/cats/typeclasses/monoid.html
Fabio Labella
@SystemFw
@harmeetsingh0013 bigger example
you have this case class Students(name: String, classID: String)
and you have List[Student]
and you want Map[String, (Int, List[Student])]
so map from Class (in a school sense) to the corresponding students. The Int is the number of students in each class
try with stdlib first, and I'll show you the Monoid solution (that uses compositionality)
Harmeet Singh(Taara)
@harmeetsingh0013
Ohhh great @SystemFw , I will try to create using stdlib
Han van Venrooij
@irundaia

I've been doing the scala exercises on cats. I've been surprised by the following example on Semigroups (note that this example has been paraphrased):

import cats.implicits._

val f1 = (x: Int) => x + 1
val f2 = (x: Int) => x * 10

val f3 = f1 |+| f2

println(f3(6)) // prints 67

I would have expected that composing of twoFunction1s would compose those functions if applicable. Were that the case I would have expected the result to be 70.

Where could I find out how the Semigroup instance for Function1 is defined? I tried looking at the cats-core code. But I couldn't find out where the Semigroup/Monoid instance is defined :(

Semigroup-Monoid are in kernel
Han van Venrooij
@irundaia
Thanks, looking at it now, it makes sense. :)
Harmeet Singh(Taara)
@harmeetsingh0013
@SystemFw here is the simple solution
case class Students(name: String, classID: String)

val students = List(Students("james1", "1"), Students("james2", "1"), Students("micky1", "1"), Students("micky2", "2"))

val noOfStudentsEachClassStudents = students.groupBy(student => student.classID).map {
  case (id, stdnts) => (id, (stdnts.size, stdnts))
}
Matthew Pocock
@drdozer
Anybody else ended up making this type?
trait Given[J, T[_]] {
  def apply[I](implicit T: T[I]): J
}
it lets you curry implicits so you can capture the types 'outside' of the left hand side of a method signature
Oleg Pyzhcov
@oleg-py
@harmeetsingh0013
students.foldMap(st => Map(st.classId -> (1, List(st))))
Fabio Labella
@SystemFw
exactly
Oleg Pyzhcov
@oleg-py
@drdozer I cannot yet imagine how it is supposed to be used?
Harmeet Singh(Taara)
@harmeetsingh0013
@harmeetsingh0013
scala students.foldMap(st => Map(st.classId -> (1, List(st)))) amazing
Fabio Labella
@SystemFw
especially interesting because you don't rely on size (imagine a more complicated metric that you need to compute as you go, as opposed to something that already exists on the data structure like size)