Aetherus

Aetherus

Is there any built-in comparison function on primitives that returns `:lt`, `:eq` or `:gt`?

Is there any built-in comparison function on two numbers / strings / booleans that returns :lt, :eq or :gt?

Most Liked

LostKobrakai

LostKobrakai

Elixir didn’t introduce a protocol, but it already kinda standardized compare/2 to return :lt | :eq | :gt for passing a module to sorting functions of Enum with elixir 1.10: Enum — Elixir v1.14.0

Decimal had to do a mayor version update for the breaking change moving from -1 | 0 | 1 to :lt | :eq | :gt. It seems like Timex didn’t do so.

adamu

adamu

defprotocol Comparable do
  def compare(a, b)
end

defimpl Comparable,
  for: [Tuple, Atom, List, BitString, Integer, Float, Function, PID, Map, Port, Reference] do
  def compare(a, b) when a < b, do: :lt
  def compare(a, b) when a == b, do: :eq
  def compare(a, b) when a > b, do: :gt
end

Usage:

iex(1)> Comparable.compare(1, 2)
:lt
iex(2)> Comparable.compare("z", "a")
:gt
iex(3)> Comparable.compare([{:foo, "bar"}], foo: "bar")
:eq
Sebb

Sebb

No, only indirectly here: myers

Also Date and Time compare return those: Date — Elixir v1.14.0

adamu

adamu

Or: defdelegate compare(a, b), to: @for

adamu

adamu

Also Version.compare/2 and Logger.compare_levels/2.

I think such atoms are not needed for types that can be compared directly using term order.

Where Next?

Popular in Questions Top

freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement