Skip to content

Instantly share code, notes, and snippets.

@bishboria

bishboria/minimal-oo.scm

Last active Apr 2, 2016
Embed
What would you like to do?
; Minimal "object oriented" scaffolding
(define (object methods)
(lambda (label method-parameters)
((cdr (assoc label methods)) method-parameters)))
; declaration: easily do funkier state stuff here
(define (person name dob)
(object `( (name . ,(lambda () name))
(age . ,(lambda (today) (- today dob)))))) ; absolute nonsense, but an example of param passing
; usage
(define someone (person 'stuart 19830102))
(someone 'name)
(someone 'age 20160402)
; funkier state thing. Didn't say it was a pretty OO system, just minimal
(define (funky foo bar)
(define vals (list foo bar))
(object `((foo . ,(lambda () (car vals)))
(bar . ,(lambda () (cadr vals)))
(foo! . ,(lambda (foo) (set-car! vals foo)))
(bar! . ,(lambda (bar) (set-car! (cdr vals) bar))))))
(define f (funky 'test 'data))
(display (f 'foo))
(display (f 'bar))
(f 'foo! 'hello)
(f 'bar! 'world)
(display (f 'foo))
(display (f 'bar))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment