Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

present for /prog/

Name: Anonymous 2008-03-03 20:30

; A prototype based object system
(define (resolve-slot object name)
  (let loop ((queue (list object)))
    (cond
      ((null? queue) #f)
      (((car queue) '_get-slot name) => (lambda (slot) slot))
      (else (loop (append (cdr queue)
                          ((car queue) '_protos)))))))
(define (meta-object)
  (let ((protos '()) (slots (make-hash-table)))
    (define (self message . args)
      (let-syntax ((meta-method
                    (syntax-rules ()
                      ((_ (method-args ...) body ...)
                       (apply (lambda (method-args ...) body ...) args)))))
        (case message
          ((_protos) protos)
          ((_slots) slots)
          ((_get-slot)
           (meta-method (name)
                        (hash-table-get slots name #f)))
          ((_set-slot!)
           (meta-method (name value)
                        (hash-table-put! slots name value)))
          ((_append-proto!)
           (meta-method (proto)
                        (set! protos (cons proto protos))))
          (else (cond
                  ((not (symbol? message))
                   (error "message must be a symbol -- META-OBJECT"
                          message args))
                  ((resolve-slot self message)
                   => (lambda (slot) (apply slot (cons self args))))
                  ; If an object doesn't respond to a message, invoke the "forward"
                  ; method if it has one.
                  ((resolve-slot self 'forward)
                   => (lambda (forward) (apply forward
                                               (append (list self message) args))))
                  (else
                   (error "don't know how to respond -- META-OBJECT"
                          message args)))))))
    self))
(define object
  (let ((self (meta-object)))
    (let-syntax ((define-slot
                   (syntax-rules ()
                     ((_ (name args ...) body ...)
                      (self '_set-slot! 'name (lambda (args ...) body ...))))))
      (define-slot (get-slot self name)
        (resolve-slot self name))
      (define-slot (set-slot! self name value)
        (self '_set-slot! name value))
      (define-slot (append-proto! self proto)
        (self '_append-proto proto))
      (define-slot (protos self)
        (self '_protos))
      (define-slot (slot-names self)
        (hash-table-map (self '_slots) (lambda (key value) key)))
      (define-slot (clone self)
        (let ((new (meta-object)))
          (new '_append-proto! self)
          (when (new 'get-slot 'init)
            (new 'init))
          new)))
    self))
(define-syntax define-method
  (syntax-rules ()
    ((_ class (name args ...) body ...)
     (class 'set-slot! 'name (lambda (args ...) body ...)))))
(define-method object (set-value! self name value)
  (self 'set-slot! name (lambda (self) value)))

Name: Anonymous 2008-03-03 20:30

(define (test-account)
  (define account (object 'clone))
  (define-method account (init self)
    (self 'set-value! 'balance 0))
  (define-method account (deposit! self amount)
    (self 'set-value! 'balance (+ (self 'balance) amount)))
  (define-method account (withdraw! self amount)
    (self 'set-value! 'balance (- (self 'balance) amount)))
 
  (let ((x (account 'clone)))
    (printf "(deposit 200)~n")
    (x 'deposit! 200)
    (printf "balance = ~a~n" (x 'balance))
    (printf "(withdraw 150)~n")
    (x 'withdraw! 150)
    (printf "balance = ~a~n" (x 'balance))))

Name: Anonymous 2008-03-03 20:47

Fucking hell, I hate LISP.

Name: Anonymous 2008-03-03 20:57

>>3
Get off of my /prog/.

Name: Anonymous 2008-03-03 20:58

I REALLY enjoyed reading your source codes.

Name: Anonymous 2008-03-03 21:10

Am I to assume this is licensed under the GPL license?

Name: Anonymous 2008-03-03 21:49

>>6
GPL license
GPL license
GPL license
GPL Public License license
License license

Name: Anonymous 2008-03-03 22:00

>>7
I lulid

Name: Anonymous 2008-03-03 22:18

>>7
Thanks for pointing that out honey. I'm off to the ATM machine so I can get some cash to purchase a new LCD display for my PC computer.

Name: Anonymous 2008-03-03 23:00

>>9
Does it run Windows with new NT technology?

Name: Anonymous 2008-03-03 23:02

>>10
No, it runs the millenium ME edition.

Name: Anonymous 2008-03-04 0:08

Did you build this inside of Emacs?

Name: Anonymous 2008-03-04 1:57

>>12
M-x configure-mode M-x make-mode

Name: Anonymous 2008-03-04 2:01

ATM Teller Machine

Name: Anonymous 2008-03-04 2:18

LCD Crystal Display
PC Computer

Name: Anonymous 2008-03-04 3:32

>>14
More like ATM To Mouth, amirite?

Name: Anonymous 2008-03-04 3:46

>>16
More like ATM Transfer Mode, amirite?

Name: Anonymous 2008-03-04 14:53

>>12
>>13
i used vim actually.
:!mzscheme -f %

Name: Anonymous 2008-03-04 15:39

VIMMER !!!

Name: Anonymous 2008-03-04 16:01

>>18
gfhyytrjhafhrtdfh tedheruylo7kr6e576454

Name: Anonymous 2008-03-04 16:56

>>20
Valid VIM command

Name: Anonymous 2008-03-04 18:16

seems like you're missing a close parenthesis....

Name: Anonymous 2008-03-04 18:19

>>21
gf open file under cursor
h move cursor left
yy yank (copy) current line
tr move cursor till r
j move cursor down
a start insert mode
fhrtdfh tedheruylo7kr6e576454 insert self

Name: Anonymous 2008-03-05 3:08

>>23
a start insert mode

YOU'RE WRONG. YOU'RE FUCKING WRONG AND YOU LIE.

Name: Anonymous 2008-03-05 4:19

a append

Name: Anonymous 2008-03-05 8:24

Good job!  But it still needs to be optimized with vectors.

Name: Anonymous 2008-03-05 8:56

>>26
-O3 -funroll-vectors

Name: Anonymous 2008-03-05 10:38

What is that?
Any example of what you can do with this code?

Name: Anonymous 2008-03-05 10:40

>>28
I wish.  It's machine code.

Name: Anonymous 2008-03-05 10:59

>>29
back to /b/, please

Name: Anonymous 2008-03-05 11:00

>>28
http://en.wikipedia.org/wiki/Prototype-based_programming
To send a message to an object: (object 'message-name . args). For example, (object 'set-value! 'life 42) (printf "~a~n" (object 'life)) prints out "42"; send the set-value! message to define a slot that has a value. You can use the set-slot! message to define a more complex slot, such as a method, or you can use the macro define-method.

Name: Anonymous 2008-03-05 12:01

>>30
I wish.  It's BBCode.

Name: Anonymous 2011-02-02 23:38

Name: Anonymous 2013-01-18 23:25

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Don't change these.
Name: Email:
Entire Thread Thread List