These are chat archives for typelevel/cats

27th
Apr 2018
Oleg Pyzhcov
@oleg-py
Apr 27 04:06
@Jacoby6000 that's scaladoc for partition, not groupBy
siyan
@blazingsiyan
Apr 27 08:28
hi~ is there any syntax for working with M[Boolean]? like regular short circuited logic...
Matt Russell
@mattrussell-sky
Apr 27 08:40
I can see how cats.free.Yoneda relates to the Yoneda lemma in category theory, but I'm a bit stumped as to how Coyoneda is "the dual view of the Yoneda lemma" -- has anyone got a good link (or quick explanation)?
Gabriel Volpe
@gvolpe
Apr 27 08:44
@mattrussell-sky I find Bartosz's articles hard to beat: https://bartoszmilewski.com/2015/09/01/the-yoneda-lemma/
Fabio Labella
@SystemFw
Apr 27 08:49
@doubajam there's ifM. I think we should add more stuff like whenM/unlessM
Oleg Pyzhcov
@oleg-py
Apr 27 08:51
we have whenA and unlessA in applicative syntax too, but it's backwards, e.g.
doStuff.whenA(true)
Matt Russell
@mattrussell-sky
Apr 27 08:53
@gvolpe Definitely a decent resource, but...the "Coyoneda" in Bartosz's article is forall x . (x -> a) -> F x, but I don't see the connection to cats.free.Coyoneda (https://github.com/typelevel/cats/blob/master/free/src/main/scala/cats/free/Coyoneda.scala), which is more like (f b, b -> a)
Fabio Labella
@SystemFw
Apr 27 09:02
@oleg-py whenA is a different op
it doesn't operate on M[Boolean], Applicatives can't do that
you need monadic power
ah, sorry, I think I slightly misread what you said :)
siyan
@blazingsiyan
Apr 27 09:05
@SystemFw will it have some sugar ops looks like (&& or ||)? :smiley:
Fabio Labella
@SystemFw
Apr 27 09:56
ah, in that sense
I'd actually be mildly against it
I think(b1, b2).mapN(_ && _) is not that bad
siyan
@blazingsiyan
Apr 27 10:23
@SystemFw ok, but it’s not short-circuited
Oleg Pyzhcov
@oleg-py
Apr 27 10:24
Yep, it has to be something along the lines of b1.ifM(b2, false.pure[M])
Fabio Labella
@SystemFw
Apr 27 10:24
@doubajam that's ifM
but I see what you mean now
Marko Dimjašević
@mdimjasevic
Apr 27 12:49
Hi everyone! I realize the following might be a question for the #scalacheck channel, but I am hoping to see a more fruitful discussion here.
I have a state machine and I'd like to generate random sequences of legal transitions within the machine. How would I do that?
One idea that comes to my mind is to use the IndexedStateT[F[_], SA, SB, A] data type. With it I could encode legal transitions. However, how do I generate sequences that respect these types in my state machine?
that is, if I have these states and transitions: A -> B, A -> C and B -> C, how do I generate random sequences of transitions A -> C? One would be A -> C and the other one would be A -> B -> C.
Luciano
@lJoublanc
Apr 27 12:57
There is a scalacheck-shapeless lib, and also gitter channel. They may be a starting point, for creating tests for generic parameters (essentially what you want is a cartesian product of the states, right?)
Marko Dimjašević
@mdimjasevic
Apr 27 13:02
@lJoublanc : I guess you can put it that way. Thanks for the pointer!
Luciano
@lJoublanc
Apr 27 13:03
Also have you looked at the guide? The last bit is on stateful testing: https://github.com/rickynils/scalacheck/blob/master/doc/UserGuide.md#stateful-testing
Eugene Platonov
@jozic
Apr 27 14:41
Hi All, is there a good reason why Functor doesn't have something like def transform[A](fa: F[A])(pf: PartialFunction[A,A]): F[A] = map(fa)(a => if (pf.isDefinedAt(a)) pf(a) else a)
Rob Norris
@tpolecat
Apr 27 14:44
We don't really do much with PF in cats.
Fabio Labella
@SystemFw
Apr 27 14:44
what's the relation between that and Functor?
Rob Norris
@tpolecat
Apr 27 14:45
.map(pf.getOrElse(_, identity)) or something like that
Eugene Platonov
@jozic
Apr 27 14:45
functor has map, and this is sort of like map, but for some values only
Fabio Labella
@SystemFw
Apr 27 14:45

a => if (pf.isDefinedAt(a)) pf(a) else a

this logic seems completely unrelated to Functor, it's just another thing you are mapping

functor has map, and this is sort of like map, but for some values only

I think this would be the case if F could somehow absorbe an empty value (kinda like FunctorFilter)

but since you are returning a default value, it seems to me that there's nothing specific to functor to it
Eugene Platonov
@jozic
Apr 27 14:47
@tpolecat sure, there are many ways to do it, but it looks ugly and verbose :)
Rob Norris
@tpolecat
Apr 27 14:48
I guess a question is, why are you using PF?
Eugene Platonov
@jozic
Apr 27 14:49
i see that it can be not related to functor, but what about functions like def void[A](fa: F[A]): F[Unit] = map(fa)(_ => ()) in Functor? it looks similar, sort of
Fabio Labella
@SystemFw
Apr 27 14:50
eh, fair enough
Eugene Platonov
@jozic
Apr 27 14:50
as for PF, i guess i had cases where I needed to map over a list of different values and transform only some of those
Schmitt Christian
@schmitch
Apr 27 14:50
is it possible if I have a EitherT[List, X, Y] to get a EitherT[Future, List[X], List[Y]] with some kind of magic function?
Rob Norris
@tpolecat
Apr 27 14:51
Unit is special. That ends up being very commonly used.
Oleg Pyzhcov
@oleg-py
Apr 27 14:51
@jozic you can always make your function total by adding case a => a :)
Michael Pilquist
@mpilquist
Apr 27 14:52
my take: derived functions like void and as(x) are justified in terms of some vague notion of widespread applicability - this is an important bar to clear b/c otherwise we'd have a million derived functions on every type class. it's also frustrating as there's no good process for determining how widely applicable a suggested combinator is
Cary Robbins
@carymrobbins
Apr 27 14:52
pf.lift.andThen(_.getOrElse(default))
Michael Pilquist
@mpilquist
Apr 27 14:53
we use this same philosophy in FS2 -- we try to minimize the size of the API and focus on methods that combine well with each other to build use case specific abstractions
Eugene Platonov
@jozic
Apr 27 14:54
@oleg-py @carymrobbins that's all true, but as I said earlier, it's verbose and ugly
@tpolecat if Unitis special, then what about def as[A, B](fa: F[A], b: B): F[B] = map(fa)(_ => b) or def tupleRight[A, B](fa: F[A], b: B): F[(A, B)] = map(fa)(a => (a, b))
what is this line where something is good enough to be added to Functor?
Cary Robbins
@carymrobbins
Apr 27 14:54
or rather, pf.orElse { case a => a }
what is this line where something is good enough to be added to Functor?
Fabio Labella
@SystemFw
Apr 27 14:55
@jozic I think another problem there is that all types currently involved in Functor (tuple, unit, A) are considered pretty unambiguously ok, where PF is not. PF is pretty bad...
Cary Robbins
@carymrobbins
Apr 27 14:56
also, void is sort of standard function which comes from Haskell
Fabio Labella
@SystemFw
Apr 27 14:56
there are use cases it for it (e.g. in ApplicativeError)
Cary Robbins
@carymrobbins
Apr 27 14:56
but notice that Haskell doesn't define void in the Functor type class
Fabio Labella
@SystemFw
Apr 27 14:56
anyway I think @mpilquist 's answer is the most honestly correct
there isn't a set rule
Rob Norris
@tpolecat
Apr 27 14:56
I think we just don't use PF in this style of programming, other than literal PFs in things like .collect which does end up being kind of nice.
The existing combinators are ones that have proven to be generally useful.
Cary Robbins
@carymrobbins
Apr 27 14:58
it seems to me that void is included in Functor for convenience since otherwise it'd have to be just an extension method
Fabio Labella
@SystemFw
Apr 27 14:58
void is super commonly used
Michael Pilquist
@mpilquist
Apr 27 14:59
in this particular example, another thing going against it is you can easily justify def transformE[A,B](fa: F[A])(pf: PartialFunction[A,B]): F[Either[A,B]] if you can justify transform
Fabio Labella
@SystemFw
Apr 27 14:59
it's the pure equivalent of havingUnit inserted in impure code
Eugene Platonov
@jozic
Apr 27 15:00

right, it's convenience

anyway I think @mpilquist 's answer is the most honestly correct

@SystemFw sorry, I don't see an answer from @mpilquist on this question

Cary Robbins
@carymrobbins
Apr 27 15:00
@jozic for one off things like this, i usually just define my own extension method
final implicit class MyFunctorOps[F[_], A](val repr: F[A]) extends AnyVal {
  def transform(pf: PartialFunction[A, A])(implicit F: Functor[F]): F[A] = ???
}
Michael Pilquist
@mpilquist
Apr 27 15:00
Fabio Labella
@SystemFw
Apr 27 15:00
Cary Robbins
@carymrobbins
Apr 27 15:00
if you really find yourself doing it that much
Eugene Platonov
@jozic
Apr 27 15:02

@carymrobbins i do too, and just wanted to know what people think on it
@SystemFw @mpilquist thanks, makes sense
as for

you can easily justify def transformEA,B(pf: PartialFunction[A,B]): F[Either[A,B]] if you can justify transform

i think what I had is different as it didn't involve additional type like Either

Cary Robbins
@carymrobbins
Apr 27 15:04
maybe that point was demonstrating that there are a seemingly limitless number of alternatives that could be proposed, and the methods on type classes need to be exclusive to avoid bloat
Eugene Platonov
@jozic
Apr 27 15:05
Sure,
so as I see it, the line is, 1) PFs are not good and 2) we add things for convenience only when they prove to be super useful to keep minimal API
would that be correct summary?
Fabio Labella
@SystemFw
Apr 27 15:08
pretty much, but with respect to 2), it's a loose rule, so I'm sure you could find counterexamples. My personal opinion is that that specific code doesn't qualify as common enough
Eugene Platonov
@jozic
Apr 27 15:14
cool, thanks everyone
OlegYch
@OlegYch
Apr 27 18:29
what would the equivalent for true.option(List(1)).sequence from scalaz?
Rob Norris
@tpolecat
Apr 27 18:35
Indentical if you pull in mouse. The boolean op isn't in cats itself.
OlegYch
@OlegYch
Apr 27 18:39
any better alternative envisioned when boolean ops were removed?
Rob Norris
@tpolecat
Apr 27 18:40
They were never part of cats. We decided not to add all those enrichments although I think mouse should just be folded in.
At the time I think people overestimated the size.
OlegYch
@OlegYch
Apr 27 18:41
i see thanks
@LukaJCB how are implicit function types relevant?
they give you an ambient reader which is just one effect
I can't see how they're useful at all, honestly.
Luka Jacobowitz
@LukaJCB
Apr 27 21:29
implicit functions can also encode CPS
So you could do tagless final with implicit functions
But yeah very loosely :smile:
Rob Norris
@tpolecat
Apr 27 21:30
I guess I'm worried martin will see that tweet and say ok problem solved. Implicit functions do everything.
Luka Jacobowitz
@LukaJCB
Apr 27 21:38
Yeah, that's not what I meant
Luka Jacobowitz
@LukaJCB
Apr 27 21:55
I added some clarification 😁
Rob Norris
@tpolecat
Apr 27 21:57
;-)
Cary Robbins
@carymrobbins
Apr 27 22:24
does cats-effect have MonadIO? or something analogous?
Rob Norris
@tpolecat
Apr 27 22:27
You can ask for F[_]: Monad: LiftIO