sabiwara
Elixir Core Team
Cmp - Semantic comparison and sorting
Seeing several threads about semantic comparison recently motivated me to release this project I’ve been working on:
https://github.com/sabiwara/cmp
Instead of the built-in comparison operator, Enum.sort/1, Enum.max/1… which are based on structural comparison and would happily compare/sort anything, Cmp will make sure these types are compatible and compare them semantically:
iex> Cmp.gt?(~D[2020-03-02], ~D[2019-06-06])
true
iex> Cmp.sort([~D[2019-01-01], ~D[2020-03-02], ~D[2019-06-06]])
[~D[2019-01-01], ~D[2019-06-06], ~D[2020-03-02]]
iex> Cmp.lte?(2, "1")
** (Cmp.TypeError) Failed to compare incompatible types - left: 2, right: "1"
Design goals
- Fast and well-optimized - the overhead should be quite small over built-in equivalents. See the
benchmarks/folder for more details. - No need to require macros, plain functions
- Easily extensible through the
Cmp.Comparableprotocol - Robust and well-tested (both unit and property-based)
Feedback welcome ![]()
Most Liked
sabiwara
Elixir Core Team
Cmp 0.1.2 got released and adds Cmp.sort_by/2, Cmp.max_by/2, and Cmp.min_by/2 (equivalents of the enum functions of the same name). ![]()
# structural comparison :(
iex> Enum.sort_by([%{date: ~D[2020-03-02]}, %{date: ~D[2019-06-06]}], & &1.date)
[%{date: ~D[2020-03-02]}, %{date: ~D[2019-06-06]}]
# semantic comparison :)
iex> Cmp.sort_by([%{date: ~D[2020-03-02]}, %{date: ~D[2019-06-06]}], & &1.date)
[%{date: ~D[2019-06-06]}, %{date: ~D[2020-03-02]}]
1
Popular in Announcing
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
I’d like to announce a small library called boundaries.
This is an experimental project which explores the idea of enforcing boundaries ...
New
LangChain is short for Language Chain. An LLM, or Large Language Model, is the “Language” part. This library makes it easier for Elixir a...
New
Hey everyone
i’ve developed a library for Jalaali calendar for elixir which supports converting Gregorian dates to Jalaali and vice vers...
New
I’ve just released version 3 of Comeonin, a password hashing library.
The following small changes have been made:
changes to the NIF c...
New
Dear Elixir community,
After a year of development, bug fixes, and improvements, we are proudly ready to share the release of Crawly 0.1...
New
Hey everyone,
I just released WebSockex which is a Elixir WebSocket client.
WebSockex strives to work as a OTP special process, be RFC6...
New
Just looking for a little feedback on a tiny helper library I built -
Sometimes I find the need to convert maps with atom keys to maps w...
New
Here is my first stab at this. README pasted below.
https://github.com/Hal9000/elixir_random
Comments and critiques are welcome.
Thank...
New
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
New
Other popular topics
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








