Questions tagged [lexical-closures]

Lexical closures are functions, often anonymous, that capture, or close over, their lexical environment. Lexical closures are used extensively in functional programming techniques that involve higher-order functions.

53 questions
3
votes
1 answer

Relationship between the LexicalEnviroment object and [[Enviroment]]

It is told that every block of code has a hidden object called LexicalEnviroment. That object contains a reference to the outer scope and an EnviromentRecord, which contains information about the current scope. On the other hand, it is said that…
user6898463
3
votes
6 answers

How is this pair of JavaScript functions different?

I’m having a hard time discerning how exactly JavaScript closures work. Please take a look at these two functions and tell how they are different that they produce entirely different results when called multiple times: Function 1 var add =…
pedroyanky
  • 323
  • 2
  • 10
3
votes
1 answer

Creating a closure around a private function in elisp

In elisp I have the following (setq lexical-binding t) (fset 'boom (cl-flet* ((tickle () (message "hi")) (pickle () (tickle))) (lambda () …
jacob
  • 2,284
  • 3
  • 20
  • 21
3
votes
3 answers

Differences between Common Lisp and Scheme lexical closures

In Common Lisp I can evaluate the following snippet of code (in SBCL) without being signalled any syntax error: (let ((x 0)) (defun my-incf (y) (setf x (+ x y))) (defun my-decf (y) (setf x (- x y)))) MY-DECF CL-USER> (my-incf…
Paulo Tomé
  • 1,910
  • 3
  • 18
  • 27
2
votes
2 answers

JavaScript Closure changing variable value from the outer scope

I don't understand why my returned function didn't change the value of the variable from the outer function. Hi, I wrote js function: function num() { let number = 0; console.log(number) return function() { return number++ …
2
votes
1 answer

Error running timer: (void-variable message) in Emacs init.el

Why do I get Error running timer: (void-variable message) in the function below in my `init.el - Emacs? (defun cypher/cowsayx-sclock (in-minutes message) (interactive "nSet the time from now - min.: \nsWhat: ") (run-at-time (* in-minutes 60) …
2
votes
3 answers

What is in a Python closure and what are the caveats for people used to OCaml?

This is a sort of a follow-up on an old answer to a question about the necessity of functools.partial : while that answer very clearly explains the phenomenon and the basic reason for it, there are still some unclear points to me. To recap, the…
ysalmon
  • 142
  • 8
2
votes
4 answers

temporarily overwrite a globally defined function in Scheme let block?

suppose I have the following functions: (define (g x) (f x)) (define (f x) (+ 1 x)) I would like to temporarily call g with a different f. For example, something like this: (let ((f (lambda (x) (+ 2 x)))) (g 5)) I would like the code above to…
xdavidliu
  • 2,411
  • 14
  • 33
2
votes
3 answers

Identify how the function has been called in closure javascript

Recently i faced one problem in hackerrank which has to calculate multiplication operation and has to return the answer. For example function multiply(a,b) { return a*b; } Now here is the problem the function might call in different ways such…
Kannan T
  • 1,639
  • 5
  • 18
  • 29
2
votes
1 answer

OCaml lexical vs dynamic scoping

I have doubts about some topics. In short I have to write an interpreter in Ocaml both static and dynamic scope. For now I implemented a version with static scoping through the use of an environment (IDE * value) lists, and a eval (evn * exp),…
Jilz
  • 31
  • 5
2
votes
2 answers

Nested Functions, Closures and Scope

I've been trying to wrap my head around scope, specially closures. I know that there are many posts about the topic, and I've been reading a lot. But most places refer to the topic as advanced, and use terminology that is relatively difficult to…
Scb
  • 339
  • 1
  • 5
  • 19
2
votes
2 answers

Racket using lexical closures

I read in Let over Lambda about lexical clojures and this code sample was given for Common Lisp: (let ((counter 0)) (lambda () (incf counter))) I tried writing this in Racket as: (let ((counter 0)) (lambda() (+ counter 1))) When I call it in…
Theodor Berza
  • 573
  • 3
  • 12
2
votes
1 answer

The nuances of closing over in Javascript

I've been trying to understand the functional composition techniques of Javascript and I came up with his working code of a toy quasi-MVC to demonstrate my question: var modelFactory = function() { var _state = false; var _listener =…
2
votes
0 answers

PHP closure functions: why does a closure have to be an anonymous function?

A lambda or anonymous function is just a function without a name. e.g. $lambda = function($a, $b) { echo $a + $b; }; A closure is a function which has access to variables not specified in its parameter list. In PHP 5.3+ these variables are…
John Sonderson
  • 3,238
  • 6
  • 31
  • 45
2
votes
3 answers

Is this scoping possible in javascript?

I am working on a javascript framework. I have several independent scripts that look like this: core.modules.example_module = function(sandbox){ console.log('wot from constructor ==', wot); return{ init : function(){ …
kevzettler
  • 4,783
  • 15
  • 58
  • 103