These are chat archives for typelevel/cats

30th
May 2016
Miles Sabin
@milessabin
May 30 2016 08:49
@soc I don't follow how your answer relates to my question?
Mark Eibes
@i-am-the-slime
May 30 2016 11:21
How do I import the applicative instance for Xor?
Peter Neyens
@peterneyens
May 30 2016 11:38
By importing cats.data.Xor, you should be able to get an applicative instance by specifying the left side, eg Applicative[Xor[String, ?]]
Mark Eibes
@i-am-the-slime
May 30 2016 11:59
specifying it where?
InTheNow
@InTheNow
May 30 2016 15:26
@non @ceedubs As it looks like you are cracking away - https://gitter.im/sbt/sbt?at=574b4bde10f0fed86f49499c
@milessabin at the current time, without the fix, I think nobody wants to commit to including some standard types, as everything with contravariance is effectively broken
Miles Sabin
@milessabin
May 30 2016 15:51
Oh, I see :-)
while I know you are not excited about having things in the std lib, I think it could really help the larger ecosystem in this case
Miles Sabin
@milessabin
May 30 2016 15:53
I have no objections to Cats having instances for stblib things.
Cody Allen
@ceedubs
May 30 2016 15:58
@InTheNow excellent! So am I reading correctly that sbt 0.13.12 will have a fix for that issue but we just don’t know the ETA yet?
InTheNow
@InTheNow
May 30 2016 15:59
I may even have an interim solution - but only may
My main reason for asking on an ETA would be to gauge how much effort I should spend on an interim solution vs real
Erik Osheim
@non
May 30 2016 16:00
@InTheNow would you be ok with us merging #1051 for now? we can re-enable these as soon as your fix is live.
i worry that we are holding up good PRs due to a known issue that will soon be a non-issue.
InTheNow
@InTheNow
May 30 2016 16:01
@non - sure, that's easy to change later
Erik Osheim
@non
May 30 2016 16:01
great, thanks!
Cody Allen
@ceedubs
May 30 2016 16:01
:thumbsup: sounds like a plan!
InTheNow
@InTheNow
May 30 2016 16:02
typelevel/cats#961 is more intrusive, however
Erik Osheim
@non
May 30 2016 16:04
@InTheNow good to know. i'm comfortable holding off on that one for now.
InTheNow
@InTheNow
May 30 2016 16:05
Also of interest could be milessabin/shapeless#551 that adds scoverage support, but disabled for 2.12.x. A similar approach (via sbt-catalysts?) for cats would enable 2.12.x support....
Erik Osheim
@non
May 30 2016 16:16
oh nice. yeah, now that 2.12 is so exciting it's probably worth starting to think about supporting it
Yilin Wei
@yilinwei
May 30 2016 16:39
@non I had a few questions when doing #1064; do you mind taking a look at the ticket when you have a little time please?
@milessabin I had things like Applicative, Functor, Monad in mind, not instances
I think it would helpful and guide a future design of collections
in the sense of "you can't sprinkle CanBuildFroms all over the place if Functor says map is nothing but A => B"
Daniel Hinojosa
@dhinojosa
May 30 2016 17:27
Dumb question that I think has been answered a thousand times before: Why do I have to call it like this Monoid[String].combine("Two", "Apex") instead of combine("Two", "Apex") and just let the implicit take over from there?
Peter Neyens
@peterneyens
May 30 2016 17:28
You could do "Two" |+| "Apex"
Yilin Wei
@yilinwei
May 30 2016 17:30
@dhinojosa I'd be surprised if the latter doesn't work?
Oh hand on
No I wouldn't
Daniel Hinojosa
@dhinojosa
May 30 2016 17:31
Yeah, I didn't think so, had me nervous there. @yilinwei
Yilin Wei
@yilinwei
May 30 2016 17:31
It's because the Monoid is the thing which has the .combine method on
Daniel Hinojosa
@dhinojosa
May 30 2016 17:31
haha
Yilin Wei
@yilinwei
May 30 2016 17:31
which means that it will try to infer the A from that Monoid apply method
which presumably infers to Nothing in this case.
As @peterneyens says, if you use |+| you sort of curry the A since the semigroup combine is on the ops.
i.e. it acts like (Monoid.apply[A](monoid: Monoid[A]): Monoid[A]).combine(String, String)
whereas in the |+| case it acts like (toSemigroupOps(String)(Monoid[String])).|+|(String)
Daniel Hinojosa
@dhinojosa
May 30 2016 17:35
Got it...
Just had to track it down
This message was deleted
import cats._
import cats.std.all._

object UsingMonoid extends App {
  import Monoid._

  assert(combine("Two", "Apex") == "TwoApex")

  //Laws:
  //Law 1: mappend mempty x = x
  println(combine(empty, "Foo") == "Foo")

  //Law 2: mappend x mempty = x
  println(combine("Foo", empty) == "Foo")

  //Law 3: mappend x (mappend y z) = mappend (mappend x y) z
  println(combine("Foo", combine("Bar", "Baz")) ==
          combine(combine("Foo", "Bar"), "Baz"))
}
That's saner
I guess most documentation uses Monoid[T].combine instead of import Monoid._ and then just use combine to make it cleaner.
But in retrospect, it is nice stating where everything is.
Daniel Hinojosa
@dhinojosa
May 30 2016 18:39
Another question, I do not find a Sum or Product in cats or algebra. Assuming I did look hard enough, Any reason for the non-existence?
Peter Neyens
@peterneyens
May 30 2016 18:41
What do you mean with Sum andProduct ?
Daniel Hinojosa
@dhinojosa
May 30 2016 18:45
Like the Haskell typeclasses Sum and Product?
@peterneyens ^^
Daniel Hinojosa
@dhinojosa
May 30 2016 19:13
So now I am moving onto Apply do I have any choice except to do a Apply[Option].something everywhere?
I tried import Apply._this time, but no dice.
Peter Neyens
@peterneyens
May 30 2016 19:20
You can use the syntax, just like 1 |+| 2 instead of Semigroup[String].combine(1, 2), you could do 5.pure[Option] instead of Applicative[Option].pure(5)
Daniel Hinojosa
@dhinojosa
May 30 2016 19:44
Hey, I like that @peterneyens thanks
You know the import to pure @peterneyens ?
Peter Neyens
@peterneyens
May 30 2016 19:49
You can use the uberimport cats.implicts._ or import cats.syntax.applicative._
Rob Norris
@tpolecat
May 30 2016 19:49
I think the buffet import cats.implicits._ will get you all this syntax.
Peter Neyens
@peterneyens
May 30 2016 19:51
Never heard it called the "buffet import" before, but I like it :smile:
Daniel Hinojosa
@dhinojosa
May 30 2016 19:54
Weird....ok I think I have that....hold on
I love buffets
Is there a nice thing to do for Functor so I don't have to do Functor[List] all the time?
Peter Neyens
@peterneyens
May 30 2016 20:03
Take a look at the syntax classes in the source : https://github.com/typelevel/cats/tree/master/core/src/main/scala/cats/syntax
Pascal Voitot
@mandubian
May 30 2016 20:05
@dhinojosa why have you to use Functor[List] ?
Daniel Hinojosa
@dhinojosa
May 30 2016 20:06
@mandubian Functor[List].map(List(1, 2, 3, 4))(x => x + 4)
it just seems that there is going to get ugly and quick if I have to type in Functor[List] everytime I want to use a map.
Pascal Voitot
@mandubian
May 30 2016 20:07
List(1, 2, 3, 4).map { _ + 4 } works too
Daniel Hinojosa
@dhinojosa
May 30 2016 20:07
Yeah, @mandubian, I would likely just use that, but I am looking for the cats typeclass way without all the extra typing.
map List(1,2,3,4) (4+) would be ideal, if I can do that in cats.
Pascal Voitot
@mandubian
May 30 2016 20:11
object Foo {
  def map[F[_], A, B](l: F[A])(f: A => B)(implicit fun: Functor[F]) = fun.map(l)(f)
}
something like that?
but you might require the now famous SI2712 plugin ;)
Don't try to reproduce too much Haskell code...
Daniel Hinojosa
@dhinojosa
May 30 2016 20:17
@mandubian Agreed, just trying to find the balance, although even if I wasn't trying to do Haskell, always typing Functor[List] or Functor[Option] is very time consuming.
Pascal Voitot
@mandubian
May 30 2016 20:18
In general, in Scala, you flatMap & map are defined directly on the structure itself
Rob Norris
@tpolecat
May 30 2016 20:19
Yeah Functor gives you .map as syntax if it's not defined already.
Daniel Hinojosa
@dhinojosa
May 30 2016 20:19
@mandubian Yes, got that already, just want to Cats equivalent without the extra-keyboard exercise.
Pascal Voitot
@mandubian
May 30 2016 20:19
but now that we have SI2712 patch, previous code becomes quite trivial :D
if you don't know SI2712 issue, I'll let you discover :D
Rob Norris
@tpolecat
May 30 2016 20:21
It's still kind of hopeless though because then you want to be able to say val x = map (4+) and get a universally quantified function back, but Scala doesn't work that way.
Daniel Hinojosa
@dhinojosa
May 30 2016 20:22
Ok.
So constrained by the language it is...
thanks @tpolecat @mandubian @peterneyens
Rob Norris
@tpolecat
May 30 2016 20:22
Although I guess in that case it would work since the function type contains both A and B ... but in general it's not going to end happily.
Pascal Voitot
@mandubian
May 30 2016 20:26
@dhinojosa you're welcome... don't hesitate to ask... if you know well haskell, you should discover things that will make you unhappy and others that might make you quite happy... different lang, different philosophy, motivations
Marius B. Kotsbak
@mkotsbak
May 30 2016 21:50
Hi, I read that .traverse should be the answers to all problems, but how to use it with XorT?
scala> def parseIntXorT(s: String): XorT[Future, NumberFormatException, Int] =
     | XorT( Future.successful(Xor.catchOnly[NumberFormatException](s.toInt)) )
parseIntXorT: (s: String)cats.data.XorT[scala.concurrent.Future,NumberFormatException,Int]

scala> val x1 = List("1", "2", "3").traverseU(parseIntXorT)
<console>:39: error: could not find implicit value for parameter U: cats.Unapply[cats.Applicative,cats.data.XorT[scala.concurrent.Future,NumberFormatException,Int]]
       val x1 = List("1", "2", "3").traverseU(parseIntXorT)
Peter Neyens
@peterneyens
May 30 2016 22:10
That works over here. What are you imports ? Do you have an implicit ExecutionContext to get the Future instances ?
Rob Norris
@tpolecat
May 30 2016 22:12
Yeah Future is a kind of a bogus ... you have to close over an ExecutionContext to get the instance.
Marius B. Kotsbak
@mkotsbak
May 30 2016 22:23
Ah, maybe that error again
That one maybe, and at least this one import cats.std.future._ solved it:
scala> val x1 = List("1", "2", "3").traverseU(parseIntXorT) x1: cats.data.XorT[scala.concurrent.Future,NumberFormatException,List[Int]] = XorT(Success(Right(List(1, 2, 3))))
Rob Norris
@tpolecat
May 30 2016 22:28
cats.implicits._ gives you that instance btw.
People seem resistant to the everything import for some reason.
Daniel Hinojosa
@dhinojosa
May 30 2016 22:29
import cats.std.all._ and import cats.implicits._ seems to not get along
Marius B. Kotsbak
@mkotsbak
May 30 2016 22:29
ah ok
Daniel Hinojosa
@dhinojosa
May 30 2016 22:29
for one I get
Error:(9, 16) could not find implicit value for parameter ev: cats.kernel.Monoid[String]
  assert(Monoid[String].combine("Two", "Apex") == "TwoApex")
               ^
Rob Norris
@tpolecat
May 30 2016 22:30
cats.implicits._ should give you everthing; you shouldn't need the other import
Daniel Hinojosa
@dhinojosa
May 30 2016 22:30
Thinking since @tpolecat is advocating import cats.implicits._ that should be my go to
Ah. haha
Thanks @tpolecat
Yep. That is badass. Thanks @tpolecat. All flows well.
Rob Norris
@tpolecat
May 30 2016 22:32
I think the intent is that import cats._, cats.data._, cats.implicits._ will bring in everything in cats. I admit that I haven't been following closely so that may not be accurate anymore.
Daniel Hinojosa
@dhinojosa
May 30 2016 23:20
Is there an Op for Applicative? Like <*> in Haskell
This message was deleted
I found (List(1,2,3,4) |@| List(5,6,7,8)).map(_+_)
which offers similar
Rob Norris
@tpolecat
May 30 2016 23:27
Yeah that's the scala formulation.