{"id":36049,"date":"2022-06-02T17:07:40","date_gmt":"2022-06-02T21:07:40","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=36049"},"modified":"2023-02-04T10:16:40","modified_gmt":"2023-02-04T15:16:40","slug":"avoiding-swifts-enumerated","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/06\/02\/avoiding-swifts-enumerated\/","title":{"rendered":"Avoiding Swift&rsquo;s enumerated()"},"content":{"rendered":"<p><a href=\"https:\/\/nilcoalescing.com\/blog\/IterateOverItemsAndIndicesInSwiftCollections\/\">Natalia Panferova<\/a> (<a href=\"https:\/\/twitter.com\/natpanferova\/status\/1528991072277237761\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/nilcoalescing.com\/blog\/IterateOverItemsAndIndicesInSwiftCollections\/\"><p>The integer value we get with <code>enumerated()<\/code> shouldn&rsquo;t be used as index into the collection unless the collection is zero-based and integer-indexed. It&rsquo;s a counter that always starts from zero.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/AirspeedSwift\/status\/1529089893250437122\">Ben Cohen<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/AirspeedSwift\/status\/1529089893250437122\">\n<p><code>zip<\/code> is better in every way&#8230; even when you just want numbers<\/p>\n<p><code>zip(0&#8230;, ingredients)<\/code> is clearer which way around the pair is<\/p>\n<p><code>zip(1&#8230;, ingredients)<\/code> if you don&rsquo;t want to keep doing +1 on a zero base<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/nicklockwood\/status\/1529108699381374976\">Nick Lockwood<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/nicklockwood\/status\/1529108699381374976\">\n<p>Another subtlety is:<\/p>\n<pre>collection.reversed().enumerated()<\/pre>\n<p>vs<\/p>\n<pre>collection.enumerated().reversed()<\/pre>\n<\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/tgrapperon\/status\/1529086945862754304\">Thomas Grapperon<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/tgrapperon\/status\/1529086945862754304\"><p>If you&rsquo;re using swift-algorithms, you can also use <code><a href=\"https:\/\/github.com\/apple\/swift-algorithms\/blob\/main\/Guides\/Indexed.md\">.indexed()<\/a><\/code> for a slightly better interface than <code>zip<\/code>.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/10\/08\/swift-algorithms-package\/\">Swift &ldquo;Algorithms&rdquo; Package<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Natalia Panferova (tweet): The integer value we get with enumerated() shouldn&rsquo;t be used as index into the collection unless the collection is zero-based and integer-indexed. It&rsquo;s a counter that always starts from zero. Ben Cohen: zip is better in every way&#8230; even when you just want numbers zip(0&#8230;, ingredients) is clearer which way around the [&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":"2022-06-02T21:07:43Z","apple_news_api_id":"eb55187f-dc60-491f-b958-1ad4c42ec3ba","apple_news_api_modified_at":"2023-02-04T15:16:43Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/A61UYf9xgSR-5WBrUxC7Dug","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":[31,2078,30,2077,71,901],"class_list":["post-36049","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-15","tag-mac","tag-macos-12","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36049","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=36049"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36049\/revisions"}],"predecessor-version":[{"id":38385,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36049\/revisions\/38385"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=36049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=36049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=36049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}