<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=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class="">Optional.map and Array.map do different things, and unifying them seems harmful.</div></div></div></blockquote><div class=""><br class=""></div><div class="">In terms of category theory, they're not different. They are both arrows from a to b in the Functor category. Obviously that's horribly abstract, so I'll put it this way: If you think of these map functions not as operations on Optional and Array, and instead think of them simply as ways to compose an arbitrary data structure, they are in fact doing the same thing. On an implementation level, and on a runtime level, they perform differently. But on a type level, a theoretical level, and a categorical level, they are the same. You merely need to accept that when using this abstraction without knowledge of the concrete type, you have no reason to make any assumptions about how the implementation and runtime will behave. At that point, all you need is to be sure that the Functor being passed in abides by the Functor laws, and that's just a matter of convention.</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="">First, I consider myself a smart person with a very broad experience with non-functional languages, but reading this makes my mind hurt, a lot:</div><div class=""><br class=""></div><div class=""><div class=""><blockquote type="cite" class=""><div class=""><div class="">&nbsp; &nbsp;typeclass Functor f where<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fmap :: (a -&gt; b) -&gt; f a -&gt; f b</div></div></blockquote>&lt;snip&gt;<br class=""><blockquote type="cite" class=""><div class=""><div class="">This makes it possible to build functions which operate not just on Maybe, but on any Functor.</div></div></blockquote><blockquote type="cite" class=""><div class=""><div class=""><br class="">&nbsp;&nbsp;&nbsp;fstrlen :: Functor f =&gt; f String -&gt; f Int<br class="">&nbsp;&nbsp;&nbsp;fstrlen fstr = fmap length faster</div></div></blockquote>&lt;snip&gt;<br class=""><blockquote type="cite" class=""><div class=""><div class="">&nbsp; &nbsp;protocol Functor {<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typealias A<br class="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;func fmap&lt;FB where FB ~= Self&gt;(f: A -&gt; FB.A) -&gt; FB<br class="">&nbsp;&nbsp;&nbsp;}<br class=""></div></div></blockquote><div class=""><br class=""></div><div class="">I understand what's going on here, but I never, ever want to see this code anywhere near (my) Swift.</div></div></div></div></blockquote><br class=""></div><div class="">HKTs that come from category theory tend to have this effect. They are hard to understand by reading the protocol definition. Admittedly, this is a big flaw with Monads and with Haskell. It takes a reasonable understanding of category theory for the purpose of this code to be obvious to the reader.</div><div class=""><br class=""></div><div class="">(I will point out that in those code examples, I used absolutely abysmal naming conventions. It could be made marginally more readable and understandable if the naming were better)</div><div class=""><br class=""></div><div class="">I'll take this time to make the point that just because you don't like the way the Functor protocol looks, doesn't mean Array and Optional aren't both Functors and Monads. As a matter of mathematics, if the Monad functions can exist on a type, and they would follow the Monad laws, that type is a Monad whether the implementor likes it or not. Of course it still needs manual implementing, but regardless, the type is theoretically a Monad. (This realization is the reason that the Java team decided to implement flatMap for Java 8's Optional class.)</div><div class=""><br class=""></div><div><blockquote type="cite" class="">I believe the way to convince the rest of us (and I would love to be convinced) is to provide a few real, “end-user” app-level use cases and explain the benefit in plain language. In particular, I don't understand the example by Jens Persson, although it seems like a valuable one.</blockquote></div><br class=""><div class="">I can (again) link to just a few of the abstract Monadic functions, all of which are very useful in practical applications.</div><div class=""><br class=""></div><div class=""><a href="https://hackage.haskell.org/package/base-4.8.1.0/docs/Control-Monad.html" class="">https://hackage.haskell.org/package/base-4.8.1.0/docs/Control-Monad.html</a></div><div class=""><br class=""></div><div class="">But perhaps it would also help to describe the architectural decisions that abiding by Monad imposes.</div><div class=""><br class=""></div><div class="">Being Monadic makes you think about data composition, which cleans up the way you design your code. Going back to the Futures example, I could implement future, and subsequently use it, like this:</div><div class=""><br class=""></div><div class=""><div class="">&nbsp; &nbsp; public class Promise&lt;T&gt; {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; private var handlers: [T -&gt; ()] = []</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; private var completed: T? = nil</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; public init() {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; private func onComplete(handler: T -&gt; ()) {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if let completed = completed {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler(completed)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handlers.append(handler)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; public func complete(t: T) {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; completed = t</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for handler in handlers {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler(t)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handlers = []</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; public var future: Future&lt;T&gt; {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Future(promise: self)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; public struct Future&lt;T&gt; {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; private let promise: Promise&lt;T&gt;</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; private init(promise: Promise&lt;T&gt;) {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.promise = promise</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; public func onComplete(handler: T -&gt; ()) {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; promise.onComplete(handler)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; }</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; public func useFutures() {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; downloadURLInFuture().onComplete { content in</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processContentAsync(content).onComplete { processed in</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processed.calculateSomethingAsync().onComplete {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(finalProduct)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; }</div></div><div class=""><br class=""></div><div class="">You can see how this resembles the infamous Node.js issue of callback hell, and how the nesting could quickly get out of hand. If only we had some form of composition... Arrows between Futures... Luckily, Future is a Monad (whether I like it or not!)</div><div class=""><br class=""></div><div class=""><div class="">&nbsp; &nbsp; // Monad</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; public extension Future {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; public static func point&lt;T&gt;(t: T) -&gt; Future&lt;T&gt; {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let promise = Promise&lt;T&gt;()</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; promise.complete(t)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return promise.future</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; public func flatMap&lt;U&gt;(f: T -&gt; Future&lt;U&gt;) -&gt; Future&lt;U&gt; {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let uPromise = Promise&lt;U&gt;()</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onComplete { t in</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f(t).onComplete { u in</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uPromise.complete(u)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return uPromise.future</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; }</div></div><div class=""><br class=""></div><div class="">Not only do I now get map and apply for free, but I also get a great method of composing Futures. The example from above can now be rewritten to be much more well composed.</div><div class=""><br class=""></div><div class=""><div class="">&nbsp; &nbsp; public func useFutures() {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; downloadURLInFuture()</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMap(processContentAsync)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMap { $0.calculateSomethingAsync() }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .onComplete { finalProduct in</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(finalProduct)</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div class="">&nbsp; &nbsp; }</div></div><div class=""><br class=""></div><div class="">The important thing here is that thinking about Monads helped me discover a better composition model. Now my code can be more readable and well composed.</div><div class=""><br class=""></div></div></div><div class="">And again, I'll mention the enormous world of functions and capabilities that can be gotten for free for the sake of even more well-composed code.</div><br class=""><div><blockquote type="cite" class=""><div class="">I don't want a flatMap on optionals (which, I believe, was thankfully removed in Swift 2).</div></blockquote><br class=""></div><div>And why on Earth not? The concrete implementation of it on Optional is very simple and easy to understand, and the method is incredibly useful. Personally, I use it all the time (it was not removed). And again, just because you don't like using flatMap doesn't mean Optional isn't a Monad.</div><div><br class=""></div><div>Finally, I'd like to point out that it doesn't hurt any end users not familiar with Monads if Array implements a higher kinded Monad protocol. As far as they have to be concerned, Array just has these useful map and flatMap functions. Meanwhile, those of us who wish to abstract over Monads in general can write our abstract code and implement Monad on our various types which mathematically have to be Monads. It's a layer of robustness and improvement that unconcerned end users don't have to bother themselves with.</div><div><br class=""></div><div>While I understand that the Swift team doesn't have the resources to implement everything that everyone wants, and that HKTs aren't very high on their list of priorities, it seems unreasonable to me that one could think of HKTs and Monads as somehow detrimental to the language.</div></body></html>