-
Notifications
You must be signed in to change notification settings - Fork 2
/
rs-e.rkt
62 lines (52 loc) · 1.97 KB
/
rs-e.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#lang racket/base
;; Code for dealing with events.
(require racket/contract/base
racket/contract/region)
(provide (struct-out rs-e)
rs-e-create
rs-e-multiple)
(struct rs-e (fn offset) #:mutable #:transparent)
; There is probably another way to check if something is null, a list or a
; procedure in a contract but I couldn't (quickly) figure out how. So
; use a helper function. TODO extract this sort of stuff into an util file.
(define (procedure-list-or-null? input)
; Check if something is a procedure or null.
(or (procedure? input) (null? input) (list? input)))
(define (list-of-event-procs? input)
; Check if something is a list of procedures.
(and (list? input)
(and (procedure? (car input)) (= 1 (procedure-arity (car input))))
(or (null? (cdr input))
(list-of-event-procs? (cdr input)))))
(define (offset-valid? input)
; Make sure no nonsensical ranges can be supplied.
(and (real? input)
(> input -1)
(< input 1)))
(define/contract (rs-e-create #:fn fn #:offset [offset 0])
; Create an event struct with a function to run (or null) and an
; optional offset (between -1 and +1)
(->* (#:fn procedure-list-or-null?)
(#:offset offset-valid?)
rs-e?)
(rs-e fn offset))
(define/contract (rs-e-multiple procedures)
; Return a procedure that runs all the supplied procedures.
(-> list-of-event-procs? procedure?)
(lambda (step-time)
(for ([procedure procedures])
(thread (lambda ()
(procedure step-time) )))))
(module+ test
(define (rs-e-test)
(let* ([proc1
(lambda (step-time)
(printf "Calling proc 1 with argument ~a\n" step-time))]
[proc2
(lambda (step-time)
(printf "Calling proc 2 with argument ~a\n" step-time))]
[multi
(rs-e-multiple (list proc1 proc2))])
(printf "You should see both proc1 and proc2 being called with argument 3.\n")
(multi 3)))
(rs-e-test))