git @ Cat's Eye Technologies define-opaque / 7123d71
Refactor the stack datatype to be simpler and easier to use. Chris Pressey 8 months ago
4 changed file(s) with 40 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
2121 If at some point it becomes more usable, I will update
2222 its name.
2323
24 ### TODO
25
26 - [ ] is more than one private field supported?
27 - [ ] support supplying initial values for private fields
28
29
2430 [Information Hiding in Scheme]: https://github.com/cpressey/Information-Hiding-in-Scheme
2020 (lambda (target args)
2121 (opaque-ops target args ops))))
2222 )
23 (make-name '(1 2 3)))))))
23 (make-name '()))))))
0 ; usage: csi -q -b demo.scm
1
2 (load "define-opaque.scm")
3
4 (define-opaque-lousy stack make-stack (items)
5 (
6 (new (lambda (args)
7 (make-stack (car args))))
8 (push (lambda (args)
9 (make-stack (cons (car args) items))))
10 (top (lambda (args)
11 (car items)))
12 (pop (lambda (args)
13 (make-stack (cdr items))))
14 )
15 )
16
17 (define demo (lambda ()
18 (let* (
19 (stack0 (stack 'new '((4 5 6))))
20 (stack1 (stack0 'pop '()))
21 (stack2 (stack1 'push '(9)))
22 (stack3 (stack2 'pop '()))
23 (stack4 (stack3 'push '(8)))
24 )
25 (display (stack0 'top '())) (newline)
26 (display (stack1 'top '())) (newline)
27 (display (stack2 'top '())) (newline)
28 (display (stack3 'top '())) (newline)
29 (display (stack4 'top '())) (newline)
30 )))
31
32 (demo)
+0
-33
src/demo.scm less more
0 ; usage: csi -q -b demo.scm
1
2 (load "define-opaque.scm")
3
4 (define-opaque-lousy stack make-stack (items)
5 (
6 (push (lambda (args)
7 (make-stack (cons (car args) items))))
8 (pop (lambda (args)
9 (let* ( (item (car items))
10 (new-items (cdr items)) )
11 (cons
12 (make-stack new-items)
13 item))))
14 )
15 )
16
17 (define demo (lambda ()
18 (let* (
19 (result (stack 'pop '()))
20 (stack2 (car result))
21 (item (cdr result))
22 (stack3 (stack2 'push '(9)))
23 )
24 (display item)
25 (newline)
26 (display (stack2 'pop '()))
27 (newline)
28 (display (stack3 'pop '()))
29 (newline)
30 )))
31
32 (demo)