Nested comprehensions in Elixir
<p>In Python, one can do this <code>[e2 for e1 in edits1(word) for e2 in edits1(e1)]</code>. What is the equivalent (and right) form of this construct in Elixir?</p>

<p>What I tried, is:</p>

<pre><code>def edits2(word) do
  (for e1 &lt;- edits1(word), do: edits1(e1))
  |&gt; Enum.reduce(MapSet.new, fn(item, acc) -&gt; MapSet.union(item, acc) end)
end
</code></pre>

<p>but this is awfully slow, since it happens I need to do a MapSet of hundreds of lists, each containing 500+ elements.</p>

<p>Ok, so the anwser to my initial question is just what @Dogbert suggested: <code>for e1 &lt;- edits1(word), e2 &lt;- edits1(e1), into: MapSet.new, do: e2</code></p>

<p>But the bottleneck wan't this particular line. See <a href="https://github.com/visar/spell_check/commit/857653593ca98310db028601e9cfc59dc1ac13a4?diff=split" rel="nofollow">https://github.com/visar/spell_check/commit/857653593ca98310db028601e9cfc59dc1ac13a4?diff=split</a> for some optimizations, which truncate the running time of the particular test to under 2s on my machine.</p>

<p>The killer was that <code>known/1</code> was recalculating the word keys every time - and there are thousands of them - but it could safely be a constant, so it takes a little longer to compile but it runs much faster.</p>

python elixir list-comprehension