<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Luiz Varela on Medium]]></title>
        <description><![CDATA[Stories by Luiz Varela on Medium]]></description>
        <link>https://medium.com/@luizvarela1?source=rss-4b8c000665ca------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*HDrXfOEUDG0Rhwj8.jpeg</url>
            <title>Stories by Luiz Varela on Medium</title>
            <link>https://medium.com/@luizvarela1?source=rss-4b8c000665ca------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 06:07:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@luizvarela1/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Pattern Matching in Elixir]]></title>
            <link>https://medium.com/codeminer-42/pattern-matching-in-elixir-6aa47399c721?source=rss-4b8c000665ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/6aa47399c721</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[erlang]]></category>
            <dc:creator><![CDATA[Luiz Varela]]></dc:creator>
            <pubDate>Wed, 06 Jan 2016 23:54:56 GMT</pubDate>
            <atom:updated>2016-01-07T03:27:17.772Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the cool things about Elixir is the pattern matching, in this post we will be able to understand about Pattern Matching and how works the pin operator “ ^”.</p><p>The pattern matching is a feature that Elixir brought from the Erlang and is a natural functional language. To make it easy see the following example:</p><pre>iex&gt; {:ok, foo} = {:ok, &quot;hello&quot;}<br>{:ok, &quot;hello&quot;}</pre><p>In this example, we are checking if the right side matches with the pattern from the left side. From the left side, we have a <a href="http://elixir-lang.org/docs/v1.0/elixir/Tuple.html">tuple</a> with an <a href="http://elixir-lang.org/docs/v1.0/elixir/Atom.html">atom</a> element and a variable called “foo”.</p><p>What Elixir does is check the type and content of the two sides, in this case, we have two tuples on the left side the first value matches the right (:ok, :ok) and the second value (foo) matches the right because we have a variable that will be filled.</p><p>The pattern matching will throw an error if the values on either side do not coincide.</p><pre>iex&gt; {a, b, c} = [:hello, &quot;world&quot;, &quot;!&quot;]<br>** (MatchError) no match of right hand side value: [:hello, &quot;world&quot;, &quot;!&quot;]</pre><p>We can use pattern matching with lists:</p><pre>iex&gt; [a, b, c] = [1, 2, 3]<br>[1, 2, 3]<br>iex&gt; a<br>1</pre><p>Or with function definitions:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b21d95ff876c64f339c77b73b015ba55/href">https://medium.com/media/b21d95ff876c64f339c77b73b015ba55/href</a></iframe><h4>Bonus: Pattern matching with recursion.</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e940a096444d813f99cae9721b4e75f2/href">https://medium.com/media/e940a096444d813f99cae9721b4e75f2/href</a></iframe><h3>Pin operator</h3><blockquote>The <a href="http://elixir-lang.org/getting-started/pattern-matching.html#the-pin-operator">pin operator</a> ^ should be used when you want to pattern match against an existing variable’s value rather than rebinding the variable…</blockquote><p>So with pin operator we treat the variable like a literal value and rebind is disabled, see example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8505de891faf443a0ce40597bbf083d6/href">https://medium.com/media/8505de891faf443a0ce40597bbf083d6/href</a></iframe><p>So, in this post, I explained a little bit about pattern matching and how to use it in Elixir. The next posts I’ll show some more complex examples.</p><ul><li><a href="http://learnyousomeerlang.com/">Learn You Some Erlang for Great Good!</a></li><li><a href="http://elixir-lang.org/getting-started/pattern-matching.html">Pattern matching - Elixir</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6aa47399c721" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codeminer-42/pattern-matching-in-elixir-6aa47399c721">Pattern Matching in Elixir</a> was originally published in <a href="https://medium.com/codeminer-42">The Miners</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Phoenix API]]></title>
            <link>https://medium.com/codeminer-42/building-a-phoenix-api-d27902a1450a?source=rss-4b8c000665ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/d27902a1450a</guid>
            <category><![CDATA[phoenix]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Luiz Varela]]></dc:creator>
            <pubDate>Mon, 04 Jan 2016 00:32:39 GMT</pubDate>
            <atom:updated>2016-01-16T14:41:57.361Z</atom:updated>
            <content:encoded><![CDATA[<p>After reading some tutorials about building APIs with Phoenix Framework, I saw that most of the posts are out of date. So, I decided to write a blog post that covers how to build a JSON API using Elixir + Phoenix from scratch.</p><p>We’ll build a simple market API called market_api with 2 models: Market and Product.</p><p>I will try to maintain the stack updated and if you are reading this and these are not the latest, let me know and I’ll update this tutorial accordingly. We are using:</p><ul><li><strong>Elixir</strong>: 1.1.1</li><li><strong>Phoenix</strong>: 1.1.2</li><li><strong>Ecto</strong>: 1.1.1</li><li><strong>Corsica</strong>: 0.4</li><li><strong>Ja_serializer</strong>: 0.6.0</li></ul><h3>Installing Phoenix</h3><p>Best instructions for installing Phoenix can be found <a href="http://www.phoenixframework.org/docs/installation">on the Phoenix website.</a></p><h3>Creating the Phoenix app</h3><p>To get started, we need to generate our application using Phoenix mix task. We do this using <em>mix phoenix.new [project_name] [command].</em></p><pre>mix phoenix.new market_api --no-brunch</pre><p>We don’t need brunch to compile assets because we have none. Then press [Yn] to install all dependencies.</p><p>Next we need to setup our database in <em>config/dev.exs</em>. You should see some configurations at the bottom of the file:</p><pre># Configure your database<br>config :market_api, MarketApi.Repo,,<br>  adapter: Ecto.Adapters.Postgres,<br>  username: &quot;postgres&quot;,<br>  password: &quot;postgres&quot;,<br>  database: &quot;pxblog_dev&quot;,<br>  hostname: &quot;localhost&quot;,<br>  pool_size: 10</pre><p>Just change the username and password, then run <em>mix ecto.create</em> to create our database. Now you can run<em> mix phoenix.server</em> and visit <a href="http://localhost:4000/">http://localhost:4000</a> and see the Phoenix welcome page.</p><h3>Creating the models</h3><p>The first thing we’ll do is utilize one of Phoenix’s generators to build our resource. We need to generate JSON resources, in Phoenix we can use the mix task <em>mix phoenix.gen.json (</em>docs<em>: </em><a href="http://hexdocs.pm/phoenix/Mix.Tasks.Phoenix.Gen.Json.html">http://hexdocs.pm/phoenix/Mix.Tasks.Phoenix.Gen.Json.html</a>)</p><p>Run:</p><pre>mix phoenix.gen.json Market markets name:string phone:string</pre><p>and:</p><pre>mix phoenix.gen.json Product products name:string barcode:string image:string price:decimal market_id:references:markets</pre><p>Inside of<em> web/models/market.ex</em>:</p><pre>has_many :products, MarketApi.Product</pre><p>and add the following code inside of <em>web/router.ex</em> with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1870867b7a64c27544b7f09758ed9eb0/href">https://medium.com/media/1870867b7a64c27544b7f09758ed9eb0/href</a></iframe><p>This will create our API routes, then we can run <em>mix ecto.migrate </em>to create the database tables.</p><h3>Seeding the database</h3><p>We need some data to test the API. Add the following lines to <em>priv/repo/seed.exs</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a03dbf1acf6a56d881a6361ab0defc66/href">https://medium.com/media/a03dbf1acf6a56d881a6361ab0defc66/href</a></iframe><p>And run mix run <em>priv/repo/seeds.exs</em> to create and insert the data into the database. Now run the server with mix phoenix.server and curl <a href="http://localhost:4000/rentals">http://localhost:4000/</a>api/markets or curl <a href="http://localhost:4000/rentals">http://localhost:4000/api/</a>products and you will see the JSON data.</p><h3>Structure our data with JSON-API protocol</h3><p>To do this, we need to use a library called <a href="https://github.com/AgilionApps/ja_serializer">ja_serializer</a>. Add the following line inside of mix.exs</p><pre>{:ja_serializer, “~&gt; 0.6.0”}</pre><p>and run mix deps.get.</p><p>add use JaSerializer.PhoenixView to the <em>web/web.exs.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2c51775585dbcde202c23515aa6265f/href">https://medium.com/media/e2c51775585dbcde202c23515aa6265f/href</a></iframe><p>Add the following lines inside the <em>web/views/market_view.ex:</em></p><pre>attributes [:id, :name, :phone]</pre><pre>has_many :products, link: “/markets/:id/products”</pre><p>and inside <em>web/views/product_view.ex:</em></p><pre>attributes [:id, :name, :barcode, :image, :price, :market_id]</pre><p>Add following to your <em>config/config.exs</em>:</p><pre>config :plug, :mimes, %{  <br>  &quot;application/vnd.api+json&quot; =&gt; [&quot;json-api&quot;]<br>}</pre><p>and run the following to recompile plug (See: <a href="http://hexdocs.pm/plug/Plug.MIME.html">http://hexdocs.pm/plug/Plug.MIME.html</a>)</p><pre>touch deps/plug/mix.exs  <br>mix deps.compile plug</pre><p>Then change your API pipeline in <em>web/router.ex</em> to:</p><pre>pipeline :api do  <br>  plug :accepts, [&quot;json-api&quot;]<br>  plug JaSerializer.ContentTypeNegotiation<br>  plug JaSerializer.Deserializer<br>end</pre><p>Now run mix phoenix.server, and call the API with the following command:</p><pre>curl -H &quot;Accept: application/vnd.api+json&quot; http://localhost:4000/api/markets</pre><p>And you should see the market’s JSON formatted to json-api. These steps are self-explained what we’ve done so far is add the attributes that will be serialized and added the relationship with the product model. (See: <a href="http://jsonapi.org/format/#fetching-relationships">http://jsonapi.org/format/#fetching-relationships</a>).</p><h3>Setting up CORS</h3><p>We’ll need to setup CORS to make your browser recognize our cross-origin resource, we will set the CORS header in our Phoenix application, we can use a library called <a href="https://github.com/whatyouhide/corsica">Corsica</a>.</p><p>Just add this line into you <em>mix.exs</em> file:</p><pre>{:corsica, &quot;~&gt; 0.4&quot;}</pre><p>And then run <em>mix deps.get</em> to install the dependencies.</p><p>Then add the following line:</p><pre>plug Corsica, origins: &quot;*&quot;</pre><p>in <em>lib/market_api/endpoint.exs, </em>just before:</p><pre>plug MarketApi.Router</pre><h3>Creating resources</h3><p>To test our POST requests we can use curl, in you terminal:</p><pre>curl -i -H &quot;Accept: application/vnd.api+json&quot; -H &#39;Content-Type:application/vnd.api+json&#39; -X POST -d &#39;{&quot;market&quot;:{&quot;name&quot;:&quot;My Market&quot;, &quot;phone&quot;:&quot;123321&quot;}}&#39; <a href="http://localhost:4000/api/markets">http://localhost:4000/api/markets</a></pre><p>And we will receive the following response:</p><pre>HTTP/1.1 201 Created<br>server: Cowboy<br>date: Sun, 03 Jan 2016 00:46:11 GMT<br>content-length: 195<br>content-type: application/json; charset=utf-8<br>cache-control: max-age=0, private, must-revalidate<br>x-request-id: 48fa2shdrgkon0k1g5m7ok25vlrpe580<br>location: /api/markets/49</pre><pre>{“jsonapi”:{“version”:”1.0&quot;},”data”:{“type”:”market”,”relationships”:{“products”:{“links”:{“related”:”/markets/49/products”}}},”id”:”49&quot;,”attributes”:{“phone”:123321,”name”:”My Market”,”id”:49}}}%</pre><h3>What’s next?</h3><p>So, this is a simple getting started, there are many parts that we left behind, like authentication and tests. I’ll try to cover this in the next posts. You can also check out <a href="https://github.com/luizvarela/market_api">my repo</a> on github.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d27902a1450a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codeminer-42/building-a-phoenix-api-d27902a1450a">Building a Phoenix API</a> was originally published in <a href="https://medium.com/codeminer-42">The Miners</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting Started with Elixir]]></title>
            <link>https://medium.com/codeminer-42/getting-started-with-elixir-ccb48d74541?source=rss-4b8c000665ca------2</link>
            <guid isPermaLink="false">https://medium.com/p/ccb48d74541</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[code]]></category>
            <dc:creator><![CDATA[Luiz Varela]]></dc:creator>
            <pubDate>Wed, 15 Jul 2015 22:39:26 GMT</pubDate>
            <atom:updated>2015-07-15T22:39:26.210Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post we will learn a little bit about Elixir basic types, strings, atoms, integers, float, booleans, lists and tuples. I will be using Elixir 1.0.2 . To install Elixir you can access the installation guide on Elixir documentation, <a href="http://elixir-lang.org/install.html">http://elixir-lang.org/install.html</a>.</p><p>We will be starting by using the REPL console that comes bundled with Elixir. So lets get started.</p><h3>Strings</h3><p>Strings in Elixir are inserted between double quotes:</p><pre>iex&gt; &quot;Hello World&quot;<br>&quot;Hello World&quot;</pre><p>We can use multi-line strings:</p><pre>iex&gt; &quot;&quot;&quot;<br>I&#39;m a multi-line<br>string<br>&quot;&quot;&quot;</pre><h3>Atoms</h3><p>Atoms are just like symbols in Ruby. They consist of a colon, followed by letters, digits, and underscores.</p><pre>:foo<br>:bar<br>:hello@world<br>:”Hello Elixir World”<br>:this_is_a_short_sentence</pre><p>In Elixir, atoms are used frequently to tag response types.</p><h3>Numbers</h3><h4>Integers</h4><p>Integers can be written in various ways, let’s see that:</p><pre><br>9876789<br>1_000_000<br>2_123 == 2123 #Like in Ruby we can use underscores to make numbers easier to read<br></pre><h4>Float Points</h4><p>Floating point numbers have a decimal point.</p><pre>1.78<br>0.56</pre><h3>Booleans</h3><p>In Elixir, everything, excepting “false” and “nil” it’s true. “false” and “nil” are both alias to the atoms with the same name, :false and :nil.</p><pre>iex&gt;:false == false<br>true</pre><pre>iex&gt;nil == :nil<br>true</pre><h3>Lists</h3><p>Lists are stored in memory as linked lists, meaning that each element in a list holds its value and points to the following element until the end of the list is reached.</p><pre>[1,2,3]</pre><pre>[:foo, :bar]</pre><pre>[:bar, 1, [2,3,4], :foo]</pre><p>Two lists can be concatenated and subtracted with ++/2 and --/2 operators.</p><pre>iex&gt; [1,2,3] ++ [4,5,6]<br>[1, 2, 3, 4, 5, 6]</pre><pre>iex&gt; [1, true, 2, false, 3, true] <strong>--</strong> [true, false]<br>[1, 2, 3, true]</pre><p>We can access the head and the tail of a list as follows:</p><pre>iex&gt; [head | tail ] = [1, 2]<br>iex&gt; head<br>1<br>iex&gt; tail<br>2</pre><h4>Note:</h4><blockquote><em>Functions in Elixir are identified by name and by number of arguments (i.e. arity). Therefore, </em>is_boolean/1<em> identifies a function named </em>is_boolean<em> that takes 1 argument.</em>is_boolean/2<em> identifies a different (nonexistent) function with the same name but different arity. — </em><a href="http://elixir-lang.org/">http://elixir-lang.org/</a></blockquote><h3>Tuples</h3><p>Tuples, on the other hand, are stored contiguously in memory.</p><pre>iex&gt; {1, 2, 3}<br>{1, 2, 3}</pre><pre>iex&gt; {:ok, &quot;hello&quot;}<br>{:ok, &quot;hello&quot;}</pre><p>We can access a Tuple element with:</p><pre>iex&gt; elem({:ok, &quot;Hello&quot;}, 1)<br>&quot;Hello&quot;</pre><p>And the tuple size with:</p><pre>iex&gt; tuple_size({:ok, &quot;Hello&quot;})<br>2</pre><h4>Note:</h4><blockquote>Tuples are used very frequently in Pattern Matching and in return values from various functions. In the next posts we will talk a little more about it</blockquote><h3>Resources</h3><ul><li><a href="http://elixir-lang.org/getting-started/basic-types.html">Basic types - Elixir</a></li><li><a href="http://learnyousomeerlang.com/starting-out-for-real">Starting Out (for real) | Learn You Some Erlang for Great Good!</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ccb48d74541" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codeminer-42/getting-started-with-elixir-ccb48d74541">Getting Started with Elixir</a> was originally published in <a href="https://medium.com/codeminer-42">The Miners</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>