Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

1
vote
1answer
20 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: ...
3
votes
1answer
35 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,...
2
votes
1answer
57 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 ...
0
votes
0answers
42 views

Two different blocks with reverse direction in single method in Swift

So I've been trying to refactor my existing code for loading data from server and synching it to the local DB. Initially there were multiple methods with duplicated pattern, then I refactored them ...
1
vote
1answer
89 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 ...
10
votes
2answers
797 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 ...
1
vote
2answers
72 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 ...
2
votes
1answer
186 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 ...
0
votes
1answer
238 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: ...
2
votes
0answers
31 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 ...
4
votes
2answers
69 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 ...
2
votes
1answer
139 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 ...
3
votes
1answer
580 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 ...
3
votes
1answer
491 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 ...
5
votes
2answers
147 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 ...
3
votes
2answers
48 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 ...
0
votes
1answer
321 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 ...
0
votes
2answers
213 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 ...
2
votes
2answers
916 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 ...
1
vote
0answers
69 views

Web Scraping class schedules

I just want some advice regarding code style and best practices. Also I want to know if I'm using constant declaration and closures properly. This code is used to get the data from here http://...
2
votes
1answer
406 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 ...
1
vote
0answers
107 views

Javascript Class System

Warning: The code snippet does run two alerts, just so you know. I wrote a javascript class system with the idea of making declaration simple and clean and instantiation even simpler, along with ...
2
votes
0answers
65 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 ...
3
votes
1answer
1k 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 <...
1
vote
0answers
775 views

JavaScript nesting XMLHttp requests and in a loop

I am new to JavaScript, especially the wonders of async and closures and would like some help on the attached code. It is in a Chrome extension and retrieves a list of users albums with the Picasa Web ...
2
votes
2answers
399 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: <...
2
votes
0answers
192 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, ...
3
votes
0answers
111 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 ...
5
votes
1answer
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 ...
0
votes
1answer
194 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: ...
3
votes
1answer
10k 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 ...
5
votes
2answers
256 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, ...
0
votes
1answer
337 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 ...
6
votes
1answer
914 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. ...
3
votes
1answer
54 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 ...
1
vote
1answer
411 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 ...
5
votes
1answer
328 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 ...
2
votes
2answers
345 views

Deeper abstracting of code into functions

I created a quiz using the module pattern. My code is as follows: ...
2
votes
1answer
215 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? ...
4
votes
2answers
96 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 ...
1
vote
1answer
348 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 ...
3
votes
1answer
378 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 ...
1
vote
3answers
454 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: ...
0
votes
3answers
155 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 ...
4
votes
2answers
562 views

JavaScript closure: code trivia to keep the event handler separate

Note: This was originally posted at SO, and as per the recommendation moving to this form. In my effort to learn JS on the backdrop of SVG development, I got lot of help from SO so far. The answer ...