https://github.com/ericentin/strict_comparison
Strict numeric comparison for Elixir
https://github.com/ericentin/strict_comparison
Last synced: 4 months ago
JSON representation
Strict numeric comparison for Elixir
- Host: GitHub
- URL: https://github.com/ericentin/strict_comparison
- Owner: ericentin
- License: other
- Created: 2015-08-29T06:50:11.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2015-08-29T06:59:24.000Z (almost 11 years ago)
- Last Synced: 2025-12-13T01:50:53.825Z (6 months ago)
- Language: Elixir
- Size: 125 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/antipax/strict_comparison)
StrictComparison
================
Strict numeric comparison.
In Elixir (and Erlang), all terms are comparable. While this is useful in many situations, sometimes you want comparing anything but 2 numbers to be an error (or, in the case of function clauses, not a match).
Simply:
```elixir
use StrictComparison
```
and Elixir’s comparator functions (<, >, <=, and >=) will only allow numeric arguments within the current scope.
Includes a full test suite for both use in regular code and guards.
Also includes a benchmark (example results from my relatively modern laptop):
```
Settings:
duration: 1.0 s
## StrictComparisonBench
[02:40:05] 1/8: strict guard float
[02:40:08] 2/8: strict guard
[02:40:10] 3/8: strict comparison float
[02:40:13] 4/8: strict comparison
[02:40:15] 5/8: regular guard float
[02:40:18] 6/8: regular guard
[02:40:20] 7/8: regular comparison float
[02:40:23] 8/8: regular comparison
Finished in 19.25 seconds
## StrictComparisonBench
regular comparison 1000000 1.68 µs/op
regular comparison float 1000000 2.06 µs/op
strict comparison 1000000 2.16 µs/op
regular guard 1000000 2.16 µs/op
regular guard float 1000000 2.21 µs/op
strict guard 1000000 2.22 µs/op
strict guard float 1000000 2.33 µs/op
strict comparison float 1000000 2.50 µs/op
```
so it's a bit slower than the regular (non-strict) version. Note that each "op"
in this case is sorting 50 pairs of random numbers.