These are chat archives for typelevel/cats

4th
Dec 2017
Felix Mulder
@felixmulder
Dec 04 2017 10:37
urgh, folks - what's a good goto debugging path to see what's wrong with implicit divergence in free.Inject?
Luka Jacobowitz
@LukaJCB
Dec 04 2017 10:39
That one always sucks
what helps though is keep adding type signatures
Felix Mulder
@felixmulder
Dec 04 2017 10:57
yeah, down the rabbit hole we gooo :D
Felix Mulder
@felixmulder
Dec 04 2017 13:06
So, I fixed the divergence - pulled in iota (long overdue). But now cats can't find the Inject[Op, App] instance :worried:
Felix Mulder
@felixmulder
Dec 04 2017 13:25
I guess I have to inject using iotas CopK.Inject T_T
Luka Jacobowitz
@LukaJCB
Dec 04 2017 13:28
Hope you get it fixed!
Felix Mulder
@felixmulder
Dec 04 2017 13:29
Thanks @LukaJCB - I have some plumbing to do XD
Luka Jacobowitz
@LukaJCB
Dec 04 2017 13:31
Good luck!
Isn’t CopK.Inject equivalent to cats.free.InjectK ?
Andrey Ivanov
@a-nigredo
Dec 04 2017 13:49
Hi guys, Do you know a channel where I can question about iota?
Felix Mulder
@felixmulder
Dec 04 2017 13:52
@LukaJCB - it looks exactly like it :)
but it requires G <: CopK[_, _]
Luka Jacobowitz
@LukaJCB
Dec 04 2017 13:54
Ah I see
Fabio Labella
@SystemFw
Dec 04 2017 14:10
it might be worth considering final tagless as well if you fighting with plumbing of coproducts
Luka Jacobowitz
@LukaJCB
Dec 04 2017 14:38
Tagless Final <3
Leif Battermann
@battermann
Dec 04 2017 14:46
How do I bring a instance of Monoid[Max[Double]] into scope?
scala> import newts.Max; import newts.syntax.all._; import cats._; import cats.data._; import cats.implicits._
import newts.Max
import newts.syntax.all._
import cats._
import cats.data._
import cats.implicits._

scala> 0.5.asMax |+| 0.75.asMax
res4: newts.Max[Double] = Max(0.75)

scala> Monoid[Max[Double]].empty
<console>:38: error: could not find implicit value for parameter ev: cats.kernel.Monoid[newts.Max[Double]]
       Monoid[Max[Double]].empty
             ^
Fabio Labella
@SystemFw
Dec 04 2017 14:54
mm
let me check where that instance is
you don't have any other imports apart from the ones above do you?
Leif Battermann
@battermann
Dec 04 2017 14:55
no
it looks like it needs an implicit value for MinBounded[Double] or sth. but there I lost track
Fabio Labella
@SystemFw
Dec 04 2017 15:00
yeah there's no MinBounded for Double
no sure whether it is an overlook or it was considered too dodgy
Andrey Ivanov
@a-nigredo
Dec 04 2017 15:14

Is it possible to create compound coproduct in iota? e.g.

type Primitive = Cop[String :: Boolean :: TNil]
type Compound = Cop[Operator :: Primitive :: TNIL]

When I do so and try to inject Cop.Inject[Int, Compound] it does not compile

Christian Pérez-Llamas
@chris-zen
Dec 04 2017 17:04
Hi, I am starting a project from scratch with cats 1.0.0-RC1, I saw sequenceU was removed on 0.9. Is there a lib I could get such functionality from ? Thanks
Luka Jacobowitz
@LukaJCB
Dec 04 2017 17:15
Just use sequence
and don’t forget to add -Ypartial-unification
Christian Pérez-Llamas
@chris-zen
Dec 04 2017 17:24
@LukaJCB thanks very much, My project uses Gradle and it is built for different versions of scala (2.10, 2.11, 2.12). I saw there was a plugin for sbt, I’ll have to implement the logic with Gradle myself I guess.
Luka Jacobowitz
@LukaJCB
Dec 04 2017 17:25
Ah, yeah that’s too bad
it will work on newest versions of 2.11 and 2.12
Why gradle though?
Christian Pérez-Llamas
@chris-zen
Dec 04 2017 17:26
it’s not up to me, but company policies :-(
Luka Jacobowitz
@LukaJCB
Dec 04 2017 17:30
And 2.10 too?
andy scott
@andyscott
Dec 04 2017 19:14

@a-nigredo yes, there’s an operator to concatenate TLists, and the coproduct macros already understand how to process these.
Something like:

type Primitive = Cop[String :: Boolean :: TNil]
type Compound = Cop[TList.Op.Concat[Operator :: TNil, Primitive#L]]

should work. Alternatively you can break it out like this:

type PrimitiveL = String :: Boolean :: Tail
type CompoundL = TList.Op.Concat[Operator :: TNil, PrimitiveL]
type Primitive = Cop[PrimitiveL]
type Compound = Cop[CompoundL]

This is what’s done behind the scenes in Freestyle

Here’s an example of roughly what you’re doing, but for CopK instead of Cop: https://github.com/frees-io/iota/blob/master/modules/examples-cats/src/main/scala/example/NestedCoproducts.scala
@felixmulder Can you some code for the problem you’re having with Inject instances?
andy scott
@andyscott
Dec 04 2017 19:30
Also, if Iota isn’t providing instances and you’d like to know why, import iota.debug.options.ShowAborts and you should see reasons for Iota’s failed implicit resolutions in the compiler output.
Andrey Ivanov
@a-nigredo
Dec 04 2017 20:04
@andyscott thanks. Anothe qu
Another question is it possible somehow emulate message about pattern matching exhastivness when somebody try to pattern match injected values?
andy scott
@andyscott
Dec 04 2017 21:00
There’s nothing implemented for that, currently. It might be possible with a wrapping macro call (requring statements like val res = foo match { … } to be written as val res = exhaustive(foo match { … }) for checking to occur) or with a compiler plugin. If you don’t mind, please file an issue on the iota repo!
It might be more reasonable to implement more robust fold methods. I’ve begun to do that for the Prod type. Cop/CopK need similiar treatment.
Dylan Arnold
@DylanArnold
Dec 04 2017 22:12
Does anyone know of any examples of using Validated with 1.0.0-RC1? I'm a cats noob and most tutorials I've found I can't seem to compile. Thanks.
Fabio Labella
@SystemFw
Dec 04 2017 22:15
@DylanArnold can you post an example that doesn't compile
my guess is that they are using the old applicative syntax
replace (fa |@| fb).map(foo) with (fa, fb).mapN(foo)
add -Ypartial-unification to your compiler options
import cats._,implicits._, data.Validated (and no other cats import)
and you should be good to go
Dylan Arnold
@DylanArnold
Dec 04 2017 22:19
@SystemFw This is what I started with. http://eed3si9n.com/herding-cats/Validated.html I've been looking at the cats migration notes and see that there have been changes etc but there is a lot to take in all at once.
Okay I think I've tried something close to that but I'll try again now that I know that's the right direction. Thanks.
Fabio Labella
@SystemFw
Dec 04 2017 22:21
see that result
replace each |@| with a comma
(just make a tuple, basically)
then replace map with mapN
that code can also be simplified btw
scala> val result =
         (valid[NEL[String], String]("event 1 ok") |@|
           invalid[NEL[String], String](NEL.of("event 2 failed!")) |@|
           invalid[NEL[String], String](NEL.of("event 3 failed!"))) map {_ + _ + _}
becomes
Christopher Davenport
@ChristopherDavenport
Dec 04 2017 22:23
Are we looking at Validated or ValidatedNel?
Fabio Labella
@SystemFw
Dec 04 2017 22:24
scala> val result =
     ( "event 1 ok".validNel[String], 
       "event 2 failed!".invalidNel[String], 
       "event 3 failed!".invalidNel[String]) mapN {_ + _ + _}

res6: cats.data.ValidatedNel[String,String] = Invalid(NonEmptyList(event 2 failed!, event 3 failed!))
but make sure you have the imports and the compiler option
@DylanArnold
the thing this code doesn't show is that when you call .validNel and .invalidNel you need to explicitly give it the type of the other side of the Validation
so if it's valid, give it the error type, if it's invalid, the result type
in the example they happen to be the same
Dylan Arnold
@DylanArnold
Dec 04 2017 22:32
@SystemFw Ah yes perfect. Your example works. Thanks for all the explanations. Makes sense now :)
Fabio Labella
@SystemFw
Dec 04 2017 22:34
no problem
let me know if you stumble into any other problem
the only thing you need to always keep in mind
add that partial unification option
Dylan Arnold
@DylanArnold
Dec 04 2017 22:35
Yep. Got it thanks.
I'm wondering if I can use Future or for example slick DBIO with Validated, any idea if it's possible?
Fabio Labella
@SystemFw
Dec 04 2017 22:36
you can
generally speaking, these are good to use if you're starting out
however
once you learn more about FP, you will realise that Future is not a good fit for it
Christopher Davenport
@ChristopherDavenport
Dec 04 2017 22:37
Future === :fire:
Dylan Arnold
@DylanArnold
Dec 04 2017 22:41
I see. The main thing I suppose right now that I need to figure out is Validated interacting with a database, e.g validating if a username exists etc. What should I learn?
Adelbert Chang
@adelbertc
Dec 04 2017 22:46
@DylanArnold id say start with DBIO
youll probably end up with something like DBIO[Validated[Error, Username]]
you can make it easier to work with by defining an Applicative for DBIO (or maybe Monad) if possible, im not sure how lawful DBIO is
and then using something like Nested[DBIO, Validated[Error, ?], ?]
Dylan Arnold
@DylanArnold
Dec 04 2017 22:50
@adelbertc Thanks for the info. Will give it a shot :)
Adelbert Chang
@adelbertc
Dec 04 2017 22:55
awesome :+1: