Skip to content

Instantly share code, notes, and snippets.

@et2010
Forked from jordonbiondo/*scratch*.el
Created October 25, 2017 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save et2010/17bf7c5f78a868bfe71fabd86b9eb4e8 to your computer and use it in GitHub Desktop.
Save et2010/17bf7c5f78a868bfe71fabd86b9eb4e8 to your computer and use it in GitHub Desktop.
Create an async.el-friendly lambda that uses variables and functions bound in the current emacs instance.
(defmacro value-bound-lambda (args symbols &rest body)
"Returns a lambda expression with ARGS, where each symbol in SYMBOLS is
available for use and is bound to it's value at creation.
Symbols needs to be a list of variables or functions available globally."
(declare (indent defun))
(let ((vars (remove-if-not 'boundp symbols))
(funcs (remove-if-not 'functionp symbols)))
`(lambda ,args
(let ,(mapcar (lambda (sym) (list sym (symbol-value sym))) vars)
,@(mapcar (lambda (sym) `(fset ',sym ,(symbol-function sym))) funcs)
,@body))))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Example
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar foobar "foobar")
(defvar fizzbuzz "fizzbuzz")
(defun slow-vowel-remover (str)
"Returns STR with no vowels after a while."
(require 'cl)
(let ((vowel-list (string-to-list "aeiou")))
(apply 'string (remove-if
(lambda (c) (sit-for .15) (member c vowel-list))
(string-to-list str)))))
(async-start
(value-bound-lambda () (foobar fizzbuzz slow-vowel-remover)
(sit-for 1)
(concat (slow-vowel-remover foobar) (slow-vowel-remover fizzbuzz)))
(lambda (result)
(message "result: %s" (propertize result 'face
'(:foreground "green")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment