Make a bit more lazy, and introduce an 'if' deriv variant

This commit is contained in:
Vivianne 2024-10-11 23:07:08 -04:00
parent e726e01deb
commit 4c2f47e04a
2 changed files with 28 additions and 3 deletions

View file

@ -12,6 +12,7 @@
deriv?
~
define~
if~
make-cell-list
cell-list?
list->cell-list
@ -120,8 +121,9 @@
(track! subscribers)
value)
(((and cell ($ <cell> value subscribers)) new-value)
(set-cell-value! cell new-value)
(trigger subscribers 'set))
(when (not (eq? (cell-value cell) new-value))
(set-cell-value! cell new-value)
(trigger subscribers 'set)))
((($ <deriv> cell effect proc))
(! cell))))
@ -162,6 +164,26 @@
(define-syntax-rule (define~ name body ...)
(define name (~ body ...)))
(define-syntax-rule (if~ test consequent alternate)
(let ((test-proc (lambda () test))
(consequent-proc (lambda () consequent))
(alternate-proc (lambda () alternate))
(last-res-type 'none)
(current-value #f))
(~ (let* ((result (test-proc))
(value (cond
((and result
(not (eq? last-res-type #t)))
(set! last-res-type #t)
(consequent-proc))
((and (not result)
(not (eq? last-res-type #f)))
(set! last-res-type #f)
(alternate-proc))
(else current-value))))
(set! current-value value)
value))))
(define-record-type <cell-list>
(_make-cell-list cells iteration-subs length-subs)
cell-list?

View file

@ -125,6 +125,9 @@
,counter)
(button (@ (click ,(lambda (e) (! counter 0))))
"Reset")
(p "The square is: " ,square))))
(p "The square is: " ,square)
,(if~ (> (! counter) 20)
`(p "Counter is big!")
`(p "I want to see " ,(~ (- 20 (! counter))) " more")))))
(append-child! (document-body) (element-ref-element root-ref))