Converting a tuple of options to an option of tuple with Scalaz or Shapeless

后端 未结 5 1410
误落风尘
误落风尘 2020-12-15 06:45

Having

(Some(1), Some(2))

I expect to get

Some((1, 2))

and having

(Some(1), None)
         


        
5条回答
  •  天命终不由人
    2020-12-15 07:34

    You can use the fact that Scalaz 7 provides a Bitraverse instance for tuples and then sequence as usual (but with bisequence instead of sequence):

    scala> import scalaz._, std.option._, std.tuple._, syntax.bitraverse._
    import scalaz._
    import std.option._
    import std.tuple._
    import syntax.bitraverse._
    
    scala> val p: (Option[Int], Option[String]) = (Some(1), Some("a"))
    p: (Option[Int], Option[String]) = (Some(1),Some(a))
    
    scala> p.bisequence[Option, Int, String]
    res0: Option[(Int, String)] = Some((1,a))
    

    Unfortunately Scalaz 7 currently needs the type annotation here.


    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 bisequence method and won't require a type annotation:

    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)
    

    Now (some(1), some("a")).bisequence will compile just fine.

    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.

提交回复
热议问题