Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
;; dispatcher is a work in progress haha
(define-syntax dispatcher
(lambda (x)
(syntax-case x (else)
((d name (case1 do1 ...) ... )
(with-syntax ( (args (datum->syntax #'d 'args)) )
#'(define (name m . args)
(case m
(case1 do1 ...) ...
(else (error (symbol->string 'name) "bad operation" m)))))))))
(define stream
(case-lambda
(()
(let ( (into '()) (data 0) )
(dispatcher stream
('connect! (set! into (append into args)))
('ping! (set! data (car args))
(for-each (lambda (o) (o 'call!)) into))
('reset! (for-each (lambda (o) (o 'reset!)) into))
('call! (error "stream" "cannot call top-level stream" 'name))
('read data)
)
stream))
((deps fn)
(let ( (into '()) (go-on (length deps)) (acc 0) (data 0) )
(dispatcher stream
('connect! (set! into (append into args)))
('ping! (set! data (car args))
(for-each (lambda (o) (o 'call!)) into))
('reset! (set! acc 0)
(for-each (lambda (o) (o 'reset!)) into))
('call! (set! acc (+ 1 acc))
(if (> acc go-on)
(error "stream" "bad shenanigans")
(when (= acc go-on)
(let ( (val (apply fn (map (lambda (s) (s 'read)) deps))) )
(stream 'ping! val)))))
('read data)
)
(map (lambda (d) (d 'connect! stream)) deps)
stream))
))
(define *time* (stream))
(define (clock phase)
(let ( (acc 0) (state 'low) (half (/ phase 2)) )
(stream (list *time*)
(lambda (dt)
(set! acc (+ acc dt))
(if (> acc half)
(begin
(set! acc 0)
(case state ('low (set! state 'high) 'rising)
('high (set! state 'low) 'falling)) )
state)))))
(define c0 (clock 0.25))
(define c1 (clock 0.5))
(define c2 (clock 1))
;; if you have a main loop in your programm that gives you the time between frames (delta-time)
;; all you have to do is (*time* 'ping! delta-time)
;; and clocks will run on thier own
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment