Hey there DEV.to community!
I'm not a professional functional programmer but recently I got involved in a personal project that I chose to write with Elixir. Elixir is super fast and a very fun programming language to learn and use.
Here in this article I will mention some points I liked about Elixir and the importance of learning a functional language as well.
Elixir is a functional programming language that runs on Erlang virtual machine and is influenced by Ruby. It is super fast and very capable when it comes to concurrency.
Here is a "Hello World" application written in Elixir:
defmodule App do def main do IO.puts "Hello World" end end
Looks pretty clean and easy to write and if you are familiar with Ruby you will recognize those "do" and "end" keywords.
Elixir is an easy-going language when it comes to syntax, it doesn't mean that you can write your code as you want, rather you can omit some rules that you should have obeyed in other languages perhaps. You can omit parenthesis for instance when defining or invoking a function:
defmodule App do def main do IO.puts sum 8, 12 end def sum x, y do x + y end end
And if you pay attention to the
sum function, you'll see that there is no return statement, Elixir assumes the last line of code as your return statement.
An anonymous function looks much cleaner if you need to create one:
defmodule App do def main do sum = fn x, y -> x + y end IO.puts sum.(7, 10) end end
And if you think it cannot be shorter see the code below which is equal to the anonymous function above:
defmodule App do def main do sum = &(&1 + &2) IO.puts sum.(7, 10) end end
Although it might look a bit bizarre if you are unfamiliar with this syntax, it can become your next huge coding style!
FP is making much more sound these days comparing to few years ago and that's because people are starting to realize how cool FP is.
Even if you don't use FP in your projects learning FP will make you think differently and thus improve your coding skills.
Elixir variables are immutable meaning that you cannot change their value (it is like defining a totally new variable if you try to assign a new value to a previously defined variable). The question here is if variables are immutable how can you loop since you need a counter variable for your
for loop or a boolean variable for your
Here is something strange! Elixir doesn't have any loop constructors like
while! Instead you'll be working with recursive functions to loop through your data!
Here is sample of printing from number 20 to 1 in Elixir:
defmodule App do def main do my_numbers 20 end def my_numbers n do if n > 0 do IO.puts n my_numbers n - 1 end end end
This might seem hard to implement but believe me if you get used to it, it is easier than a
for loop in some ways!
If you are wondering how list (array) looping works in Elixir here is the implementation:
defmodule App do def main do my_list = ["Say", "hi", "to", "Elixir"] loop_through_list my_list end def loop_through_list() do def loop_through_list [head | tail] do IO.puts head loop_through_list tail end end
Here is the procedure of how this works:
loop_through_list => head: "Say", tail: ["hi", "to", "Elixir"] Prints "Say" Invokes loop_through_list => ["hi", "to", "Elixir"] --- loop_through_list => head: "hi" -> tail : ["to", "Elixir"] Prints "hi" Invokes loop_through_list => ["to", "Elixir"] --- loop_through_list => head: "to" -> tail : ["Elixir"] Prints "to" Invokes loop_through_list => ["Elixir"] -------- loop_through_list => head: "Elixir" -> tail :  Prints "Elixir" Invokes loop_through_list =>  loop_through_list =>  (empty lid) Nothing to do and returns nil
Now that we know the procedure, the reality is Elixir provides some functions to loop through stuff without needing to write it over and over again if you need something rather simple:
defmodule App do def main do Enum.each(20..1, fn x -> IO.puts x end) Enum.each(["Say", "hi", "to", "Elixir"], fn item -> IO.puts item end) end end
A function can be combined with a switch-case statement in Elixir (kinda xD)! Here is an example:
defmodule App do def main do math = fn :sum, x, y -> x + y :subtract, x, y -> x - y :multiply, x, y -> x * y :divide, x, y -> x / y :remainder, x, y -> rem x, y end IO.puts math.(:sum, 10, 2) # 12 IO.puts math.(:subtract, 8, 3) # 5 IO.puts math.(:multiply, 4, 6) # 24 IO.puts math.(:divide, 18, 2) # 9 IO.puts math.(:remainder, 15, 4) # 3 end end
There is also guards! When defining a function you can use guards to ensure that your function does exactly what you want!
defmodule App do def main do IO.puts divide 8, 4 # 2 IO.puts divide 8, 0 # undefined end def divide(x, y) when y !== 0, do: x / y def divide(_, y) when y === 0, do: nil end
Here we defined two functions which in one we are checking if
y is not equal to 0 so we divide the numbers and one if
y is equal to 0 so it returns
_ means that we won't need this argument or variable later.
All the things I've mentioned here are just a brief explanation of Elixir's features. Elixir is fascinating and will make you a better programmer for sure.
Hope you enjoyed!
Tell me what you think about Elixir in the comments section below.