common lisp native coroutines
play

Common Lisp Native Coroutines (sort of. . . ahem. . . actually, no) - PowerPoint PPT Presentation

Common Lisp Native Coroutines (sort of. . . ahem. . . actually, no) Didier Verna April 4 2017 Coroutines Very old idea yield values without losing its state resume its computation later (yielding more values) transfer control


  1. Common Lisp Native Coroutines (sort of. . . ahem. . . actually, no) Didier Verna April 4 2017

  2. Coroutines ◮ Very old idea ◮ yield values without losing its state ◮ resume its computation later (yielding more values) ◮ transfer control elsewhere ◮ . . .

  3. Examples (defun squares () (loop :for i :from 0 :do (yield (* i i)))) (defun preorder (tree) (if (atom tree) (yield tree) (progn (preorder (car tree)) (preorder (cdr tree)))))

  4. The Condition System 3D Separation of Concerns, no mandatory stack unwinding

  5. Tricking the Condition System into Coroutin’ing A handler declining , but still side-effecting!

  6. Back to the Examples (define-condition yield () ((value :accessor value :initarg :value))) (defun yield (value) (signal 'yield :value value)) (defun squares () (loop :for i :from 0 :do (yield (* i i)))) (defun preorder (tree) (if (atom tree) (yield tree) (progn (preorder (car tree)) (preorder (cdr tree)))))

  7. Handling yielded values ? ? ? ? ? ,coroutine) ?

  8. Handling yielded values ? ? (handler-bind ((yield (lambda (condition) (let ((,value (value condition))) ,@body)))) ,coroutine) ?

  9. Handling yielded values ? `(restart-case (handler-bind ((yield (lambda (condition) (let ((,value (value condition))) ,@body)))) ,coroutine) (abort ())))

  10. Handling yielded values (defmacro with-coroutine (coroutine value &body body) `(restart-case (handler-bind ((yield (lambda (condition) (let ((,value (value condition))) ,@body)))) ,coroutine) (abort ())))

  11. Handling yielded values (defmacro with-coroutine (coroutine value &body body) `(restart-case (handler-bind ((yield (lambda (condition) (let ((,value (value condition))) ,@body)))) ,coroutine) (abort ()))) (defun ssq (n) (defun leaves (tree) (let ((step 0) (let (leaves) (sum 0)) (with-coroutine (preorder tree) leaf (with-coroutine (squares) sq (push leaf leaves)) (incf sum sq) (nreverse leaves))) (incf step) (when (> step n) (abort))) sum))

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend