<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kevin Gathuku</title>
    <description>The latest articles on DEV Community by Kevin Gathuku (@kevgathuku).</description>
    <link>https://dev.to/kevgathuku</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F20081%2F6125a19a-faea-4b80-a9da-a7919113ecc7.jpg</url>
      <title>DEV Community: Kevin Gathuku</title>
      <link>https://dev.to/kevgathuku</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kevgathuku"/>
    <language>en</language>
    <item>
      <title>Bootstrapping an Elm Project</title>
      <dc:creator>Kevin Gathuku</dc:creator>
      <pubDate>Wed, 29 May 2019 08:27:44 +0000</pubDate>
      <link>https://dev.to/kevgathuku/bootstrapping-an-elm-project-3b84</link>
      <guid>https://dev.to/kevgathuku/bootstrapping-an-elm-project-3b84</guid>
      <description>&lt;p&gt;Elm is a delightful language for building reliable web applications. It’s a language that gets compiled to JavaScript and so is used to build applications that run in a web browser. In this post, we’re going to explore different ways of starting an Elm project, starting with the simplest and moving on to setups with more advanced features such as hot-reload in development.&lt;/p&gt;

&lt;p&gt;Let’s get started!&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;Before we get started, please make sure to &lt;a href="https://guide.elm-lang.org/install.html"&gt;Install Elm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To confirm if you have Elm installed, you can try running the interactive &lt;code&gt;repl&lt;/code&gt; using the &lt;code&gt;elm repl&lt;/code&gt; command. If you get a prompt such as the one shown in this image, you're good to go 👍&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NvpoHLD3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kevgathuku.files.wordpress.com/2019/05/screen-shot-2019-05-29-at-10.15.12.png%3Fw%3D1024" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NvpoHLD3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kevgathuku.files.wordpress.com/2019/05/screen-shot-2019-05-29-at-10.15.12.png%3Fw%3D1024" alt="Elm repl"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Elm CLI
&lt;/h3&gt;

&lt;p&gt;This is the officially supported way of creating a new Elm project. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a new directory where your project will live&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mkdir my-awesome-elm-project&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Navigate to the newly created directory&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd my-awesome-elm-project&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run &lt;code&gt;elm init&lt;/code&gt; inside this directory and you should get a prompt like the one below:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8PFxjdMo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kevgathuku.files.wordpress.com/2019/05/screen-shot-2019-05-29-at-10.29.05.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8PFxjdMo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kevgathuku.files.wordpress.com/2019/05/screen-shot-2019-05-29-at-10.29.05.png" alt="Elm init prompt"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Press the &lt;code&gt;Enter&lt;/code&gt; key and it should create an &lt;code&gt;elm.json&lt;/code&gt; file in the current directory. A &lt;code&gt;src&lt;/code&gt; directory will also be created. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;It is a good idea to read through the &lt;a href="https://elm-lang.org/0.19.0/init"&gt;linked resource&lt;/a&gt; that talks more about starting new Elm projects&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Let's start by creating a new &lt;code&gt;Main.elm&lt;/code&gt; file in the &lt;code&gt;src&lt;/code&gt; directory. Once we've created the file, let's add some Elm code that should show us the classic "Hello World!" message once we run it.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="kt"&gt;Main&lt;/span&gt; &lt;span class="k"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Browser&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Html&lt;/span&gt; &lt;span class="k"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World!"&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;To run the code, let's run &lt;code&gt;elm reactor&lt;/code&gt; in our directory and it should start a new local server at &lt;code&gt;http://localhost:8000&lt;/code&gt;. &lt;code&gt;elm reactor&lt;/code&gt; is the simplest way to get started running Elm code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once you navigate to &lt;code&gt;http://localhost:8000&lt;/code&gt;, you should see an interface like the following. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RK7r7R3R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kevgathuku.files.wordpress.com/2019/05/screen-shot-2019-05-29-at-10.48.52.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RK7r7R3R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://kevgathuku.files.wordpress.com/2019/05/screen-shot-2019-05-29-at-10.48.52.png" alt="Elm Reactor"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the &lt;code&gt;src&lt;/code&gt; link, then &lt;code&gt;Main.elm&lt;/code&gt; and you should be greeted by our "Hello World!" message.&lt;/p&gt;

&lt;p&gt;And that's it! We've successfully created a project the &lt;code&gt;elm init&lt;/code&gt; command and run it using &lt;code&gt;elm reactor&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Easy to get started&lt;/li&gt;
&lt;li&gt;No external dependencies apart from &lt;code&gt;elm&lt;/code&gt; itself&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Manual reloading once we make changes&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Elm Make
&lt;/h3&gt;

&lt;p&gt;This is an extension of &lt;code&gt;elm reactor&lt;/code&gt; and includes the ability to compile our Elm code to static HTML. &lt;/p&gt;

&lt;p&gt;Using the same project from our previous section, we can compile the project using the command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;elm make src/Main.elm&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once we run this command, an &lt;code&gt;index.html&lt;/code&gt; file will be generated in the current working directory, and if you open it in a web browser, you should see the same "Hello World" message.&lt;/p&gt;

&lt;p&gt;I don't have lots of experience with &lt;code&gt;elm make&lt;/code&gt; so that's as far as I'll go with it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Parcel
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://parceljs.org/"&gt;Parcel&lt;/a&gt; is a "Blazing fast, zero configuration web application bundler" and is my personal favourite to get started with an Elm application quickly. It handles compiling your Elm code to JavaScript and is super easy to get started with.&lt;/p&gt;

&lt;p&gt;You can create an Elm application compiled by Parcel in a few simple steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Install Parcel&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn global add parcel-bundler&lt;/code&gt; or &lt;code&gt;npm install -g parcel-bundler&lt;/code&gt; &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Follow the instructions in the &lt;a href="https://parceljs.org/elm.html"&gt;Elm section&lt;/a&gt; of the Parcel website, which involves:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;creating an &lt;code&gt;index.html&lt;/code&gt; file with the following contents:
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;main&amp;gt;&amp;lt;/main&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./index.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;creating an &lt;code&gt;index.js&lt;/code&gt; file with the following contents:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Elm&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Main.elm&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nx"&gt;Elm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;main&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;creating a &lt;code&gt;Main.elm&lt;/code&gt; file with the following contents:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="kt"&gt;Main&lt;/span&gt; &lt;span class="k"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Browser&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Html&lt;/span&gt; &lt;span class="k"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, from Parcel!"&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To run the application, run the command:
&lt;code&gt;parcel index.html&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I love this setup so much that I've created my own starter project based on Parcel. Feel free to check it out on &lt;a href="https://github.com/kevgathuku/parcel-elm/"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Easy to get started with&lt;/li&gt;
&lt;li&gt;No manual configuration needed 💪&lt;/li&gt;
&lt;li&gt;Hot reload included out of the box&lt;/li&gt;
&lt;li&gt;Easy to get started with &lt;a href="https://guide.elm-lang.org/interop/"&gt;JavaScript Interop&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons:
&lt;/h4&gt;

&lt;p&gt;🤷‍♀️&lt;/p&gt;

&lt;h3&gt;
  
  
  Notable Mentions:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/halfzebra/create-elm-app"&gt;create-elm-app&lt;/a&gt; - Based on &lt;a href="https://github.com/facebook/create-react-app"&gt;create-react-app&lt;/a&gt; and creates a zero-config application based on Webpack&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/wking-io/elm-live"&gt;elm-live&lt;/a&gt; - A flexible dev server for Elm. Live reload included. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;This is not meant to be a comprehensive list, and any suggestions/additions are welcome in the comments section below.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;I hope these options are useful for you when starting out your next Elm project 🚀&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>elm</category>
    </item>
    <item>
      <title>Cleaner Clojure Functions with the :pre Special Form</title>
      <dc:creator>Kevin Gathuku</dc:creator>
      <pubDate>Thu, 03 Jan 2019 13:00:14 +0000</pubDate>
      <link>https://dev.to/kevgathuku/cleaner-clojure-functions-with-the-pre-special-form-5aca</link>
      <guid>https://dev.to/kevgathuku/cleaner-clojure-functions-with-the-pre-special-form-5aca</guid>
      <description>&lt;p&gt;In the course of programming, it's fairly common to face a scenario where you want to ensure that a function's arguments fulfil some condition(s), and if not, to throw an error. In this post, we're going to see how Clojure provides a handy special form that greatly simplifies this task.&lt;/p&gt;

&lt;p&gt;One such scenario that I've come across recently is trying to come up with a function that would compute the 3x + 1 problem (Collatz conjecture). The problem is as follows:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Take any positive integer n.&lt;/p&gt;

&lt;p&gt;If n is even, divide n by 2 to get n / 2.&lt;/p&gt;

&lt;p&gt;If n is odd, multiply n by 3 and add 1 to get 3n + 1.&lt;/p&gt;

&lt;p&gt;Repeat the process indefinitely.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The conjecture states that no matter which number you start with, you will always reach 1 eventually.&lt;/p&gt;

&lt;p&gt;The task at hand was to write a function that takes a number as an argument and returns the number of steps it takes for the number to eventually be 1.&lt;/p&gt;

&lt;p&gt;You'll also notice that the first condition for the number to be passed in is that it has to be a positive integer. Therefore, given any value less than 1, the function should throw an error.&lt;/p&gt;

&lt;p&gt;A typical solution in Clojure would look as follows:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;While this definitely works, I found out that Clojure provides a much more elegant way to accomplish this, with the help of the &lt;a href="https://clojure.org/reference/special_forms#_fn_name_param_condition_map_expr_2"&gt;&lt;code&gt;:pre&lt;/code&gt; special form&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This form enables specifying conditions about the arguments that should be met before the rest of the function body is evaluated. If any of the conditions is not met, an assertion error is thrown.&lt;/p&gt;

&lt;p&gt;This is how we could rewrite the function making use of the &lt;code&gt;:pre&lt;/code&gt; condition:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In this case, if the function is called with 0 or a negative number, an assertion error is automatically thrown.&lt;/p&gt;

&lt;p&gt;I think you would agree that the second version looks much nicer 🙂&lt;/p&gt;

</description>
      <category>programming</category>
      <category>clojure</category>
    </item>
  </channel>
</rss>
