DEV Community

bbronek
bbronek

Posted on • Updated on

Beginning my journey with elixir Part I - problem with mix and 5 most interesting things I learned so far.

Currently, I am a student of computer science and I am dedicated to programming, I tried different technologies such as node, python but elixir excites me the most and with this technology I bind my professional future. In this series, I would like to share my adventure, successes, and problems I encountered. Hope you will enjoy this series πŸ˜€

Alt Text

I started from learning basics from Elixir school and Elixir webpage. Everything was going well with learning until I got an error when I had to use mix. I installed elixir with instructions. I am using Linux Mint and the error I got when trying to create a new app was:

╰─❯ mix new app
** (Mix) Application name must start with a letter and have only lowercase letters, numbers, and underscore, got: "app". The application name is inferred from the path, if you'd like to explicitly name the application then use the "--app APP" option.
Enter fullscreen mode Exit fullscreen mode

I couldn't find an answer on StackOverflow so I decided to ask on the elixir channel on discord and good people advised me to install elixir via asdf. It is an extendable version manager that can be used to install elixir properly link.

I removed elixir and erlang then installed the latest versions of them using asdf:

1) asdf plugin add erlang && asdf plugin add the elixir
2) asdf install erlang latest
3) asdf global erlang <version>
4) asdf install elixir latest
5) asdf global elixir <version>
Enter fullscreen mode Exit fullscreen mode

Thankfully, I can now enjoy the functionality of the mix.

I will now move on to the most interesting things I learned.

1.Pin operator it makes assignment when the left side if the match includes variables. Example:

iex> x = 42
42
iex> ^x = 44
** (MatchError) no match of right-hand side value: 44
Enter fullscreen mode Exit fullscreen mode

As you can see when I tried to match x with 44 I got an error above.

2.Cond is used to match conditions similar to if-else or else in other languages. Example:

iex> cond do
...>   "apple" == "banana" -> "Incorrect"
...>   true -> "Catched all"
...> end
"Catched all"
Enter fullscreen mode Exit fullscreen mode

3.Pipe operator it takes the result of one function and passes it on as the first parameter of another expressions. Example:

iex> "HEllo WoRld" |> String.downcase() |> String.split()
["hello", "world"]
Enter fullscreen mode Exit fullscreen mode

4.Documentation of Modules and functions Example:

defmodule Calculator do
  @moduledoc """
Provides a function `add/2` to add two numbers and print them out
  """

  def add(x,y) do
   IO.puts x+y
  end
end

Enter fullscreen mode Exit fullscreen mode

You can access Calculator documentation by using the h helper function:

iex>c("file.ex") 
[Module]
...> Calculator.function(5,4)
9
:ok

iex> h Calculator

                Calculator

Provides a function add/2 to add two numbers
Enter fullscreen mode Exit fullscreen mode

5.Start new project using mix Example:

$ mix new app

Enter fullscreen mode Exit fullscreen mode

Output:

* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/app.ex
* creating test
* creating test/test_helper.exselse
* creating test/app_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:

    cd app
    mix test

Run "mix help" for more commands.

Enter fullscreen mode Exit fullscreen mode

This is all that I wanted you to present in Part I. Have a nice day and stay tuned for the next update. Bye πŸ‘‹.

Top comments (2)

Collapse
 
abhi0495 profile image
Abhi0495

Good one is there any demand for elixir ?

Collapse
 
bbronek profile image
bbronek

There is always a demand for technology even COBOL.