Skip to content

Instantly share code, notes, and snippets.

@hchbaw
Last active August 12, 2017 14:21
Show Gist options
  • Save hchbaw/03af079b443f3f61d19603fbbb82cc26 to your computer and use it in GitHub Desktop.
Save hchbaw/03af079b443f3f61d19603fbbb82cc26 to your computer and use it in GitHub Desktop.
;; https://twitter.com/supermomonga/status/889235209782046720
;;
;; https://twitter.com/hchbaw/status/895745817897689088
;; https://twitter.com/hchbaw/status/895984112988692481
(defmacro my-eflet (specs &rest body)
(declare (indent 1))
(let ((garg (cl-gensym "args--")))
`(cl-flet (,@(loop for (n b) in specs
collect
`(,@(pcase b
(`(lambda ,arg . ,body) `(,n ,arg ,@body))
(_ `(,n (&rest ,garg) (apply ,b ,garg)))))))
,@body)))
(my-eflet ((my-1+ (apply-partially '+ 1))
(my-snoc (lambda (a d) (cons d a))))
(list
(my-1+ 1)
(mapcar #'my-1+ '(1 2 3))
(not (boundp 'my-1+))
(my-snoc 'a 'b)))
;=> (2 (2 3 4) t (b . a))
(disassemble
#'(lambda (&rest args--8025)
(apply
(lambda (a d)
(cons d a))
args--8025)))
;; byte code:
;; args: (&rest args--8025)
;; 0 constant apply
;; 1 constant <compiled-function>
;; args: (a d)
;; 0 varref d
;; 1 varref a
;; 2 cons
;; 3 return
;;
;; 2 varref args--8025
;; 3 call 2
;; 4 return
(disassemble
#'(lambda (a d) (cons d a)))
;; byte code:
;; args: (a d)
;; 0 varref d
;; 1 varref a
;; 2 cons
;; 3 return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment