{"id":11777,"date":"2015-07-22T11:26:02","date_gmt":"2015-07-22T15:26:02","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=11777"},"modified":"2015-10-03T15:48:00","modified_gmt":"2015-10-03T19:48:00","slug":"higher-order-functions-in-swift-2","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/07\/22\/higher-order-functions-in-swift-2\/","title":{"rendered":"Higher Order Functions in Swift 2"},"content":{"rendered":"<p><a href=\"http:\/\/sketchytech.blogspot.com\/2015\/06\/similarly-different-join-reduce-and.html\">Anthony Levings<\/a>:<\/p>\r\n<blockquote cite=\"http:\/\/sketchytech.blogspot.com\/2015\/06\/similarly-different-join-reduce-and.html\">\r\n<p>Where you have a nested array that simply needs joining with an additional array of values placed between each use <code>join()<\/code>. If you have an array that needs to be flattened and needs additional values appended as a suffix or prefix to each nested array use <code>flatMap()<\/code>. If you require a one off initial value and possibly to append values before or after nested values use <code>reduce()<\/code>.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>The focus of this post has been arrays, but actually there are things that can be done with dictionaries as well.<\/p>\r\n<\/blockquote>\r\n<p>Update (2015-07-27): <a href=\"http:\/\/natashatherobot.com\/swift-2-flatmap\/\">Natasha Murashev<\/a>:<\/p>\r\n<blockquote cite=\"http:\/\/natashatherobot.com\/swift-2-flatmap\/\">\r\n<p>In other words, <code>flatMap<\/code> is specifically overloaded to deal with Optionals. It will take an array of optionals and return an array of unwrapped optionals without any nils.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>The idea of thinking of <code>flatMap<\/code> as dealing with containers vs just arrays makes things a lot clearer!<\/p>\r\n<\/blockquote>\r\n<p>Update (2015-09-25): <a href=\"http:\/\/www.mokacoding.com\/blog\/demistifying-swift-functor\/\">Giovanni Lodi<\/a>:<\/p>\r\n<blockquote cite=\"http:\/\/www.mokacoding.com\/blog\/demistifying-swift-functor\/\">\r\n<p>A good use case for <code>map<\/code> and <code>flatMap<\/code> in the context of <code>Optional<\/code> is to simplify code using <code>if let<\/code>s.<\/p>\r\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Anthony Levings: Where you have a nested array that simply needs joining with an additional array of values placed between each use join(). If you have an array that needs to be flattened and needs additional values appended as a suffix or prefix to each nested array use flatMap(). If you require a one off [&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-11777","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\/11777","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=11777"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11777\/revisions"}],"predecessor-version":[{"id":12406,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11777\/revisions\/12406"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=11777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=11777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=11777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}