JKWA

JKWA

Author of Advanced Functional Programming with Elixir

Another deleted scene from my book

There are many ways to solve equality: duck typing, injection, protocol dispatch.

Here’s why Funx uses the one it does:

funx

First Post! Switch mode

lucas-stellet

lucas-stellet

Hey @JKWA,

First of all, thanks for your book.

I bought it recently and it’s been an enjoyable journey reading it.
I read your article and ran the LiveBook, and I liked your idea so much that I decided to create my own version of your implementation.
The main difference is that I created a separate module for the Protocol and used defdelegate to route the function depending on the arity.

I would like to your opinion about it.

Thanks!

defmodule Book do 
  defstruct [:id, :title, :author]
end

defmodule DVD do
  defstruct [:id, :title, :director]
end
defprotocol Eq.Protocol do
  def eq?(a, b)
end
defmodule Eq do
  defdelegate eq?(a, b), to: Eq.Protocol

  def eq?(a, b, comparator) when is_function(comparator, 2) do
    comparator.(a, b)
  end
end
defimpl Eq.Protocol, for: Book do
  def eq?(%Book{id: id1}, %Book{id: id2}), do: id1 == id2
  def eq?(_, _), do: false
end

defimpl Eq.Protocol, for: DVD do
  def eq?(%DVD{id: id1}, %DVD{id: id2}), do: id1 == id2
  def eq?(_, _), do: false
end
hobbit_book1 = %Book{id: 1, title: "Hobbit", author: "Tolkien"}
hobbit_book2 = %Book{id: 1, title: "Hobbits", author: "Tolkien"}
title_comparator = fn a, b -> 
  a.title == b.title
end

Eq.eq?(hobbit_book1, hobbit_book2, title_comparator)

Most Liked

JKWA

JKWA

Author of Advanced Functional Programming with Elixir

Sure, here’s my take.

From a functional point of view, I think of the protocol as the abstraction itself, defining what it means to be equal. I use Eq.Utils as a convenient place to keep helper functions that work with Eq.

I had considered naming Eq as Eq.Protocol, but in the end I leaned away from that. I felt that framing treated the protocol more like an interface for modules to implement, which doesn’t quite match how I think about the abstraction.

The combination of defdelegate eq?(a, b), to: Eq.Protocol and eq?(a, b, comparator) is behaviorally equivalent to my approach, but to me it reads a bit like OOP. Nothing wrong with that, just a different frame.

But those are really just semantics, the real difference is in how we handle the third argument. You’re passing a function, while I’m passing a map with an :eq? key that holds the function. Treating the comparison logic as data makes it easier to compose later. For instance, check out the Funx monoid.

JKWA

JKWA

Author of Advanced Functional Programming with Elixir

You might find AI Tutor for Functional Programming - Funx , interesting. Feed in your example and get Claude to focus on the different eq?/3 functions. If it is attending to the usage rules, it does a pretty good job of explaining why you might choose one over the other.

Where Next?

Trending in Blog Posts Top

bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
ryanzidago
Hi all, In this article, I make the case for each test owning its setup. Usually I forbid my AI agents to use the setup callbacks; I mu...
New
zorn
As I’ve leaned into AI code generation on LocalCents, the volume I ship has climbed, and my worry shifted from any single change to the l...
New
jswanner
I wrote about an issue I had with a LiveView application, and how I solved the problem by debouncing updates server-side (within the Live...
New
abreujp
I’ve published a new article in my Elixir learning series on dev.to exploring what happens when tagged tuples aren’t enough - the try, re...
New
zorn
A recent ex_money v6 upgrade was blocked because Timex pins an old gettext. Rather than one big remove-and-rewrite PR, I used a shim: a m...
New
jola
Doing a little mini-series on distributed Elixir with some examples, starting with setting up your cluster and monitoring it. If you have...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement