<?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: vitorhead</title>
    <description>The latest articles on DEV Community by vitorhead (@vitorhead).</description>
    <link>https://dev.to/vitorhead</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%2F105857%2Fcf2acf8d-813b-4f17-b94b-2016fe3570b6.png</url>
      <title>DEV Community: vitorhead</title>
      <link>https://dev.to/vitorhead</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vitorhead"/>
    <language>en</language>
    <item>
      <title>Runic writing in Clojure</title>
      <dc:creator>vitorhead</dc:creator>
      <pubDate>Wed, 27 Mar 2019 00:56:03 +0000</pubDate>
      <link>https://dev.to/vitorhead/runic-writing-in-clojure-3op5</link>
      <guid>https://dev.to/vitorhead/runic-writing-in-clojure-3op5</guid>
      <description>&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;As a fan of norse mythology, I always wondered if there is a way that we could write in runic at the browser. I've stumbled upon many translators that would print images of your text in runes, but few that would write it, with the possibility of selecting it and copying, so I decided to give it a try and attempt to build my own. I'm no professional when it comes to old languages, but I know that there are some schools of thought about the translation of old runic alphabets (like elder futhark) to english. While some say that the runes had specific meanings, other say that each and every had a unique matching letter, thus leaving the oportunity to match 1-1 with the english alphabet.&lt;/p&gt;

&lt;p&gt;I needed two things: a programming language and a good source to help me with the runes. For the first, I choose Clojure: an amazing, data-driven functional programming language, which is based on LISP. I do not plan on entering the basics (like a clojure 101 post) here, but &lt;a href="https://www.braveclojure.com/clojure-for-the-brave-and-true/"&gt;this&lt;/a&gt; is a very good book for those who want to dive into the Clojure world. For the second one, I choose Google. The wise search bar led me to a &lt;a href="https://www.furorteutonicus.eu/germanic/runescribe/index.php"&gt;website which had a powerful tool&lt;/a&gt;: a &lt;em&gt;runescriber&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;At this point I had the two tools that I needed to craft the translator. Without further ado, let's start!&lt;/p&gt;

&lt;h2&gt;
  
  
  Hands on
&lt;/h2&gt;

&lt;p&gt;Clojure's structure is pretty neat, and there are many tools that help you manage your solution. We will be using &lt;a href="http://leiningen.org/"&gt;Leiningen&lt;/a&gt; to create, run and build our project. Make sure you have it installed before anything (there is a guide at the website on how to install in your specific O.S.). To create a project, go ahead and type the following command at the terminal: &lt;code&gt;lein new app runiclojure&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;After that, you will have a structure that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;| .gitignore
| doc
| | intro.md
  | project.clj
| README.md
  | resources
| src
| | runiclojure
  | | | core.clj
  | test
| | runiclojure
| | | core_test.clj
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is your project skeleton. You can edit the file &lt;code&gt;project.clj&lt;/code&gt; to add some details (description, repository, url and others) into your project. It is also where you add dependencies and other libraries (but we will not have to do that for now!)&lt;/p&gt;

&lt;p&gt;With the structure created and everythng, you can now open &lt;code&gt;src/core.clj&lt;/code&gt;, our main namespace and where we will be working for the rest of the post.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The idiomatic way to code and organize our project would be to create a module (separate files) for each functionality. To keep it short and simple, from now on everything will be at the same file (project.clj) but feel free to create other files in order to keep it neat and organized.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Code!
&lt;/h2&gt;

&lt;p&gt;To begin with we can define the &lt;em&gt;latin alphabet&lt;/em&gt; and the &lt;em&gt;runes&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;runes&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"ᚨᛒᚲᛞᛖᚠᚷᚺᛇᚲᛚᛗᚾᛟᛈᛩᚱᛋᛏᚢᚡᚹᛪᛃᛋᛎ"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;alphabet&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"abcdefghijklmnopqrstuvwxyz"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We need a function to look up each letter in the alphabet in a runic chart. I'm sure that there are plenty of ways of doing that (pattern matching, case and others) but the one I choose was to create a dictionary. Using clojure's built-in &lt;code&gt;zipmap&lt;/code&gt; I could create a dictionary where the keys are the latin letters and the values are the runes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;runes&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"ᚨᛒᚲᛞᛖᚠᚷᚺᛇᚲᛚᛗᚾᛟᛈᛩᚱᛋᛏᚢᚡᚹᛪᛃᛋᛎ"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;alphabet&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"abcdefghijklmnopqrstuvwxyz"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;zipmap&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;alphabet&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;runes&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;values&lt;/code&gt; in hand, it is possible to boot up &lt;code&gt;lein repl&lt;/code&gt; and start to write some translations. For instance, if I wanted to translate the letter &lt;em&gt;a&lt;/em&gt;, I could eval &lt;code&gt;(values "a")&lt;/code&gt; and the output would be &lt;em&gt;ᚨ&lt;/em&gt;. One way of getting values out of a clojure's dictionary is to call the dictionary with the key as a parameter. With that in mind, we can write a function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;translate&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But we do not want to translate single characters, we want to translate whole words, right?!&lt;/p&gt;

&lt;p&gt;Think of a word as a &lt;code&gt;string&lt;/code&gt;. A string is just a &lt;em&gt;collection of chars&lt;/em&gt; (a list of chars). So, we could bump up our expression using the famous &lt;code&gt;map&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;translate&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"the almighty bread"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this expression, the &lt;code&gt;translate&lt;/code&gt; function will be applied to every element in the string, handling back a new, translated, string. This is exactly what we want!&lt;/p&gt;

&lt;p&gt;However, the translation core is not yet done. What if we're provided with a char that is not in the &lt;code&gt;alphabet&lt;/code&gt; that we defined earlier? In Clojure, when we try to get a dictionary value using a key that doesn't exists, it returns nil. With that in mind, we could write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;translate-text&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;or&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;translate&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;%&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;%&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Clojure's &lt;code&gt;or&lt;/code&gt; evaluates from left to right and returns the first logical true value. Doing so, if the inputted text has a character that we haven't mapped, it won't try to translate, instead it will return the character itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prompt user input
&lt;/h2&gt;

&lt;p&gt;Our translator is now ready. We could easily import our namespace in a &lt;code&gt;lein repl&lt;/code&gt; and play with the expressions. But that would be too simple!&lt;/p&gt;

&lt;p&gt;Let's try to write a function which will allow the user to write at the terminal.&lt;/p&gt;

&lt;p&gt;Clojure's &lt;code&gt;read-line&lt;/code&gt; function read the next line from the stream and returns it as a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="n"&gt;user=&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;read-line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;dev&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;community!&lt;/span&gt;&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="c1"&gt;;type text&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="s"&gt;"hello dev community!"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We could easily take this string and apply our &lt;code&gt;translate&lt;/code&gt; function. However, &lt;code&gt;read-line&lt;/code&gt; just prompts the user for one input. If we want more, we will have to write our function with&lt;code&gt;repeatedly&lt;/code&gt; and &lt;code&gt;loop&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;repeatedly&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;read-line&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;first&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;repeatedly&lt;/code&gt; takes a zero-arg function (often with side-effects) and returns an infinite (or n) lazy sequence of it. &lt;code&gt;lines&lt;/code&gt; is basically a lazy collection of the &lt;code&gt;read-line&lt;/code&gt; function. Combining with the &lt;code&gt;loop&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; structure, we get the first element of that collection and the user is prompted for input. After that, we just print to the terminal in order to see if everything's alright.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight clojure"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;defn&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;-main&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"Runic translation!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;loop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;repeatedly&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;read-line&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;first&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;when&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;translate&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;clojure.string/lower-case&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;recur&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="p"&gt;))))))&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This function has a lot of side effects (sorry!). Most of it you probably saw in the last example. &lt;code&gt;validate&lt;/code&gt;'s purpose is to control the lifecycle of the translator. It just checks if the text send by the user is equal to "exit" and if it is, it breaks the loop. &lt;/p&gt;

&lt;h2&gt;
  
  
  Using the translator
&lt;/h2&gt;

&lt;p&gt;We can now run our code using &lt;code&gt;lein run&lt;/code&gt; inside the project's folder and check for translations at the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;vitorhead:~/workspace/runiclojure &lt;span class="nv"&gt;$ &lt;/span&gt;lein run
Runic translation!
hello dev community 
&lt;span class="o"&gt;(&lt;/span&gt;ᚺ ᛖ ᛗ ᛗ ᛈ   ᛞ ᛖ ᚹ   ᚲ ᛈ ᚾ ᚾ ᚡ ᛟ ᛇ ᚢ ᛋ&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Final considerations
&lt;/h2&gt;

&lt;p&gt;The complete source can be found &lt;a href="https://gist.github.com/vitorhead/03f6045d655d3bc32a93c67cdf6cece1"&gt;in this gist&lt;/a&gt;.&lt;br&gt;
&lt;a href="https://github.com/vitorhead/runic-clojure"&gt;Here&lt;/a&gt; you can check my repository (it has a live demo aswell) about this little project made in ClojureScript, which allows you to translate your runes in the browser!&lt;/p&gt;

&lt;p&gt;Coding the translator was a simple yet fun way of studying Clojure for me. This is a very basic and small program, however it is possible to see how powerful Clojure's implementations are, and I hope it has shown you how easy and neat is the code!&lt;/p&gt;

</description>
      <category>functional</category>
      <category>learning</category>
      <category>clojure</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Lambda requirements</title>
      <dc:creator>vitorhead</dc:creator>
      <pubDate>Thu, 04 Oct 2018 12:25:26 +0000</pubDate>
      <link>https://dev.to/vitorhead/lambda-requirements-38jo</link>
      <guid>https://dev.to/vitorhead/lambda-requirements-38jo</guid>
      <description>&lt;p&gt;A wave of functional programming posts, podcasts and tutorials have been emerging since the past years. People are always talking about functional languages like clojure, scala, haskell and others. Many have asked what it brings to the table: lazy evaluation, concurrency, easy to maintain (the code is very brief and concise), reactive programming and more.&lt;/p&gt;

&lt;p&gt;As the list goes on, the attention on functional programming increases and the thought of giving it a shot comes to your mind. You start to read some articles and books. The first chapter is great and you are really feeling the hang of it. Some pages after they start talking about lambda calculus, category theory and the stereotype that ‘functional programming is hard and unnatural’ increases in your head.&lt;/p&gt;

&lt;p&gt;If you came from an imperative language (OOP), looking at all those declarative code snippets from FP may frighten you. It is true that functional languages really have a lot of complex and beautiful mathematics behind it. Monoids, functors, monads, applicatives, transducers and many others may fear you at first. Do you really need all of that to start?&lt;/p&gt;

&lt;h2&gt;
  
  
  What do I need to dive into the functional paradigm?
&lt;/h2&gt;

&lt;p&gt;One of the first differences that you can spot is the &lt;em&gt;way&lt;/em&gt; of programming, which belongs under the declarative paradigm. &lt;strong&gt;Declarative programming&lt;/strong&gt; focuses on &lt;em&gt;what&lt;/em&gt; should be done rather than &lt;em&gt;how&lt;/em&gt; (the so famous imperative programming). It is made upon &lt;strong&gt;declarative sentences&lt;/strong&gt;, focusing more on creating expressions and evaluating functions rather than the way that the machine works and &lt;em&gt;how&lt;/em&gt; the task will be done. Imperative languages often focus on a procedural approach, explicitly manipulating &lt;em&gt;how&lt;/em&gt; the machine works and changing the states of a program, whereas the declarative programming express the logic of the operation (and do not focus on the implementation).&lt;/p&gt;


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


&lt;p&gt;This might be one of the most popular examples when we talk about functional programming. On the first &lt;code&gt;doubleAll&lt;/code&gt; function we tell exactly &lt;em&gt;how&lt;/em&gt; the computer should do the operation. We append the double of every number into a new array, but we also could do another operation after that (e.g. increment some counter, print it to the console). We are explicitly telling the machine &lt;em&gt;how&lt;/em&gt; to do it. On the second function we do not care about the implementation; instead, we focus on &lt;em&gt;what&lt;/em&gt; should be done, passing an anonymous function to &lt;code&gt;map&lt;/code&gt; (which applies the function to the array, creating a new one, fully handling the implementation).&lt;/p&gt;

&lt;p&gt;Secondly, I would say one should understand &lt;strong&gt;immutability&lt;/strong&gt;. This is one of the things that makes functional programming a lot different than other paradigms. It basically means that you should stop building structures and changing their properties every time something occurs in your code. Think of your structure as your school notebook; for every annotation the teacher writes to the blackboard, you put it in an empty space, one word next to another, rather than writing them one above other, stacking every word (and ending up with a very crazy and mixed page).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functions should be treated as data&lt;/strong&gt; and that means you can return functions or pass them as parameters and even compose then to achieve the desired result. As they are data, they should also be &lt;strong&gt;pure&lt;/strong&gt;; that means if you have a function named &lt;code&gt;is-number&lt;/code&gt; it should, literally, check if it is a number. Don’t try to sneak any other responsibility to it (e.g. print to the console, add some records to a database): it must do what it says. Period. If you want to check if it is a number and then commit it to a database (or any other logging material), create another function to access the database. Then, you can &lt;strong&gt;compose&lt;/strong&gt; both of then to your desired output. Sure that will produce a third, &lt;strong&gt;unpure&lt;/strong&gt; function (database is considered I/O, moreover this new function is not pure), however, now you have two isolated functions, hence improving the maintainability of your code (and debugging!).&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With those thoughts in mind you can start to adapt to functional programming. There is a lot more to come, however it can be a bit traumatic to try everything at once.&lt;/p&gt;

&lt;p&gt;It is hard to change from a OOP and imperative paradigm, but keep in mind that you should take one step at a time. Instead of going “full functional” with your stack, you can start to tackle some functional concepts in your current language and exercise the ideas in your head.&lt;/p&gt;

</description>
      <category>functional</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
