Learning Elixir a Functional Language

in StemSocial4 years ago

image.png
So, I have a weird fascination with checking out new languages. I'm a Ruby developer by trade. But, that's not enough for me. So, I often find myself dabbling in other languages to see what they have to offer. Nothing against Ruby either, I love it.

My latest forays have been into Crystal (which is nearly identical to Ruby, but compiled), and Elixir. I wanted to give Elixir a shot because the syntax is kinda-sorta similar to Ruby in some ways, and it is a purely functional language which is something I have yet to really dive into.

What is Elixir?

José Valim is the creator of the Elixir programming language, a research and development project of Plataformatec. His goals were to enable higher extensibility and productivity in the Erlang VM while keeping compatibility with Erlang's ecosystem.

José Valim aimed to create a programming language for large-scale sites and apps. Being a Ruby developer, he used features of Ruby, Erlang, and Clojure to develop a high-concurrency and low-latency language. Elixir was designed to handle large data volumes. Its speed and capabilities spread Elixir in telecommunication, eCommerce, and finance industries.

Elixir was first introduced in 2012. It is a general-purpose, functional language designed for building scalable and maintainable applications.

The language complies with the bytecode seen on the Erlang VM (also known as BEAM). Its syntax is often compared to the ever popular Ruby on Rails development framework, and it arguably exceeds it, given that the productivity Elixir allows for is not counterbalanced by lagging performance, as it is with Rails. But, other than syntax, it's a pretty different world as it is a functional language.

  • Good syntax (just my opinion)
  • The API is pretty stable now and I believe the core language is pretty much complete. Because of, upgrading is fairly easy.
  • Community is friendly and inclusive.
  • Scales well and a great fit for real time web.
  • The main ecosystem (Phoenix, Ecto, etc) are excellent.
  • Great macros support.

The main downsides for me are:

  • There aren't as many third party libraries compared to PHP/Ruby, so development tends to take longer.
  • Deployment is better than before, but not as simple as PHP.
  • No static types.

My experience with the language thus far is pretty minimal. I have made a CLI number guessing game and I have played around with the Phoenix Framework a bit, which is very interesting.

Here is my simple guessing game if you want to see how Elixir looks:

  # Guess between range of numbers.
  # Tell user guess.

  def start do
    range = IO.gets("What range is your number in(ex. 1-100)? \n")
            |> String.trim
            |> String.split("-")
    low = List.first(range)
    high = List.last(range)

    guess(String.to_integer(low), String.to_integer(high))
  end

  def guess(low, high) do
    answer = IO.gets("Is your number #{mid(low, high)}? \n")

    case String.trim(answer) do
      "bigger" -> bigger(low, high)
      "smaller" -> smaller(low, high)
      "yes" -> "I'm always right!"
      _ -> IO.puts ~s(Please type "bigger", "smaller", or "yes".)
           guess(low, high)
    end
  end

  def mid(low, high) do
    div(high + low, 2)
  end

  def bigger(low, high) do
    new_low = min(high, mid(low, high) + 1)
    guess(new_low, high)
  end

  def smaller(low, high) do
    new_high = max(low, mid(low, high) - 1)
    guess(low, new_high)
  end
end

GuessingGame.start

I'm planning to keep on exploring and improving in this language. The functional approach to programming is quite different from what I'm used to with Ruby. No objects or state to deal with. Just functions and more functions.

Anyone else here have experience with Elixir or functional programming? If so, what do you find as the up and down sides of it?

Sort:  

Im thinking about starting to learn a functional language - so far I only have been programming in imperative, objective or science languages.
What do You recommend? Elixir, Scala, Haskell or OCaml? I like math.

If you REALLY like math then probably OCaml or Haskell. You could even pursue functional programming in Python which would give you access to the load of libraries that it has for data science, math, and machine learning. What language(s) do you know now?

I chose Elixir because I'm more into the web side of things. The distributed computing side of it is interesting. It's built on top of Erlang, so it shares its similar abstractions for building fault-tolerant and distributed applications and its concurrency features. And when you think of distributed applications, that doesnt have to mean multiple machines. It's even on a single machine level as we have multi-processor machines. You can squeeze more out of 1 server.

Interesting read on Elixir: https://blog.discord.com/scaling-elixir-f9b8e1e7c29b?gi=ee93428b1bf8

Thanks for answer. I'm not into web things so I will not choose Elixir.

Currently I'm learning Python and Rust and I like both of them. I've just read that Scala is based on virtual machine of Java and I hate Java, so as You said I will choose probably Haskeel or OCaml.
Probably OCaml because I'm such person that don't like to choose 'the most common' things in life, and Haskell is more popular than OCaml.

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider supporting our funding proposal, approving our witness (@stem.witness) or delegating to the @stemsocial account (for some ROI).

Please consider using the STEMsocial app app and including @stemsocial as a beneficiary to get a stronger support. 
 

Thank you!

Congratulations @nolyoi! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You received more than 300 upvotes. Your next target is to reach 400 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @hivebuzz:

HiveBuzz supports meetups of the Hive UK Community
Feedback from the September 1st Hive Power Up Day