Why would I choose Elixir as a general purpose programming language?

In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of libraries, tools and the overall ecosystem. I am not that concerned about the concurrency benefits, the BEAM VM, and associated frameworks like Phoenix, nor am interested in the benefits of Python with stuff like numpy, django etc, just the plain power and elegance of the language.

  1. As a novice programmer why would a language like Elixir be preferable to one like Python or Ruby, in terms of its ability to express and implement ideas, ie as a general purpose programming language? Even assumng that the person had gained some experience with languages like Python or Ruby, what would make Elixir a more compelling choice? I am assuming that switching to a more functional style would be a better choice than the imperative and/or object oriented approach of Python and Ruby.
    In a way i am asking whether a functional programming language is a better way to program and on settling on that paradigm, Elixir would be the one to go for.

  2. As a functional programming language, why would I choose Elixir over other functional programming languages like language like Haskell, Ocaml or LISP/Scheme? I am not that familiar with any of them but if Elixir will give me all the benefits of functional programming with good browser output, then I am happy with go with it, but again the main thing is its expressiveness,not its associated libraries.

3 Likes

Quick post while I have a minute. :slight_smile:

  1. Immutability means that the code is easy to reason about, test, and if done properly, much easier to read. You have no surprise hidden mutation of state in random calls. You have no worry about how a variable is going to change as it is called. The program is easy to follow and read.

  2. Elixir is built on the EVM/BEAM, if you want to build something for the web, network processing, or as a glue it is unparalleled, you will not have to worry about significantly optimizing your code later as you may need to in any other language. Immutable function programming means you build things up via functions to transform data as it flows through the program, wonderfully simple and expressive without tons of cruft.

  3. As an aside, when not to choose Elixir, if you want to learn functional typed programming, something like Haskell will ‘teach’ you better than almost any other language, however OCaml will be the best if you want to write to ‘get stuff done’ (that does not involve massive scale, OCaml has a GIL like Python does (though being fixed later)) at a low level as OCaml is ‘fast’. But as always, for web or scaling, anything on the EVM is pretty hard to beat.

  4. Alternatives: There is a growing language on the EVM called alpaca, it is still early and young but it is a typed functional language on the EVM, interoperates with Elixir and all.

  5. Notes: LISP/Scheme is not functional, is as about as imperative as it gets by default, and with libraries/macros it becomes anything. :wink:

20 Likes

If you really don’t care I’d actually not start with Elixir since there is a lot to learn that you just wouldn’t need (Erlang and it’s tools are mostly about concurrency).

Though if you program professionally, chances are sooner or later you will have to do something web related or distributed (even if you’ll have one server with few cores) and that’s where Elixir shines.

5 Likes

In a way i am asking whether a functional programming language is a better way to program

Not in general, no. Functional programming has its advantages as well as its disadvantages. Listing advantages of functional programming in this forum feels like preaching to the choir, so just to give you a vague idea of where functional programming is just ill-suited, consider dynamic programming algorithms or other algorithms where the most straightforward (and efficient) solution involves continuously modifying an array.

Programming paradigms and languages are tools, not religions. Try them out, gain experience, extend your toolbox, then choose the right tool for the job.

9 Likes

Unless of course you use LFE, Lisp Flavoured Erlang, which is not imperative and behaves like Erlang and Elixir. :wink:

9 Likes

You may want to skim through these topics:

1 Like

I am already committed to Elixir as I am committed to developing for the web, but I have another interest which entails writing FFI extensions from interpreters to C libraries.

I find it annoying that nearly every single cool new programming language does not interface to GUIs very well, Elixir and Erlang included. You can look up every new language and Github such as Wren or Lily and they all don’t interface to GUIs well. It seems that ever since the web came into being language developers have ignored the GUI aspect of their usage. So what I want to do is to create some kind of library which can analyze a language, and generate bindings to C based libraries.

As an example there is the VCLua which enables Lua programs to use the LCL of the Lazarus IDE

There is agreement that functional languages are better at this kind of task, ie writing parsers, compilers and code generators. OvermindDL1 says Ocaml can do that job better, but I am hoping that Elixir will be good enough. I don’t want to spread my efforts too thinly.
If Elixir although not as srictly functional like Haskell or OCaml can do the job and teach functional concepts at the same time, I will be glad to go with it. Expressiveness and correctness are more important for me than high performance.

If Javascript oriented languages like PureScript or Elm are equally good even if not performant I will be happy with them so long as they can serve my front end web needs as well as my desire to learn functional programming. I suppose that Elm and Purescript can be combined with nodejs libraries for some backend/CLI programming and not just for the browser.

3 Likes

[quote=“vonH, post:7, topic:4513”]
If Elixir although not as srictly functional like Haskell or OCaml can do the job and teach functional concepts at the same time, I will be glad to go with it.
[/quote]In my personal opinion Elixir makes it easier to dilute the functional programming aspect compared to Erlang.

Elm is easier than Haskell for becoming more familiar with functional programming but it makes certain tradeoffs that makes some people switch to PureScript for production apps - for the enhanced power which comes at the cost of a steeper learning curve. And lets not forget about BuckleScript (OCaml again).

Elm is currently not a viable option with node JS and basing backend technology on the Reactor Pattern utilizing a single thread on an OS process seems questionable to say the least, especially with the other options that are available (Bryan Hunter: What every Node.js developer needs to know about Elixir). In the browser you are forced into a single thread environment - that doesn’t mean it’s a reasonable constraint to adopt on the backend.

I understand the sentiment - but it almost seems that to adopt the right functional mindset breath is as important as depth. No language is perfect and different languages show strengths in different areas, and it often takes one language to make a certain concept “click” before you can apply that same concept in another language where the concept’s implementation isn’t immediately obvious.
.

3 Likes

I actually wouldn’t, if I wasn’t concerned about easily running things in parallel and creating systems. Elixir (and all BEAM languages) is great for building systems that may or may not be scaled out over several computers. That’s the purpose of working on the BEAM. If I was learning a language and I wasn’t concerned about either easily handling concurrency or building systems, I’d choose something else.

If I was specifically trying to do functional programming I’d go with Haskell, no questions asked. It also actually has a few fairly decent alternatives when it comes to concurrency, so it’s not entirely useless in that regard. It’s also the most sane language of the alternatives you gave. On a personal level I love Racket, so as a language (disregarding platform) I prefer that over most.

TL;DR: Working on the BEAM is the only reasonable way to handle concurrency and building big systems that may or may not have to be spread out over several nodes, so gluing things together with the BEAM is really the only reasonable way to work at the moment. That’s what these languages are for. They’re not really better in any other way.

3 Likes

Can Elixir and Erlang be programmed in pure functonal style?

What features of Elixir do I have to avoid if I am to keep it as close to the functional ideal as it is capable of?

Yes, there just is nothing preventing you from doing it all the time. This makes certain things a lot more easy (such as doing IO and working with randomly generated data). This means that Elixir has a lot less steep learning curve than e.g. Haskell, at the expense of some flexibility that Haskell’s pure abstraction layers can provide. In Elixir, functional programming is a means while in Haskell it is an (the?) end itself.

Working with state is a lot harder in a pure language, because this involves Monads, which are considered one of the harder to understand concepts, if only because they are so abstract that it is hard to write a guide about them that works for everyone. I would definitely consider it to be one of the most important tools to understand for programmers especially because they are so generalizeable, but this definitely takes a lot of time and effort.

Writing code that happens to be pure is the easiest to reason about and, at least in my opinion, usually the most idiomatic way to solve a problem. If you want to keep as closely to the ‘pure functional’ ideal as possible, just don’t perform any side-effects in your functions, except at the outermost layer(s).

About using Elixir’s concurrency model: You don’t need to work with it, nor understand it, when starting out with Elixir. At some point you will start writing code where it makes sense to split certain parts of the computation or state-keeping off to a separate process so multiple things can happen at the same time, but it is important to never forget that the main unit of code organization in Elixir is not a Process, but a Module.

So: You’ll cross that bridge when you come to it. For many simple tasks, you can use Elixir perfectly without using the concurrency model directly. The code you write will still be faster than interpreted languages (Ruby, Python, …) because Elixir compiles to bytecode, and more readable / easier to understand than when using a systems language like C, C++, Rust. Yes, e.g. Haskell might compile to even faster, native, code, but learning it comes with a steeper learning curve and the documentation is not as good/readable/extensive/accessible as Elixir’s (I think these arguments can also be made for OCaml, but I’ll refrain from doing so because I have no personal experience with it, and @OvermindDL1 might lynch me :sweat_smile:).

Switching to a functional language will most importantly give you the benefit of referential transparency which makes your code a lot easier to maintain than object-oriented or imperative languages.

4 Likes

Lol, true, I was speaking more of the common CLISP and Scheme implementations. ^.^

LFE is awesome, I highly recommend everyone to play with it. :slight_smile:

GUI’s is a hard problem, QT is near the de-facto for native interfaces now, and HTML for the web. I would not see why Elixir would have a direct interface regardless, it is design to be used on headless servers.

Also, there is ElixirScript, which is new but works, it lets you write javascript inside elixir as elixir code, which then gets sent as javascript out. :slight_smile:

Yep, Elm is a fairly pure ML language, it is not like Haskell or OCaml, it is very limited, no higher typing of any sort, it is designed to be quick safe and fast to make simple HTML javascript programs, and it works well at that.

Bucklescsript is OCaml compiling to javascript, it is utterly fantastic, entirely safe and powerful, but it is a newer layer so is still getting libraries on it. My own Bucklescript-tea library adds a near identical to elm interface for writing web programs if you like that style like I do. :slight_smile:

You can ‘do’ about anything in any language, but this is the awesome part of Elixir/Erlang/LFE/etc…, they run on the EVM/BEAM, which is unmatched by anything else. :slight_smile:

You would not even necessarily ‘need’ to use Elixir either (other than it has a host of awesome libraries that are easier to use in it, but not necessary to use in it), any EVM/BEAM language would work. :slight_smile:

The both already are. Any language that compiles straight to the EVM/BEAM must be because the EVM/BEAM must be.

Not much really, the Process Dictionary is probably the most un-functional-like thing but no one really uses that anyway…

1 Like

I personally think that a first-language choice is highly personal - because you are making the decision to learn it - so it will almost always be one that you are drawn to.

So checkout Elixir, Ruby, Python etc and learn the one that resonates with you the most. Which one ‘feels’ the nicest to you? You will probably be driven by your gut reaction (an area where Ruby and Elixir shine :-))

Why choose Elixir over other functional languages? For me the reasons are similar - it’s the one that I’m most attracted to. The fact that it offers things other functional languages might not (OTP - fault tolerance, highly concurrent, scalable) is what got me interested in learning another language to begin with.

2 Likes

To OP’s main post, I don’t use Elixir for everything. For scripts and such I use Nim since I can compile it to a binary and run it fast with minimal fuss or installation on target machines.

For just scripting, I don’t think Elixir is a good fit.

I do use Elixir for everything else, including real applications used by businesses and my pet projects.