Skip to content

Instantly share code, notes, and snippets.

@kaushalmodi
Last active June 26, 2017 06:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaushalmodi/40ece29159bcd26d398528d6aa696c79 to your computer and use it in GitHub Desktop.
Save kaushalmodi/40ece29159bcd26d398528d6aa696c79 to your computer and use it in GitHub Desktop.
;; Time-stamp: <2017-06-23 17:16:36 kmodi>
;; Anonymous function aka lambda macro
;; v1 - Original
;; https://ekaschalk.github.io/post/xi-macro/
(require 'dash)
(require 's)
(defmacro xi (&rest FORMS)
`(lambda ,(--filter (s-contains? (symbol-name it)
(prin1-to-string FORMS))
'(x1 x2 x3 x4 x5))
,FORMS))
(funcall (xi print (concat x2 x1)) "there" "hi")
;; v2 - Getting rid of `s' library dependency
(require 'dash)
(defmacro xi2 (&rest forms)
`(lambda ,(--filter (string-match-p (regexp-quote (symbol-name it)) (prin1-to-string forms))
'(x1 x2 x3 x4 x5))
(message "dbg2: %S" (prin1-to-string ,forms))
,forms))
(funcall (xi2 concat x2 x1) "there" "hi")
;; v3 - Attempting to get rid of `dash' library dependency
(defmacro xi3 (&rest forms)
"Macro for lambda functions with up to 9 arguments.
Example:
In:
(funcall (xi3 concat x4 x2 x3 x1)
\"four\" \"two\" \"three\" \"one\")
this:
(xi3 concat x4 x2 x3 x1)
is the same as:
(lambda (x1 x2 x3 x4)
(concat x4 x2 x3 x1))
The example funcall above will return \"onetwothreefour\"."
`(lambda ,(let ((max-arg-list (mapcar (lambda (i) (format "x%d" i)) (number-sequence 1 9)))
arg-list)
(dolist (arg max-arg-list)
(when (string-match-p arg (prin1-to-string forms))
(add-to-list 'arg-list (intern arg) :append)))
arg-list)
,forms))
(funcall (xi3 concat x4 x2 x3 x1) "four" "two" "three" "one")
;; v4 - Removing extra stuff from v3 to make it look like v1.
(defmacro xi4 (&rest forms)
`(lambda ,(let (arg-list)
(dolist (arg '(x1 x2 x3 x4 x5))
(when (string-match-p (symbol-name arg) (prin1-to-string forms))
(add-to-list 'arg-list arg :append)))
arg-list)
,forms))
(funcall (xi4 concat x4 x2 x3 x1) "four" "two" "three" "one")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment