transcendental-lisp/lisp/random/array.lisp

42 lines
1.2 KiB
Common Lisp
Raw Normal View History

(let ((static))
2018-01-28 09:55:23 -05:00
(setq static
(dlambda
(:create-indices (length)
(call static :create-indices-tail (- length 1) ()))
2018-01-28 09:55:23 -05:00
(:create-indices-tail (index accumulator)
(if (< index 0)
accumulator
(recur (- index 1) (cons index accumulator))))))
2018-01-28 09:55:23 -05:00
(defmacro array (length)
(let* ((this (gensym))
(index-prefix (fuse this 'index))
(indices (call static :create-indices length))
2018-02-10 12:27:23 -05:00
(index-bindings (map (lambda (i) `(,(fuse index-prefix i))) indices))
(scope `((,this) ,@index-bindings)))
2018-01-28 09:55:23 -05:00
2018-02-10 12:27:23 -05:00
`(let ,scope
(setq ,this
(dlambda
(:get (i)
(eval (fuse ',index-prefix i)))
2018-01-28 09:55:23 -05:00
(:set (i value)
(if (and (< i ,length) (> i -1))
(set (fuse ',index-prefix i) value)
(call ,this :get i))) ;; show error
2018-01-28 09:55:23 -05:00
(:length ()
,length)
2018-01-27 20:02:03 -05:00
(t ()
2018-02-10 12:27:23 -05:00
((lambda (length accumulator)
(if (< length 1)
accumulator
(recur
2018-02-10 12:27:23 -05:00
(- length 1)
(cons (call ,this :get (- length 1)) accumulator))))
,length nil))))))))