Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As pointed out by Ben James, you are looking for Miles Sabin's <a href="http://halcat0x15a.github.com/scalaz/core/target/scala-2.9.2/api/scalaz/Unapply.html" rel="noreferrer"><code>Unapply</code> trick</a>. <a href="https://github.com/scalaz/scalaz/blob/scalaz-seven/core/src/main/scala/scalaz/Unapply.scala" rel="noreferrer">Here</a> it is in scalaz repo. <a href="https://github.com/scalaz/scalaz/blob/scalaz-seven/core/src/main/scala/scalaz/Traverse.scala#L49" rel="noreferrer">Here's</a> <code>traverseU</code>, implemented with its help. <a href="https://github.com/scalaz/scalaz/blob/scalaz-seven/example/src/main/scala/scalaz/example/UnapplyInference.scala" rel="noreferrer">Here</a> are some example usages. And here's my sketchy (hopefully correct) implementation for your particular case (note: I've renamed your <code>Applicative</code> to <code>ApplicativeTest</code> not to interfere with <code>Applicative</code>, defined in scalaz):</p> <pre><code>scalaz&gt; core/console [warn] Credentials file /home/folone/.ivy2/.credentials does not exist [info] Starting scala interpreter... [info] Welcome to Scala version 2.9.2 (OpenJDK 64-Bit Server VM, Java 1.7.0_15). Type in expressions to have them evaluated. Type :help for more information. scala&gt; :paste // Entering paste mode (ctrl-D to finish) import scalaz._ trait ApplicativeTest[AF[_]] { def ap[A, B](a: AF[A])(f: AF[A =&gt; B]): AF[B] def pure[A](a: A): AF[A] def fmap[A, B](a: AF[A])(f: A =&gt; B): AF[B] } def traverse_[AP, A](xs: Iterable[A])(f: A =&gt; AP)(implicit G: Unapply[ApplicativeTest, AP]): G.M[Unit] = { (xs :\ G.TC.pure(())) { (x, acc) =&gt; val apFunc = G.TC.fmap(G(f(x)))(a =&gt; identity[Unit] _) G.TC.ap(acc)(apFunc) } } implicit def optionAp = new ApplicativeTest[Option] { def ap[A, B](a: Option[A])(f: Option[A =&gt; B]): Option[B] = f flatMap (a map _) def pure[A](a: A) = Some(a) def fmap[A, B](a: Option[A])(f: A =&gt; B) = a map f } implicit def eitherAp[L]: ApplicativeTest[({type l[x]=Either[L, x]})#l] = new ApplicativeTest[({type l[x]=Either[L, x]})#l] { def ap[A, B](a: Either[L, A])(f: Either[L, A =&gt; B]): Either[L, B] = f.right flatMap (a.right map _) def pure[A](a: A) = Right(a) def fmap[A, B](a: Either[L, A])(f: A =&gt; B) = a.right map f } implicit def iterAp = new ApplicativeTest[Iterable] { def ap[A, B](a: Iterable[A])(f: Iterable[A ⇒ B]): Iterable[B] = f flatMap(a map _) def pure[A](a: A) = Iterable(a) def fmap[A, B](a: Iterable[A])(f: A ⇒ B) = a map f } // Exiting paste mode, now interpreting. import scalaz._ defined trait ApplicativeTest traverse_: [AP, A](xs: Iterable[A])(f: A =&gt; AP)(implicit G: scalaz.Unapply[ApplicativeTest,AP])G.M[Unit] optionAp: java.lang.Object with ApplicativeTest[Option]{def pure[A](a: A): Some[A]} eitherAp: [L]=&gt; ApplicativeTest[[x]Either[L,x]] iterAp: java.lang.Object with ApplicativeTest[Iterable] scala&gt; val x = traverse_(1 to 10) { | case 5 =&gt; None | case _ =&gt; Some(()) | } x: Option[Unit] = None scala&gt; val y = traverse_(1 to 10) { | case 5 =&gt; Left("x"): Either[String, Unit] | case _ =&gt; Right(()) | } y: Either[String,Unit] = Left(x) </code></pre> <p>I still don't know how to make it infer <code>Either[String, Unit]</code> instead of <code>Product with Serializable with scala.util.Either[String,Unit]</code> other than strictly specify type in one of the cases like I did on this line: <code>case 5 =&gt; Left("x"): Either[String, Unit]</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload