{"id":12429,"date":"2015-10-05T17:50:02","date_gmt":"2015-10-05T21:50:02","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=12429"},"modified":"2015-10-07T15:40:04","modified_gmt":"2015-10-07T19:40:04","slug":"what-the-heck-is-a-monad","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/10\/05\/what-the-heck-is-a-monad\/","title":{"rendered":"What the Heck Is a Monad"},"content":{"rendered":"<p><a href=\"http:\/\/khanlou.com\/2015\/09\/what-the-heck-is-a-monad\/\">Soroush Khanlou<\/a>:<\/p>\n<blockquote cite=\"http:\/\/khanlou.com\/2015\/09\/what-the-heck-is-a-monad\/\">\n<p>This is the first important part of a monad. You have to have a way to create one. In this case, the constructor, <code>Maybe.Something<\/code>, fills that role. In other languages, this is known as <code>unit<\/code> or the inconveniently-named function <code>return<\/code>. It&rsquo;s a function that takes one parameter, and returns a monad that wraps that parameter.<\/p>\n<p>[&#8230;]<\/p>\n<p>It&rsquo;s important that the block returns an already-wrapped monad, so that we can chain these calls. This is a big part of why monads are useful.<\/p>\n<p>[&#8230;]<\/p>\n<p>Functional programmers took a great name like <code>ifSomething<\/code> and made it totally inscrutable by calling it <code>flatMap<\/code>. (In some of the literature, it&rsquo;s also known as <code>bind<\/code>. In Haskell, aka peak inscrutability, it&rsquo;s invoked with the operator <code>&gt;&gt;=<\/code>.)<\/p>\n<p>[&#8230;]<\/p>\n<p>To build <code>map<\/code>, we wrap the result of the <code>map<\/code> block with the constructor and send that to <code>flatMap<\/code>:<\/p>\n<p>[&#8230;]<\/p>\n<p>For something to be monad, in addition to implementing bind and unit, it has to follow some special rules.<\/p>\n<\/blockquote>\n<p>Previously: <a href=\"http:\/\/mjtsai.com\/blog\/2015\/01\/13\/functor-and-monad-in-swift\/\">Functor and Monad in Swift<\/a>, <a href=\"http:\/\/mjtsai.com\/blog\/2015\/07\/22\/higher-order-functions-in-swift-2\/\">Higher Order Functions in Swift 2<\/a>.<\/p>\n<p>Update (2015-10-07): <a href=\"https:\/\/alpha.app.net\/jws\/post\/65278826\">Jeremy W. Sherman<\/a>:<\/p>\n<blockquote cite=\"https:\/\/alpha.app.net\/jws\/post\/65278826\"><p>Re: monads, I point people at <a href=\"https:\/\/tailcalled.wordpress.com\/2012\/04\/03\/translation-of-monads-are-just-monoids-in-the-category-of-endofunctors\/\">this article<\/a> when they want to tackle the hilariously compact yet accurate &ldquo;monoid in the category of endofunctors&rdquo; definition. Unpacks the jargon, but does use Haskell syntax.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Soroush Khanlou: This is the first important part of a monad. You have to have a way to create one. In this case, the constructor, Maybe.Something, fills that role. In other languages, this is known as unit or the inconveniently-named function return. It&rsquo;s a function that takes one parameter, and returns a monad that wraps [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[263,361,46,71,901],"class_list":["post-12429","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-theory","tag-haskell","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=12429"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12429\/revisions"}],"predecessor-version":[{"id":12446,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12429\/revisions\/12446"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=12429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=12429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=12429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}