Advertisement
Guest User

dummy objects in scheme

a guest
Oct 26th, 2011
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.53 KB | None | 0 0
  1. #lang racket
  2. (require racket/match)
  3.  
  4. ;;; common utilites
  5. (define (assert-equal? a b)
  6.   (or (equal? a b)
  7.       (error (format "assert: ~a not equal to ~b" a b))))
  8.  
  9. ;;; hash utilites
  10. (define (list->hash lst)
  11.   (define (f lst h)
  12.     (if (null? lst) h
  13.         (begin
  14.           (hash-set! h (caar lst) (cdar lst))
  15.           (f (cdr lst) h))))
  16.   (f lst (make-hash)))
  17.  
  18. (define (element? x lst)
  19.   (cond ((null? lst) #f)
  20.         ((eq? (car x) (caar lst)) #t)
  21.         (#t (element? x (cdr lst)))))
  22.  
  23. (define (union-pairs a b)
  24.   (cond ((null? b) a)
  25.         ((element? (car b) a)
  26.          (union-pairs a (cdr b)))
  27.         (#t (union-pairs (cons (car b) a) (cdr b)))))
  28.  
  29. (define (join-hash master-hash slave-hash)
  30.   (if (not master-hash) slave-hash
  31.       (list->hash (union-pairs (hash->list slave-hash) (hash->list master-hash)))))
  32.  
  33. ;;; object utilites
  34. (define Object #f)
  35.  
  36. (define (getf object field)
  37.   (object (list 'get field)))
  38.  
  39. (define (setf object field newvalue)
  40.   (object (list 'set field newvalue)))
  41.  
  42. (define (incf object field value)
  43.   (setf object field (+ value (getf object field))))
  44.  
  45. (define (call object method-name . args)
  46.   (let ((f (getf object method-name)))
  47.     (apply f object args)))
  48.  
  49. (define (object->hash object)
  50.   (if (and object (procedure? object))
  51.       (object 'get-fields-hash)
  52.       #f))
  53.  
  54. (define (make-object parent . args)
  55.   (let* ((fields (join-hash (object->hash parent) (list->hash args))))        
  56.     (hash-set! fields '*parent* parent)
  57.     (define (object event)
  58.       (match event
  59.         [(list 'get field) (hash-ref fields field)]
  60.         [(list 'set field value) (hash-set! fields field value)]
  61.         [get-fields-hash fields]))
  62.     (and (hash-has-key? fields '*init*)
  63.          ((hash-ref fields '*init*) object))
  64.     object))
  65.  
  66. ;;; object test
  67. (let* ((a (make-object Object
  68.             `(*init* . ,(lambda (self)
  69.                           (display "init A\n")))
  70.             '(fielda . 0)
  71.             '(fieldb . b)
  72.             `(methodc . ,(lambda (self x) (+ (getf self 'fielda) x)))))
  73.        (b (make-object a
  74.             `(*init* . ,(lambda (self)
  75.                           (call (getf self '*parent*) '*init*)
  76.                           (display "init B\n")))
  77.             `(fielda . 1))))
  78.   (assert-equal? 0  (getf a 'fielda))
  79.   (assert-equal? 'b (getf a 'fieldb))
  80.   (assert-equal? 1  (call a 'methodc +1))
  81.   (assert-equal? 1  (getf b 'fielda))
  82.   (assert-equal? 'b (getf b 'fieldb))
  83.   (assert-equal? 2  (call b 'methodc +1))
  84.   (assert-equal? 0  (getf (getf b '*parent*) 'fielda)))
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement