{"id":10981,"date":"2025-01-26T19:52:45","date_gmt":"2025-01-26T19:52:45","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=10981"},"modified":"2025-01-26T19:52:45","modified_gmt":"2025-01-26T19:52:45","slug":"collections-in-elixir","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/collections-in-elixir\/","title":{"rendered":"Collections in Elixir"},"content":{"rendered":"<p><em>Disclaimer: I&#8217;m going through some old posts that were in draft and publishing one&#8217;s which look relatively complete in case they&#8217;re of use: This post may not be 100% complete but does give a good overview of Elixir collections.<\/em><\/p>\n<p>Lists in Elixir are implemented as linked lists which handle handle different types. <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;3, &quot;Three&quot; :three]\r\n<\/pre>\n<p>Prepending to a list is faster than appending <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlist = &#x5B;3, &quot;Three&quot; :three]\r\n&#x5B;&quot;pre&quot; | list]\r\n<\/pre>\n<p>Appending<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlist = &#x5B;3, &quot;Three&quot; :three]\r\nlist ++ &#x5B;&quot;post&quot;]\r\n<\/pre>\n<p>List concat<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;3, &quot;Three&quot; :three] ++ &#x5B;&quot;four&quot;, :4, 4]\r\n<\/pre>\n<p>List subtraction, <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;2] -- &#x5B;2.0]\r\n<\/pre>\n<p>Head and tail<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nhd &#x5B;3, &quot;Three&quot; :three]\r\ntl &#x5B;3, &quot;Three&quot; :three]\r\n<\/pre>\n<p>Pattern matching<\/p>\n<p>We can split the head an tail using Z<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;head | tail] = &#x5B;3.14, :pie, &quot;Apple&quot;]\r\n<\/pre>\n<p>The equivalent of a dictionary known as keyword lists in Elixir<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;foo: &quot;bar&quot;, hello: &quot;world&quot;]\r\n&#x5B;{:foo, &quot;bar&quot;}, {:hello, &quot;world&quot;}]\r\n<\/pre>\n<p>Keys can be atoms, keys are ordered and do not have to be unique<\/p>\n<p>Maps<\/p>\n<p>Unlike keyword lists they allows keys of any type and are unordered the syntax for a ,ap is %{}<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nmap = %{:foo =&gt; &quot;bar&quot;, &quot;hello&quot; =&gt; :world}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Disclaimer: I&#8217;m going through some old posts that were in draft and publishing one&#8217;s which look relatively complete in case they&#8217;re of use: This post may not be 100% complete but does give a good overview of Elixir collections. Lists in Elixir are implemented as linked lists which handle handle different types. &#x5B;3, &quot;Three&quot; :three] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[730],"tags":[],"class_list":["post-10981","post","type-post","status-publish","format-standard","hentry","category-elixir"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=10981"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10981\/revisions"}],"predecessor-version":[{"id":11270,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10981\/revisions\/11270"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=10981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=10981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=10981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}