<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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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} } // 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=""> let b = a.flatMap { $0.map{$0*10} } // current<br class=""><br class=""></div><div class="">here. That is, a’s Element is Int?, and you want to apply Int->Int ($0*10) within the Int?, which is done with Optional.map<U>(_:(Wrapped)-><b class="">U</b>)->U?</div><div class=""><br class=""></div><div class="">Optional.flatMap<U>(_:(Wrapped)-><b class="">U?</b>) -> 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=""> ["1","2",nil].flatMap { $0.flatMap(Int.init) } </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>