title: How to generalize Future.sequence in Scala
body: <p>I'm trying to understand the Scala type system, and generalize the concept of <code>Future.sequence</code> which has type <code>List[Future[T]] =&gt; Future[List[T]]</code>. We can try to generalize this concept by defining a function of type <code>A[B[C]] =&gt; B[A[C]</code>.</p>
  
  <p>What is the best way to do this? I was thinking of something along the lines of the following (note that I'm using names that suggest this is a monoid, but it's not really a monoid even though it looks kind of similar):</p>
  
  <pre><code>trait Flippable[A[B[C]], B[_], C] {
    def mappend(a1: B[A[C]], a2: B[C]): B[A[C]]
    def mzero: B[A[C]]
  }
  </code></pre>
  
  <p>The above snippet does not compile because the types not consistently constrained to work with the api of the <code>mappend</code> and <code>mzero</code>. How can I specify appropriate constraints to make this compilable so that I can correctly generally define a function like Future.sequence? Are there existing higher order constructs/concepts that can be used (similar to monoid) to avoid defining the flippable trait from scratch as I'm attempting to do above?</p>