Skip to content

Instantly share code, notes, and snippets.

@jafonsor
Last active August 29, 2015 14:20
Show Gist options
  • Save jafonsor/52ba1c803a06ac5eb739 to your computer and use it in GitHub Desktop.
Save jafonsor/52ba1c803a06ac5eb739 to your computer and use it in GitHub Desktop.
(defun inc-generator (zero incer)
#'(lambda ()
(progn (setf zero (funcall incer zero))
zero)))
(defun init-vec (dim initer)
(if (= 0 dim)
()
(init-vec (cons (initer) (init-vec (- dim 1) initer)))))
(defun seq-inc-generator (seq)
(let ((index 0)
(seq-length (length seq)))
#'(lambda ()
(let ((ret-val (nth (mod index seq-length) seq)))
(progn
(setf index (+ 1 index))
ret-val)))))
(defun create-list-from (dim initer)
(if (= 0 dim)
()
(cons (funcall initer) (create-list-from (- dim 1) initer))))
(defun create-tensor (dims initer)
(if (null (cdr dims))
(create-list-from (car dims) initer)
(loop for x from 1 to (car dims)
collect (create-tensor (cdr dims) initer))))
(defun v (&rest args) args)
(defun tensor-dims (tensor)
(if (not (listp tensor))
()
(cons (length tensor) (tensor-dims (car tensor)))))
(defun reshape (dims seq)
(create-tensor dims (seq-inc-generator seq)))
(defun without-last (lst)
(cond ((null lst) ())
((null (cdr lst)) ())
(t (cons (car lst) (without-last (cdr lst))))))
;-- printing tensors --
(defun print-lst (lst)
(loop for elm in lst do
(format t "~S " elm)))
(defun print-tensor (tensor)
(if (not (listp (car tensor)))
(progn
(print-lst tensor))
(progn
(loop for sub-tensor in tensor do
(progn
(print-tensor sub-tensor)
(format t "~%"))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment