Elixir Blog Posts

Elixir code style, a brief example

The other day I wrote

  def build_string(a, b, ops, sep) do
    Enum.reduce(ops, "", fn op, acc ->
      acc <> sep <> Integer.to_string(calculate(a, b, op))
    end)
    |> String.trim_leading(sep)
  end

For a function, and was annoyed by how ugly it was. Improved version is

  def build_string(a, b, ops, sep) do
    ops
    |> Enum.map(&calculate(a, b, &1))
    |> Enum.join(sep)
  end

The real point of the blog post is to avoid introducing cases like the first function, which requires a call to String.trim_leading.

Also can use Enum.map_join/3 like:

def build_string(a, b, ops, sep) do
  Enum.map_join(ops, sep, &calculate(a, b, &1))
end

Good to see blog posts like this too!

3 Likes

Elixir 1.14.0 added Kernel.dbg/2 to improve developer experience. I spent some time playing around with dbg as a replacement for IO.inspect. Here is a post with some examples of debugging a Phoenix app.

Learn how dbg can replace IO.inspect to enhance your debugging workflow.

3 Likes

Elixir is gaining in popularity and as a result many SaaS companies are now using Elixir and Phoenix as the basis of their technology stack. Here are some of the most interesting examples.

8 SaaS Using Elixir and Phoenix in Production

A quick dive into ExUnit’s @tag-attributes and how to use them for clean and explicit test configuration

3 Likes

Many consider Elixir to be Ruby’s successor, so we decided to settle that. Together with CTO, MichaƂ Buszkiewicz and experienced Elixir & React Developer, Krzysztof Janiec, we decided to compare the two programming languages.
Elixir vs Ruby in Depth Comparison

I wrote a blog post about how we have been using Ecto Queries for search / filter forms in web applications. It aims at the same problem as the ransack gem in the rails world, but with less magic.

9 Likes

Thanks for sharing! Great post and I love the accompanying Livebook as a means of extending the content without adding too much bloat to the post itself. That’s a great pattern for future content like that.

1 Like

I took a deep-dive into Upserts with Ecto and investigated odd behaviors like:

  • Why does Ecto return a new binary_id every time I upsert a schema?
  • Why do upserts introduce gaps in the IDs of schemas?

This and more is all answered here now:

4 Likes

haha two butts and kills people, anyways great content!

1 Like

And another one. This time I explore using placeholders in Repo.insert_all/3. If you ever used insert_all/3 you probably sent waaaaaaaay more data than you had to. I explain how to never ever do this terrible mistake again, here:

7 Likes

I wrote a blog post for creating a simple infinitescroll page using LiveView, Intersection Observer API and JS interop using phx-hook.

Here’s the working/canonical link for building simple infinite scroll using LiveView (and in less than 30 lines of JS).

I wrote a blog post on how we used the Observer to debug some weird memory spikes in our Elixir gateway app. It includes some pretty interesting learnings about how SSL certs are handled in processes. It was a fun debug adventure :smiley:

6 Likes

Wrote a blog post on SQL injection in Phoenix. Covers how Ecto really discourages you from writing insecure code, what a vulnerable function looks like, and using Sobelow to detect this.

Detecting SQL Injection in Phoenix with Sobelow

1 Like

In this post, I will introduce you to the world of cybersecurity and tell you how Elixir programming language works in this environment.
Elixir vs Cybersecurity - is Elixir Language a Safe Technology?

2 Likes

Today’s blog post is about exclusion constraints in Postgres and how to prevent double-bookings of appointments with them. It’s a nice dive into how to execute custom SQL code in your migrations and how timestamp ranges work in Posgres.

You can use this if you want to prevent any double-bookings of e.g. appointments, schedules, meetings, etc.

3 Likes

New post describing how to implement a scrolling data table without running out of memory in Phoenix LiveView

Is there not a limit to how many items the browser is happy to render? If you throw 1,000,000 items into your page, what happens?

There is probably a practical limit to how many items you can shove into the DOM of a page, but the focus here was to minimize the per-connection server memory utilization.

In situations where millions of items should be rendered, it might be worth using a custom hook or some other client-side code to virtualize or GC the list.

I actually think fly.io uses phx-update="append" in their container logs page, which is much higher volume than anything I’ve ever shipped. As far as I can tell, they don’t do anything to clean up the entries. Worst-case, the user can always refresh the page or a filtering UI can be added.

Don’t mind me, I have a personal vendetta against “infinite scroll”. I have a somewhat lacklustre implementation of truly infinite scrolling so I always read posts about it with interest. Sadly, it always turns out to be finite scroll. It should be called “load a bit more until the browser collapses scroll” or “generally sufficient amount of items scroll”.

AFAIK if you want actual infinite scroll, you need virtual scrolling.

we knew that browsers start struggling with more than 20k log lines

2 Likes