Create a direct access array

This commit is contained in:
Mike Cifelli 2018-01-28 09:55:23 -05:00
parent 99ae99a8ab
commit 3e24991be8
1 changed files with 33 additions and 7 deletions

View File

@ -1,11 +1,37 @@
(defmacro array (size) (defmacro array (length)
`(let ,(map (lambda (x) (list (fuse 'index x))) (create-indices size)) (let* ((this (gensym))
(dlambda (index-prefix (fuse this 'index))
(:get (i) (eval (fuse 'index i))) (indices (create-indices length))
(:set (i value) (set (fuse 'index i) value))))) (index-bindings (map (lambda (i) (list (fuse index-prefix i))) indices)))
(defun create-indices (size) `(let ,index-bindings
(create-indices-tail (- size 1) ())) (setq ,this
(dlambda
(:get (i)
(eval (fuse ',index-prefix i)))
(:set (i value)
(if (< i ,length)
(set (fuse ',index-prefix i) value)))
(:length ()
,length)
(t ()
((lambda (indices accumulator)
(if (null? indices)
accumulator
(recur
(rest indices)
(cons
(list (first indices) (call ,this :get (first indices)))
accumulator))))
(reverse ',indices)
nil)))))))
(defun create-indices (length)
(create-indices-tail (- length 1) ()))
(defun create-indices-tail (index accumulator) (defun create-indices-tail (index accumulator)
(if (< index 0) (if (< index 0)