title: How do I to flatMap a Try[Option] in an idiomatic way
body: <p>I want to flatMap a Try[Option[A]] using some function that uses the value inside the Option to create another Try, and I want the solution to be simple and idiomatic. I have illustrated the problem with an example. The goal is to create a Option[Group] with members and events wrapped in a single Try that can contain errors from any of the three functions.</p>
  
  <pre><code>def getGroup(id: Long): Try[Option[Group]]
  
  def getMembersForGroup(groupId: Long): Try[Seq[Member]]
  
  def getMeetingsForGroup(groupId: Long): Try[Seq[Meeting]]
  </code></pre>
  
  <p>I find it difficult to flatMap from the Try returned by <code>getGroup</code> to the Try from the member- and meeting-functions because there's an Option "in the way". This is what i have come up with so far:</p>
  
  <pre><code>getGroup(id).flatMap(
    groupOpt =&gt; groupOpt.map(
      group =&gt; addStuff(group).map(group =&gt; Some(group))
    ).getOrElse(Success(None))
  )
  
  def addStuff(g: Group): Try[Group] =
    for {
      members &lt;- getMembersForGroup(g.id)
      meetings &lt;- getMeetingsForGroup(g.id)
    } yield g.copy(members = members, meetings = meetings)
  </code></pre>
  
  <p>What I don't like about my solution is that I have to wrap the group returned by <code>addStuff</code> in an Option to perform the getOrElse. At this point the type is Option[Try[Option[Group]]] which I think makes the solution difficult to understand at first glance.</p>
  
  <p>Is there a simpler solution to this problem?</p>