Comfortable learning curve for learning Elixir — part 1
Check out continuation.
As stated in this tweet coding manuals can have huge gaps in complexity from “Hello World!” to database server in next chapter :)
I want to try to give below very gradual learning curve, which will help you learn basics of Elixir in more relaxed way.
Basics — learn syntax and basic control structures
This exercises will help you learn recursion and how to iterate over data structures. I strongly recommend starting to develop using ExUnit
from very beginning, so that you’ll get taste how unit testing works in Elixir early on.
- create empty project and write module with one function and tests for it. Function should take array
[1,20,13,...]
of integers and return array of values multiplied by 2[2,40,26,...]
.
Try to write one version usingEnum.map
, one manually doing recursion and one usingEnum.reduce
. - create another module with function that can take nested list
[1,2,[5,6],[7],...]
and multiply it by 2 keeping structure in place — so you will get[2,4,[10,12],[14],...]
in the end. Try convert each version of previous functions to this one and see which one will be more comfortable working with. - extend previous function to be able to work on maps too. So
%{
key1: %{
key2: 1
},
key3: [2,3,4],
key4: 10
}
will become
%{
key1: %{
key2: 2
},
key3: [4,6,8],
key4: 20
}
Basic — learn working with strings
String in Elxir are actually binaries. Try writing following exercises one time using binary stings and one time using charlists. While binaries work much faster and take less memory, charlists are easier to work with. So try them both.
- write palindrome checker (and do not forget unit tests :). Checker is a module with one exported public function which takes Elixir String as input and returns
true
orfalse
. Start with simple palindrome word “girafarig” (it’s a name of cute Pokemon :). Think how to implement it using recursion and also usingEnum
module functions.
- change your palindrome checker function to be able to ignore character case and space character — use example palindrome “Never odd or even”. Also write tests for this case.
- extend checker function once again to be able ignore all punctuation as well. It should be able to validate that “A man, a plan, a canal, Panama!”
This should give you basic understanding how recursion works in Elixir and introduce to functions map
reduce
filter
in Enum
module and may be foldr
foldl
in Lists
module.
About The Author
I’m Gaspar Chilingarov , I facilitate DevOps transition, help moving legacy applications to cloud and write high-performance Elixir apps. You can connect with me on Twitter, Facebook, LinkedIn and GitHub.
Found this post useful? Kindly tap the ❤ button below! :) Let’s spread word about Elixir.