Skip to main content

Questions tagged [closures]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
1 answer
37 views

Can I override a function in a function using a closure?

I want to build a modified version of the occur command that obeys to isearch-filter-predicate. I made some tests and I found that I can do it using a modified version of re-search-forward that I ...
Gabriele's user avatar
  • 1,662
3 votes
1 answer
165 views

why lexical environment alist ends with a `t'?

(let ((test 233)) (lambda ())) ;; ==> (closure ((test . 233) t) ;; ==> nil) I am wondering why not simply choose a proper alist, e.g.: ((test . 233)) ; instead of ((test . 233) t) As far ...
shynur's user avatar
  • 5,538
9 votes
3 answers
473 views

Lambda in `defun` Captures the Lexical Environment, But in `let` It Doesn't

My example is simplified: (defvar wtf 10) (defun f (wtf) (lambda () (cl-incf wtf))) (setq f (f 20)) (setq g (let ((wtf 30)) (lambda () (cl-incf wtf)))) (list (funcall f) ...
shynur's user avatar
  • 5,538
2 votes
1 answer
388 views

Reliable lexical binding?

How does one reliably create a lexically bound/scoped variable? Consider the following: ;; -*- lexical-binding: t; -*- (let ((f load-file-name)) (defun foo () f)) Normally f would be lexically ...
Richard Hansen's user avatar
0 votes
1 answer
74 views

How to disassemble an lambda/anonymous function given its hex adress but not name?

Assume that we have: (defalias 'my-f (lambda () (message "a"))) (byte-compile 'my-f) => #f(compiled-function () #<bytecode 0x1dc01052127a>) (setq my-g (byte-compile (...
Daanturo's user avatar
  • 180
1 vote
1 answer
83 views

Why funcall doesn't work from a closure

I'm having difficulty to call a function passed as an argument. Why the following snippet doesn't work, and how can I make it work? lexical-binding is set to t (defun on-success (data) (print "...
iLemming's user avatar
  • 1,273
5 votes
1 answer
502 views

About closure creation

After reading this article about readable closures, I check that: Since closures are byte-code function objects, they print readably. You can capture an environment in a closure, serialize it, ...
anquegi's user avatar
  • 749
0 votes
1 answer
50 views

capture and replay a closure later

I am having a problem working with closures. I hope I can explain this clearly. When I try to start a compilation I typically do it like this: (let ((dir (get-dir)) (model-root root) (default-...
Prgrm.celeritas's user avatar
2 votes
1 answer
343 views

Why does this straightforward use of a closure fail?

Suppose you save the following code to some file test.el and then do load-file test.el ;;; -*- lexical-binding: t -*- (let ((closure-vbl 0)) (defun tst () (eval 'closure-vbl))) (tst) You get ...
Jonathan Ganc's user avatar
6 votes
1 answer
2k views

How to make local function binding for closure?

For example, when defining the natural number sequence stream, I can use ;; -*- lexical-binding: t; -*- (defun nats (n) (cons n (lambda () (nats (1+ n))))) (nats 0) => (0 closure ((n . 0) ...
xuchunyang's user avatar
  • 14.6k