-
Notifications
You must be signed in to change notification settings - Fork 4
/
hook.sld
41 lines (34 loc) · 1.18 KB
/
hook.sld
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
(define-library (schemepunk hook)
(export make-hook hook?
list->hook list->hook!
hook-add! hook-delete! hook-reset!
hook->list
hook-run)
(import (scheme base)
(schemepunk syntax)
(schemepunk list))
(cond-expand
((and (not chicken) (library (srfi 173)))
(import (srfi 173)))
(else
(cond-expand
((and chicken debug) (import (only (srfi 99) define-record-type)))
(else))
(begin
(define-record-type Hook
(list->hook arity handlers)
hook?
(arity hook-arity)
(handlers hook->list list->hook!))
(define (make-hook arity)
(list->hook arity '()))
(define (hook-add! hook proc)
(list->hook! hook (cons proc (hook->list hook))))
(define (hook-delete! hook proc)
(list->hook! hook (filter (λ x (not (eq? x proc)))
(hook->list hook))))
(define (hook-reset! hook)
(list->hook! hook '()))
(define (hook-run hook . args)
(assume (= (length args) (hook-arity hook)))
(for-each (cut apply <> args) (hook->list hook)))))))