<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 13, 2017, at 4:40 PM, Maxim Veksler via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">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 class=""><br class=""></div><div class="">How would flatMap be implemented if would have been stand alone?</div><div class=""><br class=""></div><div class="">This for ex. I can't get to compile:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'anonymous pro';" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> flatMap<ElementOfResult, Element, T: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">Sequence</span><span style="font-variant-ligatures:no-common-ligatures" class="">>(on: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">T</span><span style="font-variant-ligatures:no-common-ligatures" class="">, </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">_</span><span style="font-variant-ligatures:no-common-ligatures" class=""> transform: (</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">Element</span><span style="font-variant-ligatures:no-common-ligatures" class="">) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">throws</span><span style="font-variant-ligatures:no-common-ligatures" class=""> -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">ElementOfResult</span><span style="font-variant-ligatures:no-common-ligatures" class="">?</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'anonymous pro'; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> ) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">rethrows</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> -> [</span><span style="font-variant-ligatures:no-common-ligatures" class="">ElementOfResult</span><span style="font-variant-ligatures: no-common-ligatures;" class="">] {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'anonymous pro';" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">var</span><span style="font-variant-ligatures:no-common-ligatures" class=""> result: [</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">ElementOfResult</span><span style="font-variant-ligatures:no-common-ligatures" class="">] = []</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'anonymous pro';" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">for</span><span style="font-variant-ligatures:no-common-ligatures" class=""> element </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">in</span><span style="font-variant-ligatures:no-common-ligatures" class=""> on {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'anonymous pro';" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">if</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> newElement = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">try</span><span style="font-variant-ligatures:no-common-ligatures" class=""> transform(element) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'anonymous pro';" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> result.append(newElement)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'anonymous pro';" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'anonymous pro';" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'anonymous pro';" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> result</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'anonymous pro';" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div></div></div></div></blockquote><div><br class=""></div>This doesn’t compile because there’s no relation between T.Element.Iterator and Element. I think you want to remove the Element type parameter altogether, and make the input type of flatMap into T.Element.Iterator.</div><div><br class=""></div><div>Slava</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class="">Additionally please see 2 related SO questions, for background.</span></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><a href="http://stackoverflow.com/q/42213656" class="">http://stackoverflow.com/q/42213656</a><br class=""></span></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><a href="http://stackoverflow.com/q/42214880" class="">http://stackoverflow.com/q/42214880</a><br class=""></span></div></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>