Disclaimer: This is just a mere storytelling from my point of view and should not be used as an educational reference. Also, keep in mind that this is my first time diving into the world of functional programming. I also intend not go so technical in this article, rather just provide some brief overviews.
A month after graduating college, I nailed my first job as a junior back end developer. The only programming language I proficiently knew that time was PHP and maybe some Java (oh how I hate it — personal rant, please don’t mind). I was literally praying the night before my first day that the company uses PHP so I can adjust quickly. Guess what? My prayer was not answered. They are using Elixir!
What the (insert curse word that starts with ‘f’ here) is Elixir?? — (Urag, 2017)
At first, I was somehow mad since I would not be able to apply most of the things I learned in college but I gradually saw it as an opportunity to grow. I realized that I will not lose anything if I try to learn the language (but I will lose something if I won’t — which is my job) so I went and set-up my machine.
“Where should I start?” I went to Elixir’s Getting Started guide, and finished it covert-to-cover. This was the first time Elixir caught my attention. Their Getting Started guide is really noob-friendly. There were no confusing terminologies/jargons. Although the Getting Started guide is good enough to kickstart you, I still recommend looking for other references. I took this very comprehensive online course called, “The Complete Elixir and Phoenix Bootcamp”, never regretted a dime. The course covers all the fundamental parts of Elixir, to creating a CRUD app with Phoenix (web framework for Elixir) and up to Websockets. I was able to get a discount coupon from the author himself to share with you. Link would be at the end of this post. I really recommend taking up that course.
Elixir uses def-do-end for defining a code block instead of the curly braces found in Java and PHP. Finding a missing end
is sometimes easier to find than the elusive }
closing bracket. Some of you might be surprised that it looks eerily similar with the syntax of Ruby. Well, don’t be. The creator of Elixir language, José Valim, is a known Rubyist.
iex(1)> { a, b } = { "hello", "world" }{ "hello", "world" }iex(2)> a"hello"iex(3)> b"world"
Above is a demo executed in an interactive elixir shell which could be invoked by typing iex
in your terminal. This is very confusing at first since it violates some of your programming fundamentals. What are you talking about, Vince? I’m talking about the assignment operator =
. The assignment operator is called a match operator in Elixir. Let me explain by providing some snippets.
Let’s have this code for example:
<?php
$my_number = 1;1 = $my_number;
FATAL ERROR syntax error, unexpected '=' on line number 3
That’s ridiculous. Why are you trying to assign an integer to a variable? It’s not how it works!
If the same code will be executed in Elixir, this is what will happen:
iex(1)> my_number = 11iex(2)> 1 = my_number1
Woah, it worked. Well, in Elixir, you do not assign a value to a variable. You match them. Maybe I need to throw another snippet to make it clearer.
iex(3)> 2 = my_number** (MatchError) no match of right hand side value: 1
So what happened there? We have previously matched our my_number
to 1. Then on line 3, we tried to match 2 with my_number
which already has a value of 1, hence, it resulted to a MatchError
.
["hello", "world"]|> Enum.join(" ")|> String.capitalize
This just makes the code cleaner to look at; therefore, easier to debug. Without the pipe operator, this would be written as String.capitalize(Enum.join([“hello”, “world”], “ “))
. Confusing.
Phoenix framework, according to their website, is a productive web framework that does not compromise speed and maintainability. A simple Google search would show us that there are other web frameworks for Elixir. Phoenix is the most commonly used. It’s like the Rails for Ruby. It comes bundled with Ecto. Ecto is a DSL for interacting with your database. Phoenix also comes with a built-in support for websockets, enabling you to build real-time apps in a breeze. If you are a Ruby-guy and wants to start transitioning to Elixir, Phoenix is somehow similar to Rails. Some even say it’s Rails clone. By the way, I tried learning Ruby on Rails before and all I can say is that Phoenix requires a lesser learning curve. It feels like it is guiding you while coding. Just to show you a glimpse of Phoenix and Ecto, I’ll be dumping a snippet of code that shows a specific blog post.
# router.ex...get "/:post_id", PostController, :show...
# post_controller.ex...def show(conn, %{"post_id" => post_id}) dopost = BlogApp.Repo.get!(Post, post_id)render conn, "show.html", post: postend...
If you look closely at the router, you’ll see a wildcard. In Phoenix, :
in routes denote route wildcards. We can also see in the example the power of pattern matching. It can be used to destructure complex data. Instead of just getting the whole parameter map in the function definition, we already destructured it upon receiving.
I know some of you who have been using Elixir and the Phoenix framework for a long time would say that I missed the most important feature of Elixir, which is concurrency. To be honest, I haven’t explored through that yet and as I have mentioned in the disclaimer, I intend this post to be light as possible.
What I learned from this experience is to never be contented. Technology is constantly evolving and as someone who works alongside technology, we must also evolve.
As promised, here is the link to the discount coupon for Stephen Grider’s Udemy course, “The Complete Elixir and Phoenix Bootcamp”. If you can’t click the link, the promo code is: 4MORE1234
Thank you Stephen, for being such a generous lad. Cheers.
Thank you for reading. If you have any questions, you can always talk to me on Twitter @VinceUrag
Love what you read? You can always buy me a coffee. ❤
Connect with me on Github:
vinceurag (Vince Urag)_vinceurag has 11 repositories available. Follow their code on GitHub._github.com