4 ms·
Here's a couple ways of doing that, packaged as a runnable .exs script that you can mess around with: https://gist.github.com/tomku/21eab22e98950f31a072a2ac639
by tomku 10y ago
Here's a couple ways of doing that, packaged as a runnable .exs script that you can mess around with:
https://gist.github.com/tomku/21eab22e98950f31a072a2ac63900b16 https://gist.github.com/tomku/21eab22e98950f31a072a2ac63900b...
The key ingredient is Enum.with_index, which pairs your enumerable with the indices that you'd have in an iterative 'for' loop in another language. I've also changed from Task.start to Task.async so that I can collect the tasks and wait for them at the end of the script.
Edit: The recursive approach jdimov10 explained is really useful too, you'll see that pattern of using a recursive function's arguments to hold state all over the functional programming world.
- firasd 10y agoThanks for the code sample! Will give it a try.