Fix do and after

This commit is contained in:
Nathaniel Nicandro 2020-05-15 10:09:30 -05:00
parent 384e3a5bb3
commit 95b7e52ed3

View file

@ -110,26 +110,29 @@ IO-VALUE and IO-FN, the I/O context is maintained."
;; ;;
;; Based on explanations at ;; Based on explanations at
;; https://wiki.haskell.org/Introduction_to_Haskell_IO/Actions ;; https://wiki.haskell.org/Introduction_to_Haskell_IO/Actions
(defmacro jupyter-do (&rest io-fns) (defmacro jupyter-do (&rest io-values)
"Return an I/O action composing all IO-FNS. "Return an I/O action composing all I/O actions in IO-VALUES.
The action evaluates each of IO-FNS in sequence, binding the The action evaluates each of IO-VALUES in sequence. The result
result of one action to the next. The initial action is bound to of the do block is the result of performing the last action in
nil." IO-VALUES."
(declare (indent 0)) (declare (indent 0))
(if (zerop (length io-fns)) 'nil (if (zerop (length io-values)) 'jupyter-io-nil
(letrec ((after-chain (letrec ((after-chain
(lambda (io-fns) (lambda (io-values)
(if (zerop (length io-fns)) (if (= (length io-values) 1) (car io-values)
'jupyter-io-nil `(jupyter-after ,(funcall after-chain (cdr io-values))
`(jupyter-after ,(funcall after-chain (cdr io-fns)) ,(car io-values))))))
,(car io-fns)))))) ,(funcall after-chain (reverse io-values)))))
,(funcall after-chain (reverse io-fns)))))
(defun jupyter-after (io-value io-fn) ;; TODO: then? To write: "IO-A then IO-B." Also, I think then has
"Return an I/O value that binds IO-VALUE to IO-FN." ;; been used in other programs.
(defun jupyter-after (io-a io-b)
"Return an I/O action that performs IO-B after IO-A."
(declare (indent 1)) (declare (indent 1))
(make-jupyter-delayed (make-jupyter-delayed
:value (lambda () (jupyter-bind-delayed io-value io-fn)))) :value (lambda ()
(funcall io-a)
(funcall io-b))))
;;; Kernel ;;; Kernel
;; ;;