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
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.
6
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
5
Sebb
No, only indirectly here: myers
Also Date and Time compare return those: Date — Elixir v1.14.0
3
adamu
Or: defdelegate compare(a, b), to: @for
3
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.
2
Popular in Questions
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
Background
Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
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
can someone please explain to me how Enum.reduce works with maps
New
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
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
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
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
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
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
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
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
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
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
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
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
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
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New







