<div dir="ltr">I'm trying to understand the signature of flapMap that allows it to "strip down" Optional from a type, when called on an Optional collection but remain same type when called on a non option collection.<div><br></div><div>How would flatMap be implemented if would have been stand alone?</div><div><br></div><div>This for ex. I can't get to compile:</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:'anonymous pro'"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">func</span><span style="font-variant-ligatures:no-common-ligatures"> flatMap<ElementOfResult, Element, T: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Sequence</span><span style="font-variant-ligatures:no-common-ligatures">>(on: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">T</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">_</span><span style="font-variant-ligatures:no-common-ligatures"> transform: (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">Element</span><span style="font-variant-ligatures:no-common-ligatures">) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">throws</span><span style="font-variant-ligatures:no-common-ligatures"> -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">ElementOfResult</span><span style="font-variant-ligatures:no-common-ligatures">?</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:'anonymous pro';color:rgb(52,149,175)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> ) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">rethrows</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> -> [</span><span style="font-variant-ligatures:no-common-ligatures">ElementOfResult</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">] {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:'anonymous pro'"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">var</span><span style="font-variant-ligatures:no-common-ligatures"> result: [</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)">ElementOfResult</span><span style="font-variant-ligatures:no-common-ligatures">] = []</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:'anonymous pro'"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">for</span><span style="font-variant-ligatures:no-common-ligatures"> element </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">in</span><span style="font-variant-ligatures:no-common-ligatures"> on {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:'anonymous pro'"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">let</span><span style="font-variant-ligatures:no-common-ligatures"> newElement = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">try</span><span style="font-variant-ligatures:no-common-ligatures"> transform(element) {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:'anonymous pro'"><span style="font-variant-ligatures:no-common-ligatures"> result.append(newElement)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:'anonymous pro'"><span style="font-variant-ligatures:no-common-ligatures"> }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:'anonymous pro'"><span style="font-variant-ligatures:no-common-ligatures"> }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:'anonymous pro'"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)">return</span><span style="font-variant-ligatures:no-common-ligatures"> result</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:'anonymous pro'"><span style="font-variant-ligatures:no-common-ligatures">}</span></p><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures">Additionally please see 2 related SO questions, for background.</span></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures"><a href="http://stackoverflow.com/q/42213656">http://stackoverflow.com/q/42213656</a><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures"><a href="http://stackoverflow.com/q/42214880">http://stackoverflow.com/q/42214880</a><br></span></div></div>