<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Nov 9, 2017, at 11:43 AM, Vladimir.S via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">let a : [Int?] = [1,2,3,nil,4,nil,5]</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">let b = a.flatMap { $0.flatMap{$0*10} } &nbsp;// current</span></div></blockquote></div><br class=""><div class="">At the risk of taking us further down the rabbit hole…</div><div class=""><br class=""></div><div class="">You really want:</div><div class=""><br class=""></div><div class="">&nbsp; let b = a.flatMap { $0.map{$0*10} } &nbsp;// current<br class=""><br class=""></div><div class="">here. That is, a’s Element is Int?, and you want to apply Int-&gt;Int ($0*10) within the Int?, which is done with Optional.map&lt;U&gt;(_:(Wrapped)-&gt;<b class="">U</b>)-&gt;U?</div><div class=""><br class=""></div><div class="">Optional.flatMap&lt;U&gt;(_:(Wrapped)-&gt;<b class="">U?</b>) -&gt; U? is for when you want to apply a function that returns an optional, but want to avoid “double-wrapping". So for example, you have a [String?], and you want to turn it into [Int] dropping any strings that are either nil or not an integer, you’d write:</div><div class=""><br class=""></div><div class="">&nbsp; ["1","2",nil].flatMap&nbsp;{ $0.flatMap(Int.init) }&nbsp;</div><div class=""><br class=""></div><div class="">Which would return an [Int] of [1,2].</div><div class=""><br class=""></div><div class="">Optional.flatMap has the same “it happens to work with non-optional-returning functions” behavior as Collection.flatMap. But, though strange, it doesn’t cause the same level of active harm as Collection.flatMap. because we don’t implicitly convert elements to collections of elements.</div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>