Last active
April 2, 2016 21:01
-
-
Save bishboria/0157103e105d7144fe79ececc33d77fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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