Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One way might be to use the Bifunctor instance:</p> <pre><code>scala&gt; val t = (Option(1), Option("str")) t: (Option[Int], Option[java.lang.String]) = (Some(1),Some(str)) scala&gt; import scalaz._, Scalaz._, Tags._ import scalaz._ import Scalaz._ import Tags._ scala&gt; t.bimap(First, First) res0: (scalaz.package.@@[Option[Int],scalaz.Tags.First], scalaz.package.@@[Option[java.lang.String],scalaz.Tags.First]) = (Some(1),Some(str)) </code></pre> <p>A more idiomatic way (ok, a more generic way: would work for any tuple, tripple, etc.) would probably be using <a href="https://github.com/milessabin/shapeless" rel="nofollow noreferrer">shapeless</a> to turn your tuple into an <code>HList</code>, and then apply a natural transformation <code>Option[A] ~&gt; Option[A] @@ First</code>. Here's a sketchy implementation of this:</p> <pre><code>scala&gt; import scalaz._, Scalaz._, Tags._ import scalaz._ import Scalaz._ import Tags._ scala&gt; import shapeless._, Tuples._, Nat._ import shapeless._ import Tuples._ import Nat._ scala&gt; val t = (Option(1), Option("str")) t: (Option[Int], Option[String]) = (Some(1),Some(str)) scala&gt; object optionToFirstoption extends (Option ~&gt; FirstOption) { | def apply[A](fa: Option[A]): Option[A] @@ First = First(fa) | } defined module optionToFirstoption scala&gt; t.hlisted.map(optionToFirstoption).tupled res1: (Option[Int] with scalaz.Tagged[scalaz.Tags.First], Option[String] with scalaz.Tagged[scalaz.Tags.First]) = (Some(1),Some(str)) </code></pre>
 

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