by freedomben · 3y ago · 0 comments
I too had an ephiphany when I realized Elixir was actually a Lisp. The language went from mildly interesting to "holy crap I think this could be my favorite language." Add on some wildly productive and pragmatic frameworks like Phoenix, Nerves, and increasingly Nx, and you've got a hell of a toolkit.
My biggest criticism of Elixir has been the difficulty of writing quick scripts, but that story has improved greatly over the last couple of years. I still turn to Ruby for scripts (especially since it's often present on systems and so easy to install), but this is a good reminder that it's time to try Elixir again for that. In some ways it may be better, such as dealing with dependencies. For exmaple I try hard to avoid using any non-standard libs with Ruby scripts so I can just use the system ruby, and don't have to ship a Gemfile and/or install any gems, or mess with chruby/asdf. With Elixir that story looks even better since the deps can be part of the script itself.
Is it actually? Last time I looked at Elixir (granted, that was a while ago), the syntax for writing macros was different than the syntax for writing ordinary code, so it fails even at a simple thing like that, it doesn't seem to support homogeneous meta-programming at all.
But again, I feel like I might be wrong here and I'm happy to be proven wrong.
Even the regular syntax is sort of lispish, though, if you remove the syntactic sugar. It's all macros and datastructres.
defmodule Foo do
def bar do
"baz"
end
end
is really: defmodule Foo, do: (def bar, do: "baz")
...which is lispish if you squint the right way. defmodule(Foo, [{:do, def(:bar, [{:do, "baz"}])}])
Even aliases (like Foo) are also a kind of syntactic sugar for a specific type of atom (Foo is actually the atom :"Elixir.Foo")The ruby "story" for Gemfile dependencies for single-file scripts have gotten a BIT better since possibly last time you looked at it.
It will of course necessarily involve installing gems still though. Ideally for your use case, I feel like you'd want to install them to a local (not system-default) location ... which is ordinarily possible with bundler, but oddly the maintainers seem to think it's an anti-use-case for inline bundler? https://github.com/rubygems/bundler/issues/7131. I dunno.
Anyway, I put this here not to convince you, your decision is reasonable, but just other info for other readers.
I don't think it will be great without heroics, but a little work and I think you can get it down to < 100ms.
It's not the first tool I'd reach for, I'm still a filthy bash hacker at heart, but I could definitely see using it for the right problem.
It's also just fun to write, which is valuable in its own way.
If your point is that the potential use cases overlap too much with Ruby, I mean, fine, write Ruby. I like Elixir more. I'm not picking either language for pure performance, but rather for ergonomic purposes, so we're into kinda subjective territory.
Curious what you find to be the biggest pain point(s) here. Is it runtime containment? Library access? Etc.
$ elixir test.exs
hello, world!
$ hyperfine "elixir test.exs"
Benchmark 1: elixir test.exs
Time (mean ± σ): 471.1 ms ± 2.6 ms [User: 187.4 ms, System: 83.1 ms]
Range (min … max): 467.1 ms … 475.6 ms 10 runs
Compared to: $ ruby test.rb
hello, world!
$ hyperfine "ruby test.rb"
Benchmark 1: ruby test.rb
Time (mean ± σ): 36.3 ms ± 2.6 ms [User: 26.0 ms, System: 8.4 ms]
Range (min … max): 34.5 ms … 53.6 ms 53 runs
In practice, I've been able to get the ruby startup time to as low as 6ms - this is what I use for scripts running as keyboard macros, for example. I cannot practically use elixir for that. hyperfine "./derp"
Benchmark 1: ./derp
Time (mean ± σ): 241.0 ms ± 7.4 ms [User: 146.9 ms, System: 170.8 ms]
Range (min … max): 235.1 ms … 261.7 ms 11 runsBEAM should still be able to compile new code on the fly, for one-off scripts