{"id":10472,"date":"2015-01-13T11:53:28","date_gmt":"2015-01-13T16:53:28","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=10472"},"modified":"2015-01-13T11:53:28","modified_gmt":"2015-01-13T16:53:28","slug":"functor-and-monad-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/01\/13\/functor-and-monad-in-swift\/","title":{"rendered":"Functor and Monad in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/www.javiersoto.me\/post\/106875422394\">Javier Soto<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.javiersoto.me\/post\/106875422394\"><p>We&rsquo;ve seen what <code>map<\/code> can do when implemented on a container type, like <code>Optional<\/code>, <code>Array<\/code> or <code>Result<\/code>. To recap, it allows us to get a new container, where the value(s) wrapped inside are transformed according to a function. So <strong>what&rsquo;s a Functor<\/strong> you may ask? A Functor is any type that implements <code>map<\/code>. That&rsquo;s the whole story.<\/p><p>[&#8230;]<\/p><p>In the earlier example, we used the transformation function to return another <em>value<\/em>, but what if we wanted to use it to return a new <code>Result<\/code> object? Put another way, what if the transformation operation that we&rsquo;re passing to <code>map<\/code> can fail with an error as well?<\/p><p>[&#8230;]<\/p><p>And with that, we turned our <code>Result<\/code> type into a Monad! <strong>A Monad is a type of Functor. A type which, along with <code>map<\/code>, implements a <code>flatMap<\/code> function<\/strong> (<em>sometimes also known as <code>bind<\/code><\/em>) with a signature similar to the one we&rsquo;ve seen here.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Javier Soto: We&rsquo;ve seen what map can do when implemented on a container type, like Optional, Array or Result. To recap, it allows us to get a new container, where the value(s) wrapped inside are transformed according to a function. So what&rsquo;s a Functor you may ask? A Functor is any type that implements map. [&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,46,71,901],"class_list":["post-10472","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-theory","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10472","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=10472"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10472\/revisions"}],"predecessor-version":[{"id":10473,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10472\/revisions\/10473"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=10472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=10472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=10472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}