Yeah, the thing that has driven me to FP, and made Elixir/Erlang such a joy is that data and state are like not coupled (not saying they have to be in OOP, but god damn if I haven't had something that should only do a read do a write and fuck me too many times to count). Given the same input a function will always produce the same output, because values are constant. I've been writing Elixir for like 5-6 years now and four of them professionally, and I hope I never go back to a non FP language (save Rust or mind-boggling pay).
It's kind of mind blowing when you realize, at any point in your program, you can simply serialize out your data, inspect it. Try that with a class, good fucking luck with whatever marshaling system you have...
In Elixir/Erlang, all you (generally speaking) ever have are:
- Strings (binaries, bit-strings, charlists)
- Numbers (integer, float)
- Lists (a list of things)
- Maps (a set of keys and values)
- Structs (a map with a predetermined set of keys)
The biggest thing to keep in mind coming to a BEAM language from somewhere else is that both the language you're using (Elixir/Erlang/LFE[1]/etc) and the runtime were explicitly designed together to solve a specific category problem (distributed systems). This very unique combination provides, IMHO, the best environment you could ever want to run code in...
In most languages you start up your server in its own process. Want to debug it? You gotta restart the process and then put your debug point in. Not the case in Erlang/Elixir, here's how I start up the Phoenix framework:
iex -S mix phx.server
What that does is both (a) starts the server, (b) opens up an interactive repl for the running application. You can then interact with the running system as it is, and holy shit does it make debugging wonderful.
Something in Erlang just too slow, write a NIF (but really don't; this (NIF) is a last resort and the only way to you're likely to ever actually crash the VM). You can see a VERY simple example I have in the `wyhash_ex` directory: https://github.com/tehprofessor/shorts (please note shorts is just a toy project, and not quite complete as I got obsessed with the `gen_tcp` part).
Finally, while yes processes, are the fucking peanut butter, jam, and jelly-- you really don't end up using them directly that often in your day to day life. If they aren't making a ton of sense or you don't find yourself using them everywhere, don't sweat it-- when you need to they'll be the obvious solution (even if implementing that solution isn't obvious at first b'cuz learnings).
If you have any Elixir questions, I'm available on Twitter at the same handle as my Github links. Also, the Elixir slack is quite great as well.
> I've been writing Elixir for like 5-6 years now and four of them professionally, and I hope I never go back to a non FP language (save Rust or mind-boggling pay)
Is there such a big market for Elixir these days? I thought it was very nichey yet.
Yeah, there seems to be, generally speaking it's tilted towards more senior level folks, and projects, as Elixir isn't really on anyone's radar as an entry level language. Plus, understanding the concepts that make OTP great usually requires moderately thorough understanding of asynchronous and parallel programming.
I think it's been growing pretty steadily, IMHO, because Elixir/Erlang is such a wonderful platform for development, BEAM is extremely battle-tested at this point, and performance is quite good for how easy it all is... also, while I spoke against it, when you absolutely must, to you _can_ write something in C fairly easily (I've _never_ had a good time trying to do C-interop in Ruby or JavaScript for example).
Wow, after searching for jobs for Elixir I found that the market is not so small as I have thought (even in for my region!).
I always wanted to learn it deeply, since I appreciate functional and the concept of Actors, but never thought I would find a job for it easily... I'm changing my mind now.
Since you talked about rust in your original post, rustler allows you to write NIFs that can't crash the BEAM, never felt the need to use it, but at least it exists.
I'm totally aware of Rustler :) but it's a nice tool to share! I was mostly trying to speak to the real world environment where you have a simple routine or interface already prepackaged and you want access to it... Not that you can't use Rustler, but just being able to access the C interface directly is comforting (and sometimes easier than adding another compilation step between them even if it is _safer_ (e.g. prototyping)).
Mint (HTTP client library) - https://github.com/elixir-mint/mint
Jason (JSON library) - https://github.com/michalmuskala/jason
Tesla (Another HTTP library) - https://github.com/teamon/tesla
---
Yeah, the thing that has driven me to FP, and made Elixir/Erlang such a joy is that data and state are like not coupled (not saying they have to be in OOP, but god damn if I haven't had something that should only do a read do a write and fuck me too many times to count). Given the same input a function will always produce the same output, because values are constant. I've been writing Elixir for like 5-6 years now and four of them professionally, and I hope I never go back to a non FP language (save Rust or mind-boggling pay).
It's kind of mind blowing when you realize, at any point in your program, you can simply serialize out your data, inspect it. Try that with a class, good fucking luck with whatever marshaling system you have...
In Elixir/Erlang, all you (generally speaking) ever have are:
- Strings (binaries, bit-strings, charlists)
- Numbers (integer, float)
- Lists (a list of things)
- Maps (a set of keys and values)
- Structs (a map with a predetermined set of keys)
Recursion and pattern matching make for more readable code IMHO. Here's a fairly trivial snippet from the other day, of converting structs to maps (which Elixir doesn't do implicitly): https://gist.github.com/tehprofessor/fd8aebeb4804201a49f37d9...
The biggest thing to keep in mind coming to a BEAM language from somewhere else is that both the language you're using (Elixir/Erlang/LFE[1]/etc) and the runtime were explicitly designed together to solve a specific category problem (distributed systems). This very unique combination provides, IMHO, the best environment you could ever want to run code in...
In most languages you start up your server in its own process. Want to debug it? You gotta restart the process and then put your debug point in. Not the case in Erlang/Elixir, here's how I start up the Phoenix framework:
iex -S mix phx.server
What that does is both (a) starts the server, (b) opens up an interactive repl for the running application. You can then interact with the running system as it is, and holy shit does it make debugging wonderful.
Want a fast cache for that one operation but don't want to setup X, Y, or Z? Use ETS it's built in: https://elixir-lang.org/getting-started/mix-otp/ets.html
I can't tell you how much frustration I've saved just having reliable mechanisms for timing (thanks pre-emptive scheduling) like `Process.send_after` https://hexdocs.pm/elixir/Process.html#send_after/4
Need to make sure you're only sending messages to live nodes? `pg2` (process groups to the rescue): https://erlang.org/doc/man/pg2.html
Something in Erlang just too slow, write a NIF (but really don't; this (NIF) is a last resort and the only way to you're likely to ever actually crash the VM). You can see a VERY simple example I have in the `wyhash_ex` directory: https://github.com/tehprofessor/shorts (please note shorts is just a toy project, and not quite complete as I got obsessed with the `gen_tcp` part).
Finally, while yes processes, are the fucking peanut butter, jam, and jelly-- you really don't end up using them directly that often in your day to day life. If they aren't making a ton of sense or you don't find yourself using them everywhere, don't sweat it-- when you need to they'll be the obvious solution (even if implementing that solution isn't obvious at first b'cuz learnings).
If you have any Elixir questions, I'm available on Twitter at the same handle as my Github links. Also, the Elixir slack is quite great as well.
[1] LFE is List Flavoured Erlang
Edit: Fixing formatting. God damn I suck at this.