{"id":12372,"date":"2015-09-24T09:02:54","date_gmt":"2015-09-24T13:02:54","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=12372"},"modified":"2015-10-14T16:13:02","modified_gmt":"2015-10-14T20:13:02","slug":"custom-swift-pattern-matching-operators","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/09\/24\/custom-swift-pattern-matching-operators\/","title":{"rendered":"Custom Swift Pattern Matching Operators"},"content":{"rendered":"<p><a href=\"http:\/\/oleb.net\/blog\/2015\/09\/swift-pattern-matching\/\">Ole Begemann<\/a>:<\/p>\r\n<blockquote cite=\"http:\/\/oleb.net\/blog\/2015\/09\/swift-pattern-matching\/\">\r\n<p>We know that <code>greaterThan(0)<\/code> must produce a function that takes a value and returns <code>Bool<\/code>. So in turn, <code>greaterThan<\/code> must be a function that takes another value and returns the first function.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>Swift provides <a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/Declarations.html#\/\/apple_ref\/doc\/uid\/TP40014097-CH34-ID363\">a special syntax for the definition of curried functions<\/a> that mimics how they are called.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>It&rsquo;s impossible with this solution to give the compiler any hints for exhaustiveness checking, so it will always force us to provide a default case. If you are certain that your patterns cover every possible value, it is a good idea to put a <code>fatalError()<\/code> call into the default case to document your expectation that this code path should never get hit.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>Again, note that the missing whitespace between operators and operands is significant.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>As a more practical example, suppose you want to check a string against several prefixes and suffixes.<\/p>\r\n<\/blockquote>\r\n<p>Update (2015-10-14): <a href=\"http:\/\/oleb.net\/blog\/2015\/09\/swift-ranges-and-intervals\/\">Ole Begemann<\/a>:<\/p>\r\n<blockquote cite=\"http:\/\/oleb.net\/blog\/2015\/09\/swift-ranges-and-intervals\/\">\r\n<p>Ranges and intervals in Swift serve similar purposes but have different implementations and generic constraints. Ranges are based on indexes and are used most often in the context of collections. The fact that a range can\u2019t contain the maximum value of a type can make them unsuitable for working with intervals of numbers. Intervals work with all <code>Comparable<\/code> types and don\u2019t have the maximum-value limitation.<\/p>\r\n<p>While custom operators should be used very sparingly, I\u2019d argue that in this case they significantly improve readability without harming comprehensibility \u2013 the prefix and postfix operators are so close in meaning to their binary counterparts that even readers unfamiliar with the code should have no trouble understanding them.<\/p>\r\n<\/blockquote>\r\n\r\n<p><a href=\"http:\/\/oleb.net\/blog\/2015\/09\/more-pattern-matching-examples\/\">Ole Begemann<\/a>:<\/p>\r\n<blockquote cite=\"http:\/\/oleb.net\/blog\/2015\/09\/more-pattern-matching-examples\/\">\r\n<p>This works, but doing this for every method we want to use in this way quickly becomes tedious. So let\u2019s write a generic function, <code>flip<\/code>, that moves the first argument of a curried function to the back, right before the final return value.<\/p>\r\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: We know that greaterThan(0) must produce a function that takes a value and returns Bool. So in turn, greaterThan must be a function that takes another value and returns the first function. [&#8230;] Swift provides a special syntax for the definition of curried functions that mimics how they are called. [&#8230;] It&rsquo;s impossible [&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":[46,71,901],"class_list":["post-12372","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12372","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=12372"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12372\/revisions"}],"predecessor-version":[{"id":12557,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12372\/revisions\/12557"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=12372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=12372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=12372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}