-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.zp
32 lines (27 loc) · 1.07 KB
/
serial.zp
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
(load "monads/monads")
(define-record-type serial
(monads:_serial val id)
monads:_serial?
(val monads:_get-serial-val monads:_set-serial-val!)
(id monads:_get-serial-id monads:_set-serial-id!))
(define (monads:serial val . id)
(define (construct val id)
(monads:generic
(monads:_serial val id)
:serial
(lambda (x)
(let ((serial (monads:get-val! x)))
(list (monads:_get-serial-val serial) (monads:_get-serial-id id))))
(lambda (f)
(let ((applied (f (monads:_serial val id))))
(monads:serial (monads:serial-val applied) (add1 (monads:serial-id applied)))))))
(cond
((list? val) (construct (car val) (cadr val)))
((null? id) (construct val 1))
(else (construct val (car id)))))
(define (monads:serial? monad) "is argument serial monad?"
(eq? (monads:get-type monad) :serial))
(define (monads:serial-id monad) "get id from serial monad"
(monads:_get-serial-id (monads:get-val! monad)))
(define (monads:serial-val monad) "get value from serial monad"
(monads:_get-serial-val (monads:get-val! monad)))