fix: correct combinators for supporting more use-cases and be more readable

pull/1/head
anemofilia 2023-08-26 16:47:22 -03:00
parent 34ba4ca9fe
commit 2a845008da
No known key found for this signature in database
GPG Key ID: 5A8F3D62C87A2B33
2 changed files with 21 additions and 23 deletions

Binary file not shown.

View File

@ -1,41 +1,39 @@
(define-module (radix combinators)
#:use-module (srfi srfi-26)
#:use-module (ice-9 curried-definitions)
#:use-module (ice-9 match)
#:export (all?
any?
#:export (all-of
any-of
conjoin
disjoin
inclusive-disjoin))
(define (all? pred lst)
(define ((all-of pred) lst)
(null? (filter (negate pred) lst)))
(define (any? pred lst)
(define ((any-of pred) lst)
(cond ((null? lst) #f)
((pred (car lst)) #t)
(else (any? pred (cdr lst)))))
#| (conjoin (list (conjoin '()) predicates)) = (conjoin predicates) |#
(define (conjoin . predicates)
(lambda args
(if (null? predicates) #t
(match-let loop (((head-pred . tail-preds) predicates))
(cond ((null? tail-preds) (apply head-pred args))
((apply head-pred args) (loop tail-preds))
(else #f))))))
(define ((conjoin . predicates) . args)
(if (null? predicates) #t
(match-let loop (((head-pred . tail-preds) predicates))
(cond ((null? tail-preds) (apply head-pred args))
((apply head-pred args) (loop tail-preds))
(else #f)))))
#| (disjoin (list (disjoin '()) predicates)) = (disjoin predicates) |#
(define (disjoin . predicates)
(lambda args
(if (null? predicates) #f
(null? (cdr (filter (cut apply <> args)
predicates))))))
(define ((disjoin . predicates) . args)
(if (null? predicates) #f
(= 1 (length (filter (cut apply <> args)
predicates)))))
(define (inclusive-disjoin . predicates)
(lambda args
(if (null? predicates) #f
(match-let loop (((head-pred . tail-preds) predicates))
(cond ((null? tail-preds) (apply head-pred args))
((not (apply head-pred args)) (loop tail-preds))
(else #f))))))
(define ((inclusive-disjoin . predicates) . args)
(if (null? predicates) #f
(match-let loop (((head-pred . tail-preds) predicates))
(cond ((null? tail-preds) (apply head-pred args))
((not (apply head-pred args)) (loop tail-preds))
(else #f)))))