Compare commits

...

2 Commits

Author SHA1 Message Date
1e3191340c Add "conjoin" from chicken scheme 2025-01-03 09:25:49 -06:00
7e00aac988 Dropping when-not (because its unless) 2025-01-02 09:29:55 -06:00
2 changed files with 22 additions and 11 deletions

20
d-.scm
View File

@ -6,13 +6,13 @@
~>>
as~>
if-not
when-not
if-let
for
partial
argmin
iterate
upply
conjoin
distinct?
generator
macro-expand
@ -42,9 +42,6 @@
(define-syntax-rule (if-not pred body ...)
(if (not pred) body ...))
(define-syntax-rule (when-not pred body ...)
(when (not pred) body ...))
(define-syntax-rule (if-let ([ident test]) expr ...)
(let ([ident test])
(if ident
@ -73,18 +70,30 @@
vals))
(define (iterate n f v)
"Repeatedly call f on values returned from (f v)"
(if (zero? n)
v
(iterate (1- n) f (f v))))
(define (upply a b cmp . fs)
"U-shapped apply, apply fs to a and b as in compose, then apply cmp to both results"
(let ([arm-f (apply compose fs)])
(cmp (arm-f a) (arm-f b))))
(define (conjoin . preds)
"Returns a procedure that applies each pred to a single value, and returns #t if all return a truthy value. With no preds returns a one arg function that always returns true"
(if (null? preds)
(lambda (x) #t)
(lambda (x)
(if ((car preds) x)
((apply conjoin (cdr preds)) x)
#f))))
(define (distinct? . a)
"Are all values distinct, as in equal?"
(if (or (null? a) (null? (cdr a)))
#t
(and (not (any (lambda (x) (eq? (car a) x)) (cdr a)))
(and (not (any (lambda (x) (equal? (car a) x)) (cdr a)))
(apply distinct? (cdr a)))))
;; Shamelessly ripped from https://wingolog.org/archives/2013/02/25/on-generators
@ -100,6 +109,7 @@
value))))
(define-syntax generator
;; generator with an anaphoric yield
(lambda (x)
(syntax-case x ()
[(generator expr ...)

View File

@ -30,12 +30,6 @@
(define-test "test"
(assert-equal 'a (if-not #f 'a 'b))))
(define-test-suite "when-not"
(define-test "positive-case"
(assert-equal 'return (when-not #f 'do-some-stuff 'return)))
(define-test "negative-case"
(assert-unspecified (when-not #t 'do-some-stuff 'return))))
(define-test-suite "for"
(define-test "permutation"
(define value (for ([i (iota 2)]
@ -69,6 +63,13 @@
(define value (upply -3 5 = abs 1-))
(assert-equal #t value)))
(define-test-suite "conjoin"
(define-test "test"
(assert-equal #t ((conjoin negative? odd? rational?) -3))
(assert-equal #f ((conjoin negative? odd? rational?) -4)))
(define-test "vacuous"
(assert-equal #t ((conjoin) '(some donkus)))))
(define-test-suite "generator"
(define-test "test"
(define number-generator