As my first 12in23 language, here’s my initial foray into learning Elixir. These notes will be informal and more stream of consciousness than anything. I have no idea if anyone will get any benefit from reading this, but I’m sure going to have fun writing it.
I’m starting out by working my way through the Elixir introduction. Once I finish that, I’ll start on a real project.
Basic types
Interesting that string concatenation is <>
(which I’m used to meaning !=
in SQL and Excel/Google Sheets).
The naming of the div
and rem
functions makes sense. It also amuses me from an HTML/CSS perspective. (You could pull <>
into this as well.)
Being able to drop the parentheses in function calls is nice (in that it’s cleaner), but I think the legibility gain from including them is probably worth it.
Ah, I think I’ve seen this kind of function identification (name + arity) somewhere before (maybe Haskell?). It’s a little different and reminds me vaguely of man pages, but I like it — nice to be able to tell at a glance how many arguments a function takes.
The built-in help in iex
seems nice, and I’m liking the arity identification more and more as I continue to see it. Looking through the h/0
list now for a brief detour. Interesting that you can compile a file and export bytecode from the REPL. And hot dang, introspection with i
is great. I don’t know what PIDs and ports are (in context of Elixir, that is). Okay, from the processes page it looks like PIDs are related to processes which are a fundamental Elixir concept — and not the same as OS processes. Also looks this processes page is a later section of the introduction, so I’ll set it aside for now. (At this point, I’m curious as to why you can set PIDs from strings or atoms or three integers, as opposed to having the system auto-assign them. Also intrigued by ports and references — in the languages I’ve used, ports are almost always ints.)
It’s been a while since I used a language with atoms (Ruby). Intrigued by aliases, looking forward to learning more about them later.
Nice to see string interpolation works as expected. (And in another brief detour I’m now wondering about the different syntaxes across languages. I’ve seen `${...}`
in JavaScript and f"{...}"
in Python. The "#{...}"
here is interesting.) One thing I’m noticing is that there doesn’t seem to be an uninterpolated string. (Both JS and Python have those.) I wonder how much of a performance hit there is in Elixir from having all strings support interpolation. Probably not enough to matter most of the time.
Up next: anonymous functions. The fn
and end
feel a little noisy syntactically, but with syntax highlighting it’s probably fine. Interesting that invocation needs the .
before the parentheses — so variables and functions appear to be in separate namespaces. I wonder if that’s actually true, and if there are other namespaces as well. Glad to see closures. (It would be weird not to, to be honest.)
Ah, list concatenation with ++
and --
. One of the reasons I think it’s good to have broad experience with programming languages, by the way, is so your brain can fill up with multiple meanings for almost every symbol. I jest.
Interesting that a list of ASCII numbers automatically returns a charlist. And interesting that charlists are not strings. (Maybe this is the answer to the earlier comment about all strings being interpolated. I wonder if charlists are limited to ASCII.)
I’m realizing that this is going to take forever if I keep commenting on every last little thing. Attempting now to be more selective with comments.
Okay, lists and tuples. At this point I’m wondering if there are structs/records/objects/etc. Update: I did a quick look ahead and yes, there are maps and structs.
I like that the names for size
and linear
tell you if the operation is O(1) or O(n).
Basic operators
Ooh, the distinction between or
/and
/not
and ||
/&&
/!
is interesting. And here’s ===
again. Looks like it’s not quite the same as in JavaScript but is conceptually similar (stricter than ==
).
Cross-data-type sorting is interesting.
Pattern matching
Ah, I love pattern matching. (I’ve mostly seen it in Rust so far.)
Good to see tuple and list destructuring. And I like how you can match as part of the destructuring. I don’t think I’ve seen that before. (But it’s been a little while since I’ve used Rust — you could say I’m getting rusty (I am so sorry) — and I can’t remember if it has this.)
Ooh, I like the [head | tail]
matching. That’s cool.
The pin operator is new (to me) and interesting. Kind of a different way of doing immutability. And in matching, I like how it lets you use a variable without having to overwrite what’s in the variable (which would happen without pinning). I’m also thinking about how it’s the same operator for pinning version numbers in npm.
case, cond, and if
I like case
. And guards. (Guards in Elixir, I mean. I’m not making a statement about guards out in the real world.)
Oh, now that’s interesting — anonymous functions can have multiple clauses (which I think I’ve seen in Haskell) but they can also have guards. That seems like a potentially cleaner way to handle main branches of logic within a function, maybe, but I don’t know — food for thought. And interesting that it’s restricted by arity, though in Elixir it appears that functions with different arity are considered different functions entirely. (Which is quite different from variadic functions in, say, JavaScript or Python.)
cond
feels (to a degree) like a more powerful switch
. Except in this case you can change the condition (both left- and right-hand sides!) for each branch. That’s intriguing. (Actually, giving it a few seconds’ more thought, case
is much closer to a switch
than cond
is. Both of them are more flexible, though.) Playing around with cond
feels kind of like having a superpower.
Happy to see unless
. I haven’t used an unless
since Perl a long, long time ago. I know it’s just syntactic sugar but I like it.
I love that if
can return a value (as in Rust). It’s something I often find myself missing when I’m writing, say, JavaScript. Ternaries are the next best thing but not as ideal, in my opinion.
Okay, this is long enough for today. Looking forward to continuing through the tutorial soon! I’m really enjoying this.