Created
March 1, 2017 20:39
-
-
Save yurapyon/85c956676d9db0788507b2b2f8989fa7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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)) | |
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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