Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the fact that Scalaz 7 provides a <code>Bitraverse</code> instance for tuples and then sequence as usual (but with <code>bisequence</code> instead of <code>sequence</code>):</p> <pre><code>scala&gt; import scalaz._, std.option._, std.tuple._, syntax.bitraverse._ import scalaz._ import std.option._ import std.tuple._ import syntax.bitraverse._ scala&gt; val p: (Option[Int], Option[String]) = (Some(1), Some("a")) p: (Option[Int], Option[String]) = (Some(1),Some(a)) scala&gt; p.bisequence[Option, Int, String] res0: Option[(Int, String)] = Some((1,a)) </code></pre> <p>Unfortunately Scalaz 7 currently needs the type annotation here.</p> <hr> <p>In a comment Yo Eight states that the type annotation will remain mandatory here. I'm not sure what his or her reasoning is, but it's in fact perfectly easy to write your own wrapper that will provide any appropriately typed tuple with a <code>bisequence</code> method and won't require a type annotation:</p> <pre><code>import scalaz._, std.option._, std.tuple._ class BisequenceWrapper[F[_, _]: Bitraverse, G[_]: Applicative, A, B]( v: F[G[A], G[B]] ) { def bisequence = implicitly[Bitraverse[F]].bisequence(v) } implicit def bisequenceWrap[F[_, _]: Bitraverse, G[_]: Applicative, A, B]( v: F[G[A], G[B]] ) = new BisequenceWrapper(v) </code></pre> <p>Now <code>(some(1), some("a")).bisequence</code> will compile just fine.</p> <p>I can't think of a good reason Scalaz wouldn't include something like this. Whether or not you want to add it in the meantime is a matter of taste, but there's definitely no theoretical obstacle to letting the compiler do the typing here.</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