Strange Leaflet about Elixir - Page 1
- Author: Stephen Ball
- Published:
-
- Permalink: /blog/strange-leaflet-about-elixir-page1
Elixir is a language of independently running processes
If you like there’s a Livebook version of this post where you can actually run code and follow along. This was written to be a Livebook first, but works well enough as a post too.
If you read it as a post you can follow along an the Elixir REPL iex
> read leaflet
(Taken)
WELCOME TO ELIXIR!
ELIXIR is a language of adventure, danger, and low cunning. In it you will explore some of the most amazing territory ever seen by mortals. No computer should be without one!
> drop it
Dropped.
> look
I’ll be honest with you reader. There isn’t anything to see here except what you already see. There is no state filled game world to explore. That leaflet you just read isn’t real. There is no player-character that once held a leaflet, read it, and then dropped it.
The leaflet is this right here. What you’re reading now.
But that doesn’t mean we can’t do things! I mean, it doesn’t have to.
Let me show you what I mean. Run the following Elixir code in iex or at least in your mental Elixir parser.
0
There you see? You just made an Elixir runtime evaluate some code!
This is not code right here. Only text. But the contents are the same. (In the Livebook version of this post the above 0
is in a Livebook code cell and this following 0
is in a markdown cell.)
0
That little snippet of text is only Elixir-highlighted code. It’s not real. It’s only text. That is, it’s only data. But it’s not evaluated data. It’s just a 0
printed on your screen. That’s all it will ever do: be a 0
. We can’t add to it, ask it if it is in fact a 0
, or crash an Elixir process by dividing by it. It simply is.
There’s a peace to that. A satisfaction. There is where I am and this is where I’ll be, it says. That first zero we saw in the actual evaluating code block was much more involved with itself. It wasn’t simply text, it was CODE. A value passing through lines of code down through many layers of abstraction to hit the transistors on your fancy CPU cores and then back up again.
…In the land of motherboard, in the fires of Mount CPU, the Dark Lord Transistor forged in secret a master Ring, to control all others. And into this Ring he poured his cruelty, his malice and his will to dominate all life. One Ring to rule them all.
Or not it’s only a zero.
But a zero has its power!
1 / 0
IO.puts("I'm old Frodo")
** (ArithmeticError) bad argument in arithmetic expression: 1 / 0
:erlang./(1, 0)
+ Code
That division by zero kills an Elixir process right in its tracks. It’ll never reach that puts statement. No chance.
But let’s hold on a second.
What is Elixir?
Well I hope you’re curious to hear my answer to that question because that’s all I’m offering here.
I think if you’ve never really met Elixir you may be thinking of it as programming language. And that’s true, from a certain point of view. But it’s missing the actual story.
That’s like “What is Earth?” “A rocky planet orbiting the sun”
Yes that’s TRUE but there’s a lot of context you just… you just skipped over.
Elixir is MORE like an operating system with running processes, cpu, memory, and a /bin full of tools ready for you to use.
You can start off simple and learn some basic syntax of Elixir and kind of lump it in with every other language out there but you’re missing it. You’re simply missing it!
Let me show you something.
self()
#PID<0.105.0>
Every Elixir process has a PID. All code in Elixir runs in a process. But make no mistake: it is not an operating system process but a process running on the Erlang Virtual Machine called the BEAM.
(Assuming you’re running this in Livebook or at least following along in the Elixir REPL.)
We have a PID. Right now! A process ID. In this very Livebook. It’s listening to everything we say.
Just as our pid is sharing all of our adventures here, other pids are sharing its. They were with our pid when it evaluated the zero. They were with our pid when it divided by the zero and failed to print Bilbo’s words. They’re even with our pid as it prints its own story, right now.
Process.info(self())
|> Keyword.filter(fn {key, _value} ->
Enum.member?([:group_leader, :links, :status, :current_function], key)
end)
[
current_function: {Process, :info, 1},
status: :running,
links: [#PID<0.127.0>, #PID<0.133.0>],
group_leader: #PID<0.133.0>
]
You see, Elixir is a language where your processes are never alone. Your code is never running by itself with no friends and no allies. Your code has access to a universal force. An energy field that surrounds, penetrates, and binds our code together. It has the BEAM.
If you choose to keep reading the leaflet turn to page 2 »