Normen (35) [Avatar] Offline
#1
Hi,

in the sidebar on page 125 you state

>We won't discuss that here since it isn't all that relevant to what we're trying to cover, but if you're interested, check out the code associated with this chapter.

The solution, however, is incomplete:

object Par {
...
/* Gives us infix syntax for `Par`. */
implicit def toParOps[A](p: Par[A]): ParOps[A] = new ParOps(p)

class ParOps[A](p: Par[A]) {

}
}

Best, /nm
xobbit (2) [Avatar] Offline
#2
I know this is an old post but it was the second result checking for the same question on Google so throwing my answer on...

  import scala.language.implicitConversions

  implicit def toParOps[A](p: Par[A]): ParOps[A] = new ParOps(p)

  class ParOps[A](p: Par[A]) {
    def map2[B,C](b: Par[B])(f: (A,B) => C): Par[C] = Par.map2(p,b)(f)
  }


sum now works as follows...

      def sum(ints: IndexedSeq[Int]): Par[Int] =
        if (ints.length <= 1)
          unit(ints.headOption getOrElse 0)
        else {
          val (l,r) = ints.splitAt(ints.length/2)
          //map2(fork(sum(l)), fork(sum(r)))(_ + _)
          fork(sum(l)).map2(fork(sum(r)))(_ + _)
        }