top | item 45231852

My first impressions of Gleam

237 points| AlexeyBrin | 6 months ago |mtlynch.io | reply

78 comments

order
[+] munchler|6 months ago|reply
> I’ve also heard that functional languages lend themselves especially well to parsing tasks, and I’ve never understood why, so it’s a good opportunity to learn.

This is mainly due to a technique called "parser combinators", which can be expressed very cleanly in functional languages. The idea is to start with a standard set of primitive general-purpose parsers and combine them into more complex parsers that are specific to the task at hand. If you have an interest in either functional programming or parsing in general, parser combinators are good to have in your toolkit.

It looks like Gleam has at least two parser combinator packages that you might be able to use:

    • party (https://hexdocs.pm/party/)
    • parser_gleam (https://hexdocs.pm/parser_gleam/)
[+] lpil|6 months ago|reply
Parser combinators are very slow in languages without a sufficiently optimising compiler, I wouldn't recommend them here.

In Gleam pattern matching and the "splitter" package is the go-to for text parsing.

[+] IshKebab|6 months ago|reply
I don't think parser combinators are any more difficult in imperative languages like Python than functional ones? I found a few for Python and C++.
[+] asplake|6 months ago|reply
Or thanks to algebraic data types and pattern matching? Even in a modest recursive descent parser, these can be really nice to use.
[+] phplovesong|6 months ago|reply
Id say mostly because of recursion is usually better for tree like ds, and builtin tco and exhaustive pattern matching. This is why ocaml is usually the goto for building a new languge prototype.
[+] jszymborski|6 months ago|reply
Gleam has long kinda seemed like my idea programming language. My only real hang-up is maybe an irrational one, but I don't love that it needs either a VM (BEAM) to run, or for it to be compiled to an interpreted language (javascript). I really wish it could target LLVM or something so it could be compiled down to native.

Maybe someone can sell me on BEAM though.

EDIT: The comments below are indeed beginning to sell me on BEAM, I'm realizing my reluctance might come from some negative experiences I've had dealing with the JVM.

[+] brightball|6 months ago|reply
The BEAM is very lightweight and necessary within Erlang to enforce one of its greatest tradeoffs:

- No universal garbage collector, every process (aka green thread) has its own heap that is reclaimed when it dies.

- No process can takeover the CPU. If you run a hugely intensive task in one process, everything else, millions of other processes potentially, will continue responding normally and consistently. The hugely intensive task will just take longer.

There’s more that applies to some advanced use cases, but these are the 2 core elements that are awesome.

[+] doawoo|6 months ago|reply
Glad you say you're coming around on the BEAM. It really is a fantastic virtual machine. Of course if you look at my comment history you'll see I'm a big fan of it and tend to push it, but that's only because it has actually made my job (and hobby) of engineering easier.

There's something elegant about how everything lives inside a process, and communication between those processes is crazy simple, you just `send(pid, value)` and boom you can not only talk across large codebases, but you can talk across _networked clusters of BEAM VMs_ with little setup.

At my current job, we're using it on embedded Linux for IoT devices, and it's really amazing to section off the code into processes that, if something like an i2c sensor or component glitches out, just let the process crash, and have it be restarted automatically by a Supervisor into a fresh well known state.

Definitely give it a look, it's nothing like the JVM ;)

[+] gmassman|6 months ago|reply
The BEAM is an amazing piece of technology. It’s built to scale massive concurrent systems and has great developer ergonomics. I’ve used it with Elixir and it’s really a breath of fresh air as far as running a webserver goes. Much more flexible and simpler to manage than a python or nodejs runtime, and also capable of scaling up with far fewer resources than you would think. Highly recommend giving it a go!
[+] Cyph0n|6 months ago|reply
I have never seriously used a BEAM language (Elixir/Erlang/Gleam), but for me, it’s the opposite: the most attractive part of Gleam is that it runs on BEAM :)

My reservations at this point are mostly around maturity of the language, the stdlib, and the library ecosystem; although I haven’t been following progress too closely.

[+] h14h|6 months ago|reply
BEAM's concurrency model makes it the ideal runtime for web servers IMO. Similarly, the ubiquity of JS in the browser makes it an excellent runtime to target as well.

I agree though that it would be amazing to compile Gleam code into universal native binaries. Would make it a fatalistic general-purpose language.

AFAIK it's entirely possible -- just more of a resourcing concern given the small team.

[+] giancarlostoro|6 months ago|reply
As others have pointed out. The BEAM is one of the most efficient VMs out there. It isnt FASTER than C or C++ but it is insanely efficient. Stuff like a simple hello world fits in small bytes in memory. The resilience of the BEAM is impeccable. Discord reaps the benefits and uses Rust when it needs to. I love backend systems and my ideal backend always winds up being similar to what BEAM provides OOTB.

My only reason for not using it is nobody pays me to use it, they pay me to write basic CRUD apps in .NET

[+] dysoco|6 months ago|reply
I thought the whole point of Gleam is having a statically typed lang running in the BEAM VM? If you want something similar outside of it you probably want Scala, Rust, OCaml, etc.
[+] tcoff91|6 months ago|reply
You need the BEAM because operating systems we have available do not have the properties of the BEAM.

OS processes are far to heavy and slow to start, and the scheduler doesn’t work the way erlang needs it to. The BEAM solves those issues amongst others.

Also you need the BEAM for hot swapping code at runtime without taking the system offline.

[+] obeavs|6 months ago|reply
You can try Moonbit. It's extraordinarily well designed and compiles to highly optimized JS, WASM or even native
[+] sodapopcan|6 months ago|reply
To add to all these other answers, what would you hope to get out of it without BEAM? Just the syntax?
[+] justusthane|6 months ago|reply
As someone who isn’t a programmer, but enjoys dabbling, and has been curious about functional languages, this was a really helpful (and fun!) read!

Tutorials written by beginners are so valuable, because once you’re more experienced with a given subject, it’s hard to remember what it was like to be a beginner. It can feel vulnerable to write them because you have to expose your ignorance, but it’s such a great thing to do.

[+] WJW|6 months ago|reply
It really is a neat language. Sort of like the smaller more elegant language that has been waiting to break out of Haskell, but combined with Erlang. Definitely not the "standard" programming language but very very nice.

The core language is pretty small too. Any moderately experienced programmer can probably burn through the language tour and be ready for a few simple AoC problems in an afternoon or so. This is due to the language designer having a very strict dedication to keeping the language as simple as possible. (Much to the chagrin of my muscle memory, as I keep expecting pattern matching on function arguments to work but it doesn't)

[+] asib|6 months ago|reply
> [...] It’s an Elixir-like language that supports static typing.

Maybe just me, but when I tried Gleam it really came off much more like Rust. In fact, other than running on the BEAM (and having some OTP libs in the works), it doesn't really _feel_ like Elixir at all to me, but that is definitely an opinion.

[+] sodapopcan|6 months ago|reply
They probably just mean "BEAM language that isn't Erlang."

All BEAM languages always bring something new to the table aside from just syntax (for Gleam it's static type, for Elixir it's macros and, well, mix!) but none of them try and abstract away the core tenants of the BEAM being functional working with modules and processes. So ya, in that sense you could say it's like Elixir.

[+] sbrother|6 months ago|reply
How good is the interop story with Elixir/Erlang currently? Can I include a few gleam modules in my Elixir application and let mix take care of compiling and linking everything properly?
[+] tengbretson|6 months ago|reply
I've kind of gotten a

Scala : Gleam :: JavaScript : Lua

vibe from it.

[+] thayne|6 months ago|reply
My biggest problem with gleam is the lack of any kind of ad-hoc polymorphism (interface, trait, typeclass, protocol, etc.).

It also seems weird to make a language for BEAM that depends on a separate library for actors and concurrency.

[+] Nezteb|6 months ago|reply
I thought so too, although treating everything as data and functions is really nice [1].

Regarding the BEAM and why Gleam has a separate library for actors: [2]

> "One notable difference between Elixir and Gleam is that Elixir gets to just re-use the OTP code as-is (with some Elixir wrappers on top for convenience). Gleam concluded that the OTP is built expecting dynamic types, and that for best results in Gleam they'd need to re-implement the key primitives. That's why the example shown is an "Actor" not a GenServer - it serves the same purpose, and might even fit in a Supervision tree, but isn't actually a GenServer."

There's a brief snippet mentioning this in the Gleam OTP readme as well: [3]

> "Actors do not yet support all OTP system messages, so some of the OTP debugging APIs may not be fully functional."

[1] https://mckayla.blog/posts/all-you-need-is-data-and-function...

[2] https://news.ycombinator.com/item?id=40645489

[3] https://github.com/gleam-lang/otp#limitations-and-known-issu...

[+] IncreasePosts|6 months ago|reply
I'm glad I'm not the only one who hoarded all their AIM log data. Whenever I want to cringe I can pull up a random file
[+] figbert|6 months ago|reply
I was tricked into trying Gleam earlier this summer, and really liked it! I anticipate that I will at least use it for all future web projects (one example: https://github.com/FIGBERT/bdab) due to my serious JavaScript allergy.

I will note something that might be of use for the work in this particular article: https://hexdocs.pm/gleam_stdlib/gleam/dynamic/decode.html

Gleam Decoders are something I haven’t fully wrapped my head around, but are supposedly very powerful and do exactly what this article is focused on (parsing input data into Gleam types) in a more(?) idiomatic way.

[+] oDot|6 months ago|reply
I moved to Gleam with all of my projects and am not going back. The language is fantastic but really the most impactful are the coding paradigms that it enables, especially the actor model and especially on the front end (The Elm Architecture)
[+] sureglymop|6 months ago|reply
So do you build stuff using Wisp and Lustre?

I would love to look at some example applications!

[+] datboi_420|6 months ago|reply
This was a great read! One thing that def makes Gleams error handling _look_ nicer, is utilizing `result.try` with the `use` keyword.
[+] librasteve|6 months ago|reply
thanks for posting this --- very interesting to read the "first impressions of FP" side of it

to be honest, I felt like shouting "please try raku" (https://raku.org) after about 4 paragraphs!

why? well raku Grammars are built in parsers...

[+] scuff3d|6 months ago|reply
I tried with Gleam, I really wanted to like it. I really like Louis Pilfold and I like his goals for the language. But something about it just didn't click for me. It's a good language, but at the end of the day I just didn't enjoy writing it.

It might be something the author talked about, but I think the language might be too small. It's one of the things I dislike about Rust too. I prefer a more batteries included approach because I can't stand having to pull in a bunch of small dependencies.

[+] jitl|6 months ago|reply
Rust is a HUGE language with a small stdlib. Gleam is a small language with a small stdlib.
[+] garbthetill|6 months ago|reply
Im on the same boat and this is coming from someone who loves the beam thanks to elixir, I think the marketing of the lang threw me of a bit. But I might give it a try this week, as im feeling a bit burnt out with elixir

earlier this week I was thinking why aren't there more languages around the actor model and completely forgot gleam exists

[+] back2dafucha|6 months ago|reply
The only languages Im interested in are future proof AI resistant languages. Since LLMs need alot of training (because they cant read language ASTs and write code correctly), a language that either isnt possible to express using fonts and character sets on the Internet, can only live in a private cloud, and is known to only verified practitioners runs on everything, and yet cant be decompiled.

You can launch a nuclear war in 5 lines of Visual Basic. I want a language AI doesnt know and cannot ever know.

[+] DetroitThrow|6 months ago|reply
>and cannot ever know

It might be resistant to human uptake in that case, too? Brett Victor I suppose has some interesting human-first or human-only physical computers.

[+] jcmontx|6 months ago|reply
You better start writing your own compilers then
[+] lordofgibbons|6 months ago|reply
Gleam has caught my eye for the past year or so, and I'd totally learn it if I didn't believe firmly that we won't be coding by hand within the next 9 months. It'll all be done by LLMs so syntax and ergonomics won't mean too much. At least as soon as LLMs learn to stop being turbo-slop generators.
[+] IshKebab|6 months ago|reply
9 months? I mean... 9 years maybe. There's absolutely no way we won't be coding by hand in 9 months, unless the only thing you do is landing pages and CRUD forms.
[+] buggy6257|6 months ago|reply
See you in 9 months then to check back.
[+] echelon|6 months ago|reply
> if I didn't believe firmly that we won't be coding by hand within the next 9 months.

LLM-assisted coding is awesome, but it feels like a self-driving style problem.

It's going to take 20 years to get there.

[+] a3w|6 months ago|reply
Doe LLMs write valid Gleam programs? Trying with ChatGPT three years ago, it did not. Workarounds, like "here is the syntax as a system prompt", put into the prompt I would not consider understanding, as Gleam idioms and patterns will certainly not all fit.
[+] 0x3f|6 months ago|reply
You should learn Gleam then.