Question: I have some scala futures. I can easily run them in parallel with `Future.sequence`. I can also run them one-after-another with something like this:

```
def serFut[A, B](l: Iterable[A])(fn: A ⇒ Future[B]) : Future[List[B]] =
  l.foldLeft(Future(List.empty[B])) {
    (previousFuture, next) ⇒
      for {
        previousResults ← previousFuture
        next ← fn(next)
      } yield previousResults :+ next
  }

```

(Described [here](http://www.michaelpollmeier.com/execute-scala-futures-in-serial-one-after-the-other-non-blocking/)). Now suppose that I want to run them *slightly* in parallel - ie with the constraint that at most `m` of them are running at once. The above code does this for the special case of `m=1`. Is there a nice scala-idiomatic way of doing it for general `m`? Then for extra utility, what's the most elegant way to implement a kill-switch into the routine? And could I change `m` on the fly?

My own solutions to this keep leading me back to procedural code, which feels rather wimpy next to scala elegance.

Answer: 
You should accumulate the results in a dictionary. You should use the values of 'a' and 'b' to form a key of this dictionary

Here, I have used a `defaultdict` to accumulate the entries

```
l1 = [{'a': 1, 'b': 2, 'c': 3, 'd': 4}, {'a': 5, 'b': 6, 'c': 7, 'd': 8}]
l2 = [{'a': 5, 'b': 6, 'e': 100}, {'a': 1, 'b': 2, 'e': 101}]

from collections import defaultdict
D = defaultdict(dict)
for lst in l1, l2:
    for item in lst:
        key = item['a'], item['b']
        D[key].update(item)

l3 = D.values()
print l3

```

output:

```
[{'a': 1, 'c': 3, 'b': 2, 'e': 101, 'd': 4}, {'a': 5, 'c': 7, 'b': 6, 'e': 100, 'd': 8}]

```
One can achieve a nice and quick solution using pandas.

```
l1 = [{'a': 1, 'b': 2, 'c': 3, 'd': 4}, {'a': 5, 'b': 6, 'c': 7, 'd': 8}]
l2 = [{'a': 5, 'b': 6, 'e': 100}, {'a': 1, 'b': 2, 'e': 101}]

import pandas as pd
pd.DataFrame(l1).merge(pd.DataFrame(l2), on=['a','b']).to_dict('records')

```