← 피드로
It took me years to find a real-world use case for lispy-mode’s convolute key, bound to C! Here it is 🙂
My original code. There’s a loop and error handling inside. I want to move the error handling before the loop.
;; ORIGINAL
(defun post-products (products &rest keys)
"Call the API, at most 500 products by POST."
(let ((batches (batches products 500)))
(loop for batch in batches
for i = 1 then (incf i)
with nb-batches = (length batches)
do
(format t "POST request ~a/~a… " i nb-batches)
(handler-case
(prog1
(apply #'post/public/offers/v1/products/ean :verbose t :location (location) :products batch keys)
(format t "~&POST batch products ~a/~a done.~&" i nb-batches))
;; TODO: stop loop earlier.
(error (c)
(format *error-output* "~&POST products error: ~a~&" c))))))
Enter fullscreen mode Exit fullscreen mode
Put the cursor here on the prog1:
(defun post-products (products &rest keys)
"Call the API, at most 500 products by POST."
(let ((batches (batches products 500)))
(loop for batch in batches
for i = 1 then (incf i)
with nb-batches = (length batches)
do
(format t "POST request ~a/~a… " i nb-batches)
(handler-case
cursor ------> |(prog1
(apply #'post/public/offers/v1/products/ean :verbose t :location (location) :products batch keys)
(format t "~&POST batch products ~a/~a done.~&" i nb-batches))
;; TODO: stop loop earlier.
(error (c)
(format *error-output* "~&POST products error: ~a~&" c))))))
Enter fullscreen mode Exit fullscreen mode
If you didn’t, either activate Alt-x lispy-mode and press capital C or call Alt-x lispy-convolute (with the mode active or inactive), and TADA!
;; We want to move the HANDLER-CASE before the loop.
;;
;; With lispy-mode => C (convolute)
;;
(defun post-products (products &rest keys)
"Call the API, at most 500 products by POST."
(let ((batches (batches products 500)))
(handler-case
(loop for batch in batches
for i = 1 then (incf i)
with nb-batches = (length batches)
do
(format t "POST request ~a/~a… " i nb-batches)
(prog1
(apply #'post/public/offers/v1/products/ean :verbose t :location (location) :products batch keys)
(format t "~&POST batch products ~a/~a done.~&" i nb-batches)))
;; TODO: stop loop earlier.
(error (c)
(format *error-output* "~&POST products error: ~a~&" c)))))
Enter fullscreen mode Exit fullscreen mode
If I record a video, will you make it a trending one?
추출 본문 · 출처: dev.to · https://dev.to/vindarel/emacs-lispy-mode-convolute-real-world-example-fe1
답글 남기기