Title

Comparators sublibrary

Author

John Cowan

Status

This SRFI is currently in final status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to srfi-162@nospamsrfi.schemers.org. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.

Abstract

This SRFI provides a few extra procedures and comparators to go with SRFI 128, Comparators. Implementers are urged to add them to their SRFI 128 libraries, for which reason they are not packaged as a separate library.

Rationale

The original specification for comparators was SRFI 114. This was a large and complex SRFI with many procedures and comparators; its sample implementation had many bugs. I replaced it with SRFI 128, which is much simpler and cleaner. However, I now believe that SRFI 114 was cut back too far. In particular, there are no comparators (as opposed to comparator constructors) in SRFI 128. This SRFI provides the comparators of SRFI 114 that seem generally useful, so as to have them generally available without needing to define them locally.

The SRFI 114 procedures comparator-max and comparator-min have also been restored because they are equivalent to the max and min methods of the Haskell typeclass Ord. Comparators are equivalent to the three typeclasses Eq, Ord, and Hashable, but the first and last typeclass do not provide any additional methods not already available in SRFI 128 except inequality, which is normally not provided by Scheme libraries. Equivalent procedures that accept a list have also been added.

Specification

Procedures

(comparator-max comparator obj1 obj2 ...)

(comparator-min comparator obj1 obj2 ...)

(comparator-max-in-list comparator list)

(comparator-min-in-list comparator list)

These procedures are analogous to min and max respectively, but may be applied to any orderable objects, not just to real numbers. They apply the ordering procedure of comparator to the objects to find and return a minimal (or maximal) object. The order in which the values are compared is unspecified. If two objects are equal in the sense of the comparator, either may be returned.

The -in-list versions accept a single list argument.

Useful comparators

These comparators are useful for comparing standard Scheme types, and should be more efficient than default comparators.

default-comparator

A pre-created default comparator. It should behave exactly like a comparator returned by SRFI 128 make-default-comparator.

The following pre-created comparators must provide both ordering predicates and hash functions. They behave exactly like comparators returned from the example calls on make-comparator shown in SRFI 128.

boolean-comparator

A comparator for booleans such that #f compares before #t.

real-comparator

A comparator for real numbers such that smaller numbers compare before larger numbers.

char-comparator

A comparator for characters using Unicode codepoint order.

char-ci-comparator

A comparator for characters using char-ci<?.

string-comparator

A comparator for strings using the implementation-specific definition of string<?.

string-ci-comparator

A comparator for strings using the implementation-specific definition of string-ci<?.

pair-comparator

Compares pairs as if by the application of make-pair-comparator to pairs with default-comparator as the car and cdr comparators.

list-comparator

Compares lists as if by the application of make-list-comparator to lists with default-comparator as the element comparator.

vector-comparator

Compares vectors as if by the application of make-vector-comparator to vectors with default-comparator as the element comparator.

The following comparators behave exactly like the comparators returned by calls to make-eq-comparator, make-eqv-comparator, and make-equal-comparator respectively.

eq-comparator

eqv-comparator

equal-comparator

Implementation

The sample implementation is found in a single file in the repository of this SRFI named srfi/128/162-impl.scm. It should simply be included in whichever SRFI 128 library is in use by the implementation. It is important to include it after all other files. It is also of course necessary to extend the library exports to include the four new procedures and thirteen new comparators.

Alternatively, there is a full implementation of the (srfi 128) library including all extensions from this SRFI. The supported Schemes are Chicken 5 and Chibi.

The following files are part of the implementation:

Copyright

Copyright © John Cowan (2018). All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Editor: Arthur A. Gleckler