mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-19 05:39:18 +02:00
Implement SRFI 162.
This commit is contained in:
parent
69aed93502
commit
d4527d23dc
4 changed files with 124 additions and 16 deletions
|
@ -5,15 +5,8 @@
|
||||||
(chibi test))
|
(chibi test))
|
||||||
(begin
|
(begin
|
||||||
(define (run-tests)
|
(define (run-tests)
|
||||||
(define default-comparator (make-default-comparator))
|
|
||||||
(define number-comparator
|
(define number-comparator
|
||||||
(make-comparator real? = < (lambda (x . o) (exact (abs (round x))))))
|
(make-comparator real? = < (lambda (x . o) (exact (abs (round x))))))
|
||||||
(define string-comparator
|
|
||||||
(make-comparator string? string=? string<? string-hash))
|
|
||||||
(define string-ci-comparator
|
|
||||||
(make-comparator string? string-ci=? string-ci<? string-ci-hash))
|
|
||||||
(define eq-comparator (make-eq-comparator))
|
|
||||||
(define eqv-comparator (make-eqv-comparator))
|
|
||||||
(define ht-default (make-hash-table default-comparator))
|
(define ht-default (make-hash-table default-comparator))
|
||||||
(define ht-eq (make-hash-table eq-comparator 'random-argument "another"))
|
(define ht-eq (make-hash-table eq-comparator 'random-argument "another"))
|
||||||
(define ht-eqv (make-hash-table eqv-comparator))
|
(define ht-eqv (make-hash-table eqv-comparator))
|
||||||
|
|
|
@ -24,5 +24,15 @@
|
||||||
;; Comparison predicates:
|
;; Comparison predicates:
|
||||||
=? <? >? <=? >=?
|
=? <? >? <=? >=?
|
||||||
;;Syntax:
|
;;Syntax:
|
||||||
comparator-if<=>)
|
comparator-if<=>
|
||||||
(include "128/comparators.scm"))
|
;;SRFI 162:
|
||||||
|
comparator-max comparator-min
|
||||||
|
comparator-max-in-list comparator-min-in-list
|
||||||
|
default-comparator boolean-comparator real-comparator
|
||||||
|
char-comparator char-ci-comparator
|
||||||
|
string-comparator string-ci-comparator
|
||||||
|
list-comparator vector-comparator
|
||||||
|
eq-comparator eqv-comparator equal-comparator)
|
||||||
|
(include "128/comparators.scm")
|
||||||
|
(include "128/162-impl.scm")
|
||||||
|
)
|
||||||
|
|
93
lib/srfi/128/162-impl.scm
Normal file
93
lib/srfi/128/162-impl.scm
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
(define (comparator-max-in-list comp list)
|
||||||
|
(let ((< (comparator-ordering-predicate comp)))
|
||||||
|
(let loop ((max (car list)) (list (cdr list)))
|
||||||
|
(if (null? list)
|
||||||
|
max
|
||||||
|
(if (< max (car list))
|
||||||
|
(loop (car list) (cdr list))
|
||||||
|
(loop max (cdr list)))))))
|
||||||
|
|
||||||
|
(define (comparator-min-in-list comp list)
|
||||||
|
(let ((< (comparator-ordering-predicate comp)))
|
||||||
|
(let loop ((min (car list)) (list (cdr list)))
|
||||||
|
(if (null? list)
|
||||||
|
min
|
||||||
|
(if (< min (car list))
|
||||||
|
(loop min (cdr list))
|
||||||
|
(loop (car list) (cdr list)))))))
|
||||||
|
|
||||||
|
(define (comparator-max comp . args)
|
||||||
|
(comparator-max-in-list comp args))
|
||||||
|
|
||||||
|
(define (comparator-min comp . args)
|
||||||
|
(comparator-min-in-list comp args))
|
||||||
|
|
||||||
|
#;
|
||||||
|
(define default-comparator ;Defined in comparators.scm
|
||||||
|
(make-default-comparator))
|
||||||
|
|
||||||
|
(define boolean-comparator
|
||||||
|
(make-comparator
|
||||||
|
boolean?
|
||||||
|
boolean=?
|
||||||
|
(lambda (x y) (and (not x) y))
|
||||||
|
boolean-hash))
|
||||||
|
|
||||||
|
(define real-comparator
|
||||||
|
(make-comparator
|
||||||
|
real?
|
||||||
|
=
|
||||||
|
<
|
||||||
|
number-hash))
|
||||||
|
|
||||||
|
(define char-comparator
|
||||||
|
(make-comparator
|
||||||
|
char?
|
||||||
|
char=?
|
||||||
|
char<?
|
||||||
|
(lambda (c) (number-hash (char->integer c)))))
|
||||||
|
|
||||||
|
(define char-ci-comparator
|
||||||
|
(make-comparator
|
||||||
|
char?
|
||||||
|
char-ci=?
|
||||||
|
char-ci<?
|
||||||
|
(lambda (c) (number-hash (char->integer (char-downcase c))))))
|
||||||
|
|
||||||
|
(define string-comparator
|
||||||
|
(make-comparator
|
||||||
|
string?
|
||||||
|
string=?
|
||||||
|
string<?
|
||||||
|
string-hash))
|
||||||
|
|
||||||
|
(define string-ci-comparator
|
||||||
|
(make-comparator
|
||||||
|
string?
|
||||||
|
string-ci=?
|
||||||
|
string-ci<?
|
||||||
|
string-ci-hash))
|
||||||
|
|
||||||
|
(define pair-comparator
|
||||||
|
(make-pair-comparator
|
||||||
|
default-comparator
|
||||||
|
default-comparator))
|
||||||
|
|
||||||
|
(define list-comparator
|
||||||
|
(make-list-comparator
|
||||||
|
default-comparator
|
||||||
|
list?
|
||||||
|
null?
|
||||||
|
car
|
||||||
|
cdr))
|
||||||
|
|
||||||
|
(define vector-comparator
|
||||||
|
(make-vector-comparator
|
||||||
|
default-comparator
|
||||||
|
vector?
|
||||||
|
vector-length
|
||||||
|
vector-ref))
|
||||||
|
|
||||||
|
(define eq-comparator (make-eq-comparator))
|
||||||
|
(define eqv-comparator (make-eqv-comparator))
|
||||||
|
(define equal-comparator (make-equal-comparator))
|
|
@ -2,11 +2,7 @@
|
||||||
(export run-tests)
|
(export run-tests)
|
||||||
(import (scheme base) (srfi 128) (chibi test))
|
(import (scheme base) (srfi 128) (chibi test))
|
||||||
(begin
|
(begin
|
||||||
(define default-comparator (make-default-comparator))
|
|
||||||
(define real-comparator (make-comparator real? = < number-hash))
|
|
||||||
(define degenerate-comparator (make-comparator (lambda (x) #t) equal? #f #f))
|
(define degenerate-comparator (make-comparator (lambda (x) #t) equal? #f #f))
|
||||||
(define boolean-comparator
|
|
||||||
(make-comparator boolean? eq? (lambda (x y) (and (not x) y)) boolean-hash))
|
|
||||||
(define bool-pair-comparator
|
(define bool-pair-comparator
|
||||||
(make-pair-comparator boolean-comparator boolean-comparator))
|
(make-pair-comparator boolean-comparator boolean-comparator))
|
||||||
(define num-list-comparator
|
(define num-list-comparator
|
||||||
|
@ -30,9 +26,6 @@
|
||||||
vector-cdr))
|
vector-cdr))
|
||||||
(define list-qua-vector-comparator
|
(define list-qua-vector-comparator
|
||||||
(make-vector-comparator default-comparator list? length list-ref))
|
(make-vector-comparator default-comparator list? length list-ref))
|
||||||
(define eq-comparator (make-eq-comparator))
|
|
||||||
(define eqv-comparator (make-eqv-comparator))
|
|
||||||
(define equal-comparator (make-equal-comparator))
|
|
||||||
(define symbol-comparator
|
(define symbol-comparator
|
||||||
(make-comparator
|
(make-comparator
|
||||||
symbol?
|
symbol?
|
||||||
|
@ -280,4 +273,23 @@
|
||||||
#; (test (hash-salt) (fake-salt-hash #t)) ; no such thing as fake-salt-hash
|
#; (test (hash-salt) (fake-salt-hash #t)) ; no such thing as fake-salt-hash
|
||||||
) ; end comparators/bound-salt
|
) ; end comparators/bound-salt
|
||||||
|
|
||||||
|
|
||||||
|
(test-group "comparators/min-max"
|
||||||
|
(test 5 (comparator-max real-comparator 1 5 3 2 -2))
|
||||||
|
(test -2 (comparator-min real-comparator 1 5 3 2 -2))
|
||||||
|
(test 5 (comparator-max-in-list real-comparator '(1 5 3 2 -2)))
|
||||||
|
(test -2 (comparator-min-in-list real-comparator '(1 5 3 2 -2)))
|
||||||
|
) ; end comparators/min-max
|
||||||
|
|
||||||
|
(test-group "comparators/variables"
|
||||||
|
;; Most of the variables have been tested above.
|
||||||
|
(test-assert (=? char-comparator #\C #\C))
|
||||||
|
(test-assert (=? char-ci-comparator #\c #\C))
|
||||||
|
(test-assert (=? string-comparator "ABC" "ABC"))
|
||||||
|
(test-assert (=? string-ci-comparator "abc" "ABC"))
|
||||||
|
(test-assert (=? eq-comparator 32 32))
|
||||||
|
(test-assert (=? eqv-comparator 32 32))
|
||||||
|
(test-assert (=? equal-comparator "ABC" "ABC"))
|
||||||
|
) ; end comparators/variables
|
||||||
|
|
||||||
)))) ; end comparators
|
)))) ; end comparators
|
||||||
|
|
Loading…
Add table
Reference in a new issue