Questions tagged [closures]
9 questions
8
votes
3 answers
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)
(funcall g)
wtf)
;; ==> (21 11…

shynur
- 4,065
- 1
- 3
- 23
6
votes
1 answer
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) t) nil (nats (1+ n)))
But I want to call simply…

xuchunyang
- 14,302
- 1
- 18
- 39
5
votes
1 answer
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, read it
back in, and evaluate it. That’s pretty…

anquegi
- 739
- 5
- 21
3
votes
1 answer
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 as I know, the latter conses more bytes.
emacs-version:…

shynur
- 4,065
- 1
- 3
- 23
2
votes
1 answer
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 bound (to the value of load-file-name at load time),…

Richard Hansen
- 495
- 4
- 15
2
votes
1 answer
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 the error tst: Symbol's value as variable is…

Jonathan Ganc
- 53
- 6
1
vote
1 answer
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 "This works!")
(print data))
(defun send-req (url…

iLemming
- 1,223
- 9
- 14
0
votes
1 answer
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
()
#)
(setq my-g (byte-compile (lambda () (message "b"))))
=> #f(compiled-function
()
…

Daanturo
- 180
- 8
0
votes
1 answer
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-directory dir)
(compilation-environment env)
…

Prgrm.celeritas
- 849
- 6
- 15