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

后端 未结 5 1388
误落风尘
误落风尘 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:07

    • Starting Scala 2.13, this exact behavior is provided in the standard library by Option#zip:

      Some(2) zip Some('b') // Some((2, 'b'))
      Some(2) zip None      // None
      None zip Some('b')    // None
      None zip None         // None
      
    • Before Scala 2.13, Option#zip was returning an Iterable and it was possible to combine it with headOption:

      Some(2) zip Some('b') headOption // Some((2, 'b'))
      Some(2) zip None headOption      // None
      

提交回复
热议问题