JKWA

JKWA

Author of Advanced Functional Programming with Elixir

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

Here’s why Funx uses the one it does:

funx

Showing Posts 1 to 3

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)
JKWA

JKWA OP

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 OP

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.

— All posts loaded —

Where Next? Top

Trending in Blog Posts Top

mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
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
pckrishnadas88
A while back, I had to process millions of database updates in a legacy system that was already hitting its 64 GB RAM limit, so scaling t...
New
rhcarvalho
At the heart of every Phoenix application is the often “invisible” HTTP server layer. For over a decade Cowboy has served the community ...
New
pckrishnadas88
I’ve published Part 3 of my Elixir distributed systems learning series. This part explores process monitoring using the low-level primit...
New
zorn
I wrote up a blog post talking about the first skill I made, one that does a detailed Elixir dependency update. More than Dependabot, it...
New
pckrishnadas88
I’ve published Part 4 of my Elixir distributed systems learning series. This part explores process linking using low-level primitives di...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews