Questions tagged [closures]
The closures tag has no usage guidance.
8
questions
8
votes
3
answers
305
views
Lambda in `defun` Captures the Complete 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))...
2
votes
1
answer
251
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 ...
0
votes
1
answer
44
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 (...
1
vote
1
answer
61
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 "...
5
votes
1
answer
413
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, ...
0
votes
1
answer
49
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-...
2
votes
1
answer
335
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 ...
6
votes
1
answer
1k
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) ...