Applicative syntax question

110 views
Skip to first unread message

Alec Zorab

unread,
Apr 16, 2013, 4:43:04 AM4/16/13
to scala-fu...@googlegroups.com
Hey,

I'm introducing a friend of mine to a lot of stuff through Scalaz, and he's come back with a question I couldn't answer:

Why do we have to do this:

streamZipApplicative.apply3(Zip(astream), Zip(bstream), Zip(cstream))((_,_,_))

instead of this:
 
(Zip(astream) |@| Zip(bstream) |@| Zip(cstream))((_,_,_))

?


Lars Hupel

unread,
Apr 16, 2013, 4:51:41 AM4/16/13
to scala-fu...@googlegroups.com
Hi Alec,

using Scala 2.9.2 and scalaz 7.1-SNAPSHOT:

scala> import scalaz._
import scalaz._

scala> import scalaz.std.stream._
import scalaz.std.stream._

scala> import scalaz.syntax.apply._
import scalaz.syntax.apply._

scala> import scalaz.Tags.Zip
import scalaz.Tags.Zip

scala> (Zip(Stream(1,2,3)) |@| Zip(Stream(4,5,6))) { _ + _ }
res10: scala.collection.immutable.Stream[Int] = Stream(5, ?)

It is a bit unfortunate that we have a type class called `Zip` and also
a tag called `Zip`, though.

Hope that helps
Lars

Alec Zorab

unread,
Apr 16, 2013, 5:01:25 AM4/16/13
to scala-fu...@googlegroups.com
Welcome to Scala version 2.10.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_10).
Type in expressions to have them evaluated.
Type :help for more information.

scala>  import scalaz._
import scalaz._

scala> import scalaz.std.stream._
import scalaz.std.stream._

scala> import scalaz.syntax.apply._
import scalaz.syntax.apply._

scala> import scalaz.Tags.Zip
import scalaz.Tags.Zip

scala> (Zip(Stream(1,2,3)) |@| Zip(Stream(4,5,6))) { _ + _ }
res0: scala.collection.immutable.Stream[Int] = Stream(5, ?)

scala> res0.force
res1: scala.collection.immutable.Stream[Int] = Stream(5, 6, 7, 6, 7, 8, 7, 8, 9)

res1 doesn't look like it's been zipped to me...


Lars

--
You received this message because you are subscribed to the Google Groups "scala-functional" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-function...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Lars Hupel

unread,
Apr 16, 2013, 5:19:00 AM4/16/13
to scala-fu...@googlegroups.com
> res1 doesn't look like it's been zipped to me...

You are completely right. I don't know exactly why that happens, but it
seems to me that it could be a type inference problem:

scala> import scalaz.syntax.applicative._
import scalaz.syntax.applicative._

scala> ToApplicativeOps(Zip(Stream(1,2,3))).F.pure(1).force
res11: scala.collection.immutable.Stream[Int] = Stream(1)

scala> ToApplicativeOpsUnapply(Zip(Stream(1,2,3))).F.pure(1).force
// doesn't even compile

Apparently the conversion to the trait which provides the syntax is
"lossy", i.e. tags get lost. I don't know what can be done about it.
(The only thing which comes to my mind is to provide "fat" wrappers,
i.e. `case class ZipStream[A](...)`.)
Reply all
Reply to author
Forward
0 new messages