Thanks for the suggestions.
I might have simplified a little too much. My actual use case looks (with a more complex merge method) more like:
case class Foo(x: Option[String], y: Option[Int])
val foos = Seq(Foo(Some("abc"), None),
Foo(None, Some(42))
)
def merge[T](f: Foo => Option[T]): Option[T] = foos.map(f).flatten.headOption
val x = merge(_.x)
val y = merge(_.y)
val result = Foo(x,y)
So I’d rather keep the filter away from the use site where the function is defined.
The implicit thing is clever. I’m not sure where the actual implicit conversion is defined (it’s not in Predef) so I can’t take a look, but are
f andThen implicitly[Option[T] => TraversableOnce[T]]
Implicit lookup for conversions also looks in the companion object of the source type, so implicitly[Option[T] => TraversableOnce[T]] should probably return option2iterable from Option’s companion, the implementation of which is just calling toList.