{"id":22762490,"url":"https://github.com/sabiwara/cmp","last_synced_at":"2025-04-14T20:42:29.937Z","repository":{"id":105503151,"uuid":"609429356","full_name":"sabiwara/cmp","owner":"sabiwara","description":"Semantic comparison and sorting for Elixir","archived":false,"fork":false,"pushed_at":"2024-05-25T03:51:39.000Z","size":36,"stargazers_count":25,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-07T13:07:58.578Z","etag":null,"topics":["comparison","elixir-lang","sorting"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sabiwara.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-04T06:19:39.000Z","updated_at":"2024-09-22T01:27:34.000Z","dependencies_parsed_at":"2024-10-26T06:13:02.883Z","dependency_job_id":"b19fae39-46f1-4fc0-a4ef-aa79be59d8dd","html_url":"https://github.com/sabiwara/cmp","commit_stats":{"total_commits":12,"total_committers":1,"mean_commits":12.0,"dds":0.0,"last_synced_commit":"f962d4c718a7b1b7fb88f7119139da9e9132fe7b"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabiwara%2Fcmp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabiwara%2Fcmp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabiwara%2Fcmp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sabiwara%2Fcmp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sabiwara","download_url":"https://codeload.github.com/sabiwara/cmp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248959952,"owners_count":21189970,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["comparison","elixir-lang","sorting"],"created_at":"2024-12-11T10:08:16.704Z","updated_at":"2025-04-14T20:42:29.890Z","avatar_url":"https://github.com/sabiwara.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cmp\n\n[![Hex Version](https://img.shields.io/hexpm/v/cmp.svg)](https://hex.pm/packages/cmp)\n[![docs](https://img.shields.io/badge/docs-hexpm-blue.svg)](https://hexdocs.pm/cmp/)\n[![CI](https://github.com/sabiwara/cmp/workflows/CI/badge.svg)](https://github.com/sabiwara/cmp/actions?query=workflow%3ACI)\n\nSemantic comparison and sorting for Elixir.\n\n## Why `Cmp`?\n\nThe built-in comparison operators as well as functions like `Enum.sort/2` or\n`Enum.max/1` are based on Erlang's term ordering and suffer two issues, which\nrequire attention and might lead to unexpected behaviors or bugs:\n\n### 1. Structural comparisons\n\nBuilt-ins use\n[structural comparison over semantic comparison](https://hexdocs.pm/elixir/Kernel.html#module-structural-comparison):\n\n```elixir\niex\u003e ~D[2020-03-02] \u003e ~D[2019-06-06]\nfalse\n\niex\u003e Enum.sort([~D[2019-01-01], ~D[2020-03-02], ~D[2019-06-06]])\n[~D[2019-01-01], ~D[2020-03-02], ~D[2019-06-06]]\n```\n\nSemantic comparison is available but not straightforward:\n\n```elixir\niex\u003e Date.compare(~D[2019-01-01], ~D[2020-03-02])\n:lt\n\niex\u003e Enum.sort([~D[2019-01-01], ~D[2020-03-02], ~D[2019-06-06]], Date)\n[~D[2019-01-01], ~D[2019-06-06], ~D[2020-03-02]]\n```\n\n`Cmp` does the right thing out of the box:\n\n```elixir\niex\u003e Cmp.gt?(~D[2020-03-02], ~D[2019-06-06])\ntrue\n\niex\u003e Cmp.sort([~D[2019-01-01], ~D[2020-03-02], ~D[2019-06-06]])\n[~D[2019-01-01], ~D[2019-06-06], ~D[2020-03-02]]\n```\n\n### 2. Weakly typed\n\nBuilt-in comparators accept any set of operands:\n\n```elixir\niex\u003e 2 \u003c \"1\"\ntrue\n\niex\u003e 0 \u003c true\ntrue\n\niex\u003e false \u003c nil\ntrue\n```\n\n`Cmp` will only compare compatible elements or raise a `Cmp.TypeError`:\n\n```elixir\niex\u003e Cmp.lte?(1, 1.0)\ntrue\n\niex\u003e Cmp.lte?(2, \"1\")\n** (Cmp.TypeError) Failed to compare incompatible types - left: 2, right: \"1\"\n```\n\n## Installation\n\n`Cmp` can be installed by adding `cmp` to your list of dependencies in\n`mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:cmp, \"~\u003e 0.1.3\"}\n  ]\nend\n```\n\nThe documentation can be found at\n[https://hexdocs.pm/cmp](https://hexdocs.pm/cmp).\n\n## Design goals\n\n- Fast and well-optimized - the overhead should be quite small over built-in\n  equivalents. See the `benchmarks/` folder for more details.\n- No need to require macros, plain functions\n- Easily extensible through the `Cmp.Comparable` protocol\n- Robust and well-tested (both unit and property-based)\n\nSupporting comparisons between non-homogeneous types such as mixed `Decimal` and\nbuilt-in numbers for instance is a non-goal. This limitation is a necessary\ntrade-off in order to ensure the points above. Use the `Decimal` library\ndirectly if you need this.\n\n## Copyright and License\n\nCmp is licensed under the [MIT License](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsabiwara%2Fcmp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsabiwara%2Fcmp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsabiwara%2Fcmp/lists"}