Note: This was taken from my blog over at http://www.weeklycommit.com This blog post series is going to go through the basics of for someone coming from the Ruby language. Elixir programming Why? Well from the outside, Elixir and Ruby very similar syntactically. However, that is all they really share in common. look When you start using Elixir you will realise how different the two langauges are, I mean for starters, Elixir is a functional programming language where as Ruby is object orientated (although it can be argued that Elixir is the , but that’s an argument for another time). most Objected Oriented language out of them all This series isn’t going to look at the reasons why one would want to use Elixir over Ruby, but if you want to get an idea, check out these posts: Will Phoenix Destroy Rails — Quora Elixir and Phoenix — CarbonFive Why I’m betting on Elixir — Medium Pattern Matching Alright so with that out of the way lets jump into one of my favorite features of Elixir — . pattern matching If you have never used pattern matching before, be warned, once you start, you can’t go back. At it’s very simplest form, pattern matching is a way to . destructure complex data types But what does that mean? Well with an example, let’s say you had a map (the Elixir equivalent of a Ruby Hash ): {key: "val"} %{age: 22, name: "Harry"} How would you go about getting the value of the key? :name In ruby, you might do something like this: person = {name: "harry", age: 22} person[:name] # => "harry" However, in Elixir you could do this instead: %{name: name} = %{age: 22, name: "Harry"}name # => "Harry" The reason this works is because that the operator in Elixir doesn't mean assign, but rather match. Therefore the above statement is saying: = Make match . %{name: name} %{age: 22, name: "Harry"} So how does it do this? Well the process is something like this: It see’s that both the left and right hand side are the same data type (maps) It then see’s that both maps have a key called so they match :name It then notices that the map on the left does not have a value for the key however, the map on the right does have a value. Therefore, it must mean that is the same as . :name name "Harry" Another way to think of it is like algebra. E.g. 1 + x = 1 + 2 What’s the value of X? 2 But you can do even more then that. Let’s say that with the above, we wanted to have another person and see if it was the same as the second one. In Ruby, you might do something like this: person = {name: "harry", age: 22} person2 = {name: "glen", age: 33} person[:name] == person2[:name] With Elixir, you could do this instead: %{name: name} = {age: 22, name: "harry"}%{name: ^name} = {age: 33, name: "glen"} # => Error. No match on right hand side.... What we are doing here is using the ‘pin’ operator. This is basically saying, that we matched the value of to a particular value before, so use that value instead of trying to match against a new one. name The benefits of this may not be obvious straight away. However, for me the power of pattern matching really shines when writing functions. Let’s say you have a method that takes in a hash and multiplies two numbers together, . One implementation in Ruby could be: only if they are equal def multiply_equal(n1, n2) raise "Not equal" if n1 != n2 n1 * n2end Pretty simple. However, we can do better. In Elixir, using pattern matching you could instead do something like this: def multiply_equal(x,x) do x * xend def multiply_equal(_,_) do raise "Not Equal"end # or in one line def multiply_equal(x,x), do: x * x def multiply_eqaul(_,_), do: raise "Not Equal" So what’s going on here? It might not be evident at first but one thing to note, is that in elixir, you . can define a function multiple times In the above example, we are defining two, two argument functions. The first function, will only ever be invoked if both arguments are the same, the second function, will be invoked for every other combination of two arguments. multiply_equal Since a variable can only have one value, when we define a function head with the same two arguements e.g. we are saying that this function is only ever to be invoked, if both arguments are the same. It makes no assumptions about the data types that get passed in. (x,x) E.g., these will all be invoked by the first function head multiply_equal( [1,2,3,4], [1,2,3,4] ) multiply_equal( "foo", "foo" ) multiply_equal( 'N', [78] ) (click to see why that last one matches) here The benefits of this might not be immediately clear, but trust me, as you dive more and more into Elixir and Phoenix you will come to love this more then life itself. Putting it all together To illustrate how pattern matching and recursion can be used together we are going to work through creating our own multiply function. Basically, we are going to replicate the function common in most languages. * Now, it took me longer then I would like to admit to realise that multiplication is just addition, multiple times. So how can we go about creating this using pattern matching and recursion? Well do this, we are going to define 2 functions. In elixir, if a function has the same name but different arity (i.e. number of arguements, they are considered seperate functions). So: def func(a,b) IO.puts aend Is a different function to: def func(a) IO.puts aend To that effect we are going to have two functions. which is the one that is publicly exposed and consumed by other modules, and which is called by the function. multiply/2 mulitply/3 multiply/2 So basically we need first create a function which takes two numbers which are to be multiplied and call our second function passing in an accumulator. We will use the accumulator to track the end result from the function. multiply/3 def multiply(n1,n2) do mutliply(n1,n2,0)end (We could even write this on a single line) def multiply(n1,n2), do: multiply(n1,n2,0) Now we need to actually implement the multiplier logic. When dealing with recursion in elixir it is always best to write your success or end function head first, so that the function actually has an exit point (e.g. won’t run into infinity). The way our recursive function is going to work is that we are going to add to the accumulator (which starts at 0) for every value of and subtract 1 from until we reach 0. n1 n2 n2 So basically: 10 * 10 would look like: From this, we can tell that our success or end case, is when is equal to 0. n2 Therefore, the function will look like: def multiply(n1,n2), do: multiply(n1,n2,0) defp multiply(_n1, 0, acc), do: acc (remember makes it a private method). defp So here we are saying that if is called and the value of is equal to , then don't iterate again, instead just return the current value of the accumulator. multiply/3 n2 0 This is our end result. Now we need to implement the iteration logic. Now remember we need to add the value of to and subtract the value of by 1. n1 acc n2 That looks like this: def multiply(n1,n2,acc) do new_acc = n1 + acc new_n2 = n2 - 1 #recursive call multiply(n1, new_n2, new_acc)end That last line calls the same function again with the new values of and . n2 acc However, this looks pretty messy.. let’s refactor it a bit to look more proper. def multiply(n1,n2,acc) do multiply(n1, n2 - 1, acc + n1)end We can even right this on a single line: def multiply(n1,n2,acc), do: multiply(n1, n2 - 1, acc + n1) And that’s it, we can now multiply two numbers together in just three lines of Elixir. The final implementation looks like: def multiply(n1,n2), do: multiply(n1,n2,0) defp multiply(_n1, 0, acc), do: acc defp multiply(n1,n2,acc), do: multiply(n1, n2 - 1, acc + n1) Pretty neat huh? Now obviously this won’t handle negative numbers, but that’s pretty easy to handle as well. Again, there is no need to use if statements here, instead we can use . guard_clauses Guard clauses are essentially, parts of a function head that add conditions to the passed in arguments. It’s another way of pattern matching basically. In our case, this be achieved by checking if the value of is less then 0. If it is, we simply reverse the operations on and (e.g. we add 1 to and subtract from ). n2 n2 acc n2 n1 acc This is what that head will look like: def multiply(n1,n2,acc) when n2 < 0 do multiply(n1, n2 + 1, acc - n1)end # or on one line def multiply(n1,n2,acc) when n2 < 0, do: multiply(n1, n2 + 1, acc - n1) That part is the guard clause. We are saying here that first only call this function head if the arguments can be matched to (which they can) if is less then 0. when n2 < 0 n1,n2,acc AND n2 Now because this is recursive, functions in Elixir are checked in the order that they are defined. So e.g. if we had this method below our original one, it would never get called: def multiply(n1,n2), do: multiply(n1,n2,0) # 1 defp multiply(_n1,0,acc), do: acc # 2 defp multiply(n1,n2,acc), do: multiply(n1,n2 - 1, acc + n1) #3 defp multiply(n1,n2,acc) when n2 < 0, do: multiply(n1,n2 + 1, acc - n1) #4 - Never called This is because function head will always match the arguments (assuming it passes through function head ). Therefore, all we need to do is move above e.g. #3 #2 #4 #3 def multiply(n1,n2), do: multiply(n1,n2,0) defp multiply(_n1,0,acc), do: acc defp multiply(n1,n2,acc) when n2 < 0, do: multiply(n1,n2 + 1, acc - n1) defp multiply(n1,n2,acc), do: multiply(n1,n2 - 1, acc + n1) Now we have a fully working mutliply function written from scratch, using pattern matching and recursion. Below is some sample output(note the method was defined under a module called : Recurse iex(1)> Recurse.multiply(2,-3) #=> -6iex(2)> Recurse.multiply(-2,-3) #=> 6iex(3)> Recurse.multiply(2, 3) #=> 6iex(4)> Recurse.multiply(-2, 3) #=> -6 If you have enjoyed this little guide or have any questions, let me know in the comments below. I’d be happy to help you out! is how hackers start their afternoons. We’re a part of the family. We are now and happy to opportunities. Hacker Noon @AMI accepting submissions discuss advertising &sponsorship To learn more, , , or simply, read our about page like/message us on Facebook tweet/DM @HackerNoon. If you enjoyed this story, we recommend reading our and . Until next time, don’t take the realities of the world for granted! latest tech stories trending tech stories