Questions tagged [closure]

A function (often anonymous) bound to the referencing environment in its original lexical scope in such a way that it will still have access to that environment (its variables and other references) if executed outside that scope.

Filter by
Sorted by
Tagged with
0 votes
0 answers
81 views

Debouncing events and stale closures in functional React

Context As part of our React application, in which we use functional components, we encountered a scenario where we needed to debounce parts of a DOM event listener for usability purposes. The actual ...
M0nst3R's user avatar
  • 213
1 vote
2 answers
116 views

Each item of the array must loop and show message for some time (duration) and repeat msg each value of delay

Is there a better way to loop item of an array using forEach and make repeat it using setTimout/...
HackerMF's user avatar
  • 121
2 votes
0 answers
28 views

Make better solution for Bean counting example from "Eloquent JavaScript" book

How can this code be improved by "closuring" of functions here? ...
maxiwer's user avatar
  • 21
2 votes
1 answer
361 views

Display a Text() with a foreground color based on a condition

I decided to write a simple tab bar for macOS using Swift. tabs.swift ...
shreyasm-dev's user avatar
1 vote
0 answers
125 views

A Tabbed calculator in python with tkinter

For learning purposes, I have written a small calculator app on Python, tkinter. I want to check if my design is on best practices for OOP, And seek advice on Nested functions For easy readability, I ...
Mattey's user avatar
  • 29
2 votes
0 answers
44 views

Finding, substituting and executing values in dictionaries

I wrote this code for finding, substituting and executing values in dictionary, much like $() bash operator works. I used nested functions for this, forming ...
nonForgivingJesus's user avatar
2 votes
0 answers
64 views

Immutable-Object-like Structure in PHP

I made a closure-based PHP structure that behaves mostly like a classical object. To change the object's state, one must derive a new copy by passing the altered state to the ...
Benni's user avatar
  • 143
2 votes
1 answer
107 views

Toggling the background color of a page using JavaScript closures

This question is to see if I can get some input on the "design pattern" I tried to implement here. I'm just learning about closure in JavaScript and I think I'm starting to get it. I'm wondering if ...
Anthony's user avatar
  • 191
5 votes
2 answers
975 views

Groovy script to get average price per group from different data arrays

I've been trying to learn Groovy lately and I tried to solve a problem which involves designing a routine that will calculate the average Product price per Group. I have the following data: ...
Marcelo Tataje's user avatar
5 votes
1 answer
2k views

Logging with decorators (passing log file name as argument)

This is for some quantum mechanics software so there will be references to molecules/atoms etc but the core issue is a python logging one, unrelated to all that. I have a couple of logging decorators,...
QuantumChris's user avatar
  • 1,370
3 votes
1 answer
377 views

Rust closure to be called on a cache miss

The official Rust book chapter 13.1 includes an exercise to expand on the example provided in the chapter: Try modifying Cacher to hold a hash map rather than a single value. The keys of the hash map ...
qwerty's user avatar
  • 33
2 votes
1 answer
130 views

Closure implementation

I have custom cell of tableview which is used as header as well as normal cell. In custom cell I have this properties ...
Prashant Tukadiya's user avatar
-3 votes
1 answer
52 views

Javascript - How does this preservation of variable works in Closure javascript? [closed]

...
sofs1's user avatar
  • 97
11 votes
2 answers
2k views

Throttling execution of a function

I have a Javascript function that calls my backend API using AJAX, let's say, updateDataUsingAjax. This is a resource intensive process, and I do not wish to allow ...
Nisarg Shah's user avatar
1 vote
2 answers
96 views

Performing a sum with Swift

I wrote the following in Swift to perform a sum: let total:Double = session.courses.reduce(0) { $0 + $1.totalPrice() } It's concise, it does the job but it does ...
Arnaud's user avatar
  • 111
2 votes
1 answer
342 views

Toggle case of a string in Swift

What is benefit of using uppercased() / lowercased() over the following approach ? (Other than making it more readable) Any ...
kanadenipun's user avatar
0 votes
1 answer
470 views

MS Fakes Shims. Assert "was called" (by closures)

Is it a good or bad practice to use closures for asserting the method was called in such a way? The method set a shim and return an Action wich both capture local mutable variable: ...
SerG's user avatar
  • 171
2 votes
0 answers
51 views

Using a Closure to generate express route handler function

I'm using a lambda function to generate the callback handler for each list page in a group of routes, because the sql is very repetitive for each of these routes. The lambda function uses a callout to ...
HSchmale's user avatar
  • 875
4 votes
2 answers
74 views

Print out number x followed by everything in the range of 1 to (x - 1)

The original idea is to print the following numbers: 7, 1, 2, 3, 4, 5, 6 So some number x followed by everything in the range ...
EJoshuaS - Stand with Ukraine's user avatar
2 votes
1 answer
203 views

Maintaining state in a pagination handler

In my application I wrote a pagination code that retrieves data from the server as the user scrolls down. In this case, I need to mantain the current page, whether a request is being processed, and ...
victor's user avatar
  • 233
2 votes
1 answer
754 views

Array wrapper concept with method chaining that provides a fluent interface

I've been working on a class(2 actually) that wraps around the array data structure of PHP. It doesn't bring anything new to the table, it simple makes the function ...
user2994883's user avatar
3 votes
1 answer
684 views

Optionally Lazy Parameters

Inspired by Swift's @autoclosure feature, I tried writing a brief C++-14 header that permits "optionally" lazy parameters (by "lazy" I mean ...
Kyle Strand's user avatar
5 votes
2 answers
153 views

Filtering the attributes for a custom tag

I have function that takes all of the attributes passed to a custom tag, and returns a select subset. I am try to convert the current function, which does not use closures ...
James A Mohler's user avatar
3 votes
2 answers
53 views

Configuring fields to be extracted by a data processor

I solved this problem using closures with Perl and I wonder if/how I could/should have done it differently? Background: User creates config file describing properties of some source data file that ...
lzc's user avatar
  • 161
2 votes
1 answer
80 views

Optimize KnockoutJs Pagination Computed Function

I have written a function that builds an array of paging options for rendering on the view. I have modelled it after DataTables for jQuery, a live example with lots of data is available here: http://...
Sean Thorburn's user avatar
0 votes
1 answer
561 views

Form validation using IIFE and closure [closed]

Problem statement Write an IIFE in which we have to validate the input provided from a form using closures. The form consists of name, email, phone, address in which name, email and phone number ...
overexchange's user avatar
  • 3,321
0 votes
2 answers
371 views

Write an anonymous function validating email address

Problem statement Write an anonymous function which take email address as parameter and returns true/false after validating the input parameter. Objective This assignment will help you ...
overexchange's user avatar
  • 3,321
2 votes
2 answers
2k views

Date formatters, lazily instantiated once

I would like to create two date formatters only on first call. In Objective-C I would use dispatch_once() but now working in Swift. Am I doing it correctly? My code ...
Szu's user avatar
  • 203
2 votes
1 answer
869 views

Reimplementation of Underscore _.throttle

I've built a function that mimics _.throttle (returns a new, throttled version of the passed function, that, when invoked repeatedly, will only actually call the original function at most once per ...
elledienne's user avatar
2 votes
0 answers
73 views

groupJoin() in Swift

I have created the following function in Swift for this library I am writing, which joins two arrays on a key (extracted using given closures), and then groups them by key in a ...
vrwim's user avatar
  • 161
3 votes
1 answer
2k views

removeAll(closure) in Swift

From this library I wrote, I have created this function in Swift, but I'm not happy with the implementation. Can anyone suggest a better way to do this? I really don't like changing counters inside <...
vrwim's user avatar
  • 161
2 votes
2 answers
701 views

Remove list of files from another list of files by their name

In groovy I have a list of files A and also another list of files B. I want to remove all the files from A that have a filename that also occurs in the list of files B. I wrote the following code: <...
valenterry's user avatar
2 votes
0 answers
202 views

Using my own Optional type with fold method in groovy

I have written an Optional type in groovy roughly based on java8s Optional. I implemented and use a fold method and would like to know if it can be improved. First, ...
valenterry's user avatar
3 votes
0 answers
119 views

Mangling MP3 collections

I'm writing a little tool for mangling MP3 collections and, as a challenge, I decided to write in in Hy, a dialect of Python that uses Lisp syntax. Like every good developer, I wrote out my list of ...
Elf Sternberg's user avatar
5 votes
1 answer
2k views

Closure as UIControlEvents handler

I'm expanding the functionality of this SO answer. Mainly I'm focusing on: You can add as many handlers as you want. You can remove (i.e. cancel) the handler. You can receive the ...
rintaro's user avatar
  • 151
0 votes
1 answer
240 views

Object with highest count in a Groovy list

I have a list of Parcel objects and am trying to write a closure that picks the parcel object with highest count: ...
OTUser's user avatar
  • 737
3 votes
1 answer
15k views

Sharing a database connection with multiple modules

I am building what essentially could be viewed as a glorified database wrapper as a Python package, where I'm having several classes and functions spread out into different modules. My current problem ...
user avatar
5 votes
2 answers
284 views

'Countdown' Numbers round - combine numbers arithmetically to reach a target

Countdown is a British gameshow where contestants compete in word and number challenges. During the numbers round, six numbers are chosen semi-randomly and the task is to combine them using addition, ...
Alex's user avatar
  • 339
0 votes
1 answer
542 views

Storing array inside closure

Is storing array values inside of a closure considered to be bad practice? I having been trying to decide if I should just use a standard object with key value pair. This is not the method I am ...
rancho's user avatar
  • 1
6 votes
1 answer
1k views

MVC Model validation callback

I have made a simple MVC framework of my own for my personal website to learn a thing or two about how this whole thing even works. I think I've got the idea, but there's one thing I'm not sure about. ...
user2374794's user avatar
3 votes
1 answer
60 views

Implementing an "Add Company" section in a project

I'm working on avoiding JavaScript that either directly modifies the DOM a ton, contains a ton of anonymous functions, or is encapsulated in giant ...
sir_thursday's user avatar
1 vote
1 answer
457 views

Node.js callback closure/scope

Is it a bad idea to use the notfound callback like this? If so, why not? (bad scope/closure? I'm a noob node.js dev.) Is it a bad idea to use the ...
Sleeper Smith's user avatar
6 votes
1 answer
382 views

Closures in C#, necessary or not?

I've written JavaScript for a while, but lately I've been writing a lot of C#. I wanted to write a method that could take the column name of a DataTable and return ...
wootscootinboogie's user avatar
2 votes
2 answers
357 views

Deeper abstracting of code into functions

I created a quiz using the module pattern. My code is as follows: ...
Ivan Durst's user avatar
2 votes
1 answer
229 views

Is my JavaScript OOP style correct?

I have done the following code for testing a web-service. Is my style correct for using OOP in JavaScript? ...
user avatar
4 votes
2 answers
104 views

Writing a user's profile image to disk using nested Node.JS closures

I am writing non-blocking code as it should be in Node.JS. If I want run function A after B I do it using callback. But I find this style guide: Use closures, but don't nest them. Otherwise your ...
Valery's user avatar
  • 361
1 vote
1 answer
438 views

JavaScript binding loss with setTimeout

My current task is to extend some JavaScript of a framework we are using. A pop-up should slide in, instead of just appear in the middle of the window. So I have overridden one of the framework's ...
Angelo.Hannes's user avatar
3 votes
1 answer
404 views

Correctly dealing with closures and modularity in Javascript

The code below is designed to deal with this situation: There is a Battle environment with Characters; these Characters obviously have certain characteristics and abilities, and they need to be ...
Sunyatasattva's user avatar
1 vote
3 answers
508 views

Cleanest way to close over a counter in CoffeeScript

I haven't spent too much time with CoffeeScript and am trying to have a simple counter: ...
speg's user avatar
  • 111
0 votes
3 answers
167 views

Processing image blocks using a task runner

This code works, but I'm sure it isn't written according to "best practices" for closures. On the other hand, at least it's intuitive to me... the taskRunner object runs functions on setTimeout, so ...
Aerik's user avatar
  • 103