Plaster
New
List
Login
common-lisp
default
shinmera
2021.09.29 18:53:51
(defun each (sequence function &key lifetime) (etypecase sequence (null (pend :success NIL)) (cons (labels ((next (sequence) (when sequence (let ((result (funcall function (pop sequence)))) (if (typep result 'promise) (then result (lambda (v) (declare (ignore v)) (funcall #'next sequence))) (then (pend :success sequence) #'next)))))) (then (pend :success sequence) #'next))) (vector (let ((length (length sequence))) (labels ((next (i) (when (< i length) (let ((result (funcall function (aref sequence i)))) (if (typep result 'promise) (then result (lambda (v) (declare (ignore v)) (funcall #'next (1+ i)))) (then (pend :success (1+ i)) #'next)))))) (then (pend :success 0) #'next))))))
Raw
Annotate
Repaste
Annotations
common-lisp
default
shinmera
2021.09.29 19:00:03
(defun iterate (end-p cur-fun step-fun start function) (labels ((next (object) (unless (funcall end-p object) (let* ((cur (funcall cur-fun object)) (next (funcall step-fun object)) (result (funcall function cur))) (if (typep result 'promise) (then result (lambda (v) (declare (ignore v)) (funcall #'next next))) (then (pend :success next) #'next)))))) (then (pend :success start) #'next))) (defun each (sequence function) (etypecase sequence (null (pend :success NIL)) (list (iterate #'null #'car #'rest sequence function)) (vector (let ((length (length sequence))) (iterate (lambda (i) (<= length i)) (lambda (i) (aref sequence i)) #'1+ 0 function)))))
Raw
Repaste