{"id":18573,"date":"2017-08-08T14:04:35","date_gmt":"2017-08-08T18:04:35","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=18573"},"modified":"2017-08-08T14:04:35","modified_gmt":"2017-08-08T18:04:35","slug":"lazy-permutations-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/08\/08\/lazy-permutations-in-swift\/","title":{"rendered":"Lazy Permutations in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/www.figure.ink\/blog\/2017\/7\/30\/lazy-permutations-in-swift\">Joshua Emmons<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.figure.ink\/blog\/2017\/7\/30\/lazy-permutations-in-swift\">\n<p>Why is our generic type <code>X<\/code> an iterator but we force <code>Y<\/code> to conform to <code>Collection<\/code>? If you look at the nested loop in our naive example above, you&rsquo;ll see that while we iterate over <code>xs<\/code> only once, we actually loop over <code>ys<\/code> a number of times (an <code>xs.count<\/code> number of times, to be precise).<\/p>\n<p><code>IteratorProtocol<\/code> allows us to iterate over a set exactly once, so it&rsquo;s perfect for our <code>X<\/code> type. But only <code>Collection<\/code> guarantees us the ability to non-destructively traverse a sequence over and over. So <code>Y<\/code> must be a little more constrained.<\/p>\n<p>[&#8230;]<\/p>\n<p>And so, finally, we can lazily iterate over every ordered pair of our collections &mdash; all while only storing the collections themselves, not their product.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Joshua Emmons: Why is our generic type X an iterator but we force Y to conform to Collection? If you look at the nested loop in our naive example above, you&rsquo;ll see that while we iterate over xs only once, we actually loop over ys a number of times (an xs.count number of times, to [&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":[71,901],"class_list":["post-18573","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18573","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=18573"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18573\/revisions"}],"predecessor-version":[{"id":18574,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18573\/revisions\/18574"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=18573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=18573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=18573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}