Last active
March 24, 2025 22:51
-
-
Save borwickatuw/341e7452137be4cf8cf1fe9dc67eb430 to your computer and use it in GitHub Desktop.
Common Lisp/sbcl: map a function's return values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defun multiple-value-plist (properties func &rest args) | |
| "Wrap a function call, binding each return value to properties specified in `properties. Returns a plist. | |
| For example: | |
| (multiple-value-plist '(:return-arg1 :return-arg2) #'func args) | |
| would return a plist with properties `return-arg1` and `return-arg2` bound to the first and second values returned from `#'func`." | |
| (let ((return-vals (multiple-value-list (apply func args)))) | |
| ; thanks to copilot for this: | |
| (apply #'append (mapcar #'list properties return-vals)))) | |
| ; --- | |
| (defvar example-return | |
| (multiple-value-plist | |
| '( | |
| :body-or-stream | |
| :status-code | |
| :headers | |
| :uri | |
| :stream | |
| :must-close | |
| :reason-phrase) | |
| #'drakma:http-request "http://lisp.org")) | |
| (getf example-return :headers) | |
| (defun jb-http-request (&rest args) | |
| (apply #'multiple-value-plist | |
| '( | |
| :body-or-stream | |
| :status-code | |
| :headers | |
| :uri | |
| :stream | |
| :must-close | |
| :reason-phrase) | |
| #'drakma:http-request args)) | |
| (setq testresp (jb-http-request "http://lisp.org")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment