<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="">Looks like I finally have to draw on drawings... (hope the pictures aren’t considered inappropriate content ;-)<div class=""><br class=""></div><div class="">But after all, it might be the best way to convey my point anyways: I’m not thinking in terms of type names (why should it only be possible to flatten a „Sequence“? Many people associate this operation with lists, which aren’t common in Swift at all) or implementation details (neither Optional nor Sequence actually has a flatten-method).</div><div class="">Instead, I tend to look at the easiest way to explain how to get from input to output, so here is what I have in my mind when I speak about Sequence-Sequence flatmap:<br class=""><div class=""><br class=""><div class=""><img apple-inline="yes" id="60A3DD70-C0BD-41F9-8C00-F39181017DED" src="cid:BAC01AA1-C5B0-43BC-BC66-0A879FE110C3@fritz.box" class=""></div><div class=""></div><div class=""><a href="https://imgur.com/hy4rel1" class="">https://imgur.com/hy4rel1</a></div><div class=""><br class=""></div><div class="">There is a transform, which turns an input value (red circle) into a collection (blue basket) of output values (green squares).</div><div class="">map takes an array of those input values, and stores each result-collection in an array (light blue).</div><div class="">flatten takes that container, unwraps each sub-collection, and stores its contents together with the other elements.</div><div class="">Note that empty collections aren’t skipped or removed — they just have nothing that they could contribute to the final result, so no trace of them is left.</div><div class="">flatMap just is flatten performed on the output of map.</div><div class=""><br class=""></div><div class="">Now, Optionals… I could simply say „think of those as an array with a maximal size of one“ — but because there’s really no fundamental difference, I just copied the sequence-illustration, and changed one tiny bit: The transformation doesn’t return multiple items now, so that the blue basket can act as representation for an Optional.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><img apple-inline="yes" id="F2581785-F697-488D-BB72-2212A9EF5E8B" src="cid:3A077B2B-08A9-46DA-9766-48ACE045556C@fritz.box" class=""></div><div class=""><a href="https://imgur.com/qq95b31" class="">https://imgur.com/qq95b31</a></div><div class=""><br class=""></div></div></div><div class="">So conceptionally, both overrides are equivalent: The transformation always produces something — an Optional, and that can contain a value, or it can be empty, just like an array.</div><div class="">You also see that the flatten step really does what it says: It removes one layer of objects, and leaves a flush surface without gaps.</div><div class=""><br class=""></div><div class="">So, that should really be my last word in this discussion — I won’t add an animation or start singing about Optionals ;-)</div><div class=""><br class=""></div><div class="">- Tino</div><div class=""><br class=""></div></body></html>