<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I had the need for a concurrent map recently. I had a part of a program which needed to read chunks of data and concurrently process them and assemble the results in an array. This isn’t necessarily as obvious as it sounds, because of arrays being value types. I came up with the following snippet which I’d like to check for correctness; it could also be helpful to others.<div class=""><br class=""></div><div class="">Perhaps this is something Dispatch should provide out-of-the-box?</div><div class=""><br class=""></div><div class="">- Karl</div><div class=""><br class=""></div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">extension&nbsp;DispatchQueue&nbsp;{</div><div class=""><br class=""></div><div class="">&nbsp;&nbsp;static&nbsp;func&nbsp;concurrentMap&lt;T&gt;(iterations:&nbsp;Int, execute block: (Int) -&gt;&nbsp;T) -&gt; [T] {</div><div class="">&nbsp; &nbsp;&nbsp;var&nbsp;result =&nbsp;Array&lt;T&gt;()</div><div class="">&nbsp; &nbsp;&nbsp;result.reserveCapacity(iterations)</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp;&nbsp;result.withUnsafeMutableBufferPointer&nbsp;{ (results:&nbsp;inout&nbsp;UnsafeMutableBufferPointer&lt;T&gt;)&nbsp;in</div><div class="">&nbsp; &nbsp; &nbsp;&nbsp;concurrentPerform(iterations: iterations) { idx&nbsp;in</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;results[idx] = block(idx)</div><div class="">&nbsp; &nbsp; &nbsp;&nbsp;}</div><div class="">&nbsp; &nbsp;&nbsp;}</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp;&nbsp;return&nbsp;result</div><div class="">&nbsp;&nbsp;}</div><div class="">}</div><div class=""><br class=""></div><div class="">extension&nbsp;Array&nbsp;{</div><div class="">&nbsp;&nbsp;func&nbsp;concurrentMap&lt;T&gt;(execute block: (Element)-&gt;T) -&gt; [T] {</div><div class="">&nbsp; &nbsp;&nbsp;return&nbsp;DispatchQueue.concurrentMap(iterations:&nbsp;count) { block(self[$0]) }</div><div class="">&nbsp;&nbsp;}</div><div class="">}</div></blockquote></body></html>