{"id":27659,"date":"2019-12-23T16:22:07","date_gmt":"2019-12-23T21:22:07","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=27659"},"modified":"2019-12-23T16:22:07","modified_gmt":"2019-12-23T21:22:07","slug":"swifts-collectionofone","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/12\/23\/swifts-collectionofone\/","title":{"rendered":"Swift&rsquo;s CollectionOfOne"},"content":{"rendered":"<p><a href=\"https:\/\/swiftrocks.com\/faster-swift-array-operations-with-collectionofone.html\">Bruno Rocha<\/a>:<\/p>\n<blockquote cite=\"https:\/\/swiftrocks.com\/faster-swift-array-operations-with-collectionofone.html\"><p>The cases where using <code>CollectionOfOne<\/code> is the best approach possible is when you are forced to operate on <code>Collections<\/code>. In the Standard Library for example, <code>CollectionOfOne<\/code> is used to insert elements in specific positions of an <code>Array<\/code>:<\/p><pre>public mutating func insert(_ newElement: __owned Element, at i: Int) {\n  _checkIndex(i)\n  self.replaceSubrange(i..&#60;i, with: CollectionOfOne(newElement))\n}\n<\/pre><p>Because <code>replaceSubrange<\/code> replaces a <code>Collection<\/code> with another one, cases where the replacement is only a single element can greatly benefit from using <code>CollectionOfOne<\/code>.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Bruno Rocha: The cases where using CollectionOfOne is the best approach possible is when you are forced to operate on Collections. In the Standard Library for example, CollectionOfOne is used to insert elements in specific positions of an Array:public mutating func insert(_ newElement: __owned Element, at i: Int) { _checkIndex(i) self.replaceSubrange(i..&#60;i, with: CollectionOfOne(newElement)) } Because [&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":"2019-12-23T21:22:10Z","apple_news_api_id":"c1447193-7639-4a01-a9d7-b5b7bc3a7acd","apple_news_api_modified_at":"2019-12-23T21:22:11Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AwURxk3Y5SgGp17W3vDp6zQ","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-27659","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\/27659","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=27659"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/27659\/revisions"}],"predecessor-version":[{"id":27660,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/27659\/revisions\/27660"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=27659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=27659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=27659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}