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
1 vote
2 answers
66 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/...
user avatar
2 votes
0 answers
21 views

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

How can this code be improved by "closuring" of functions here? ...
user avatar
  • 21
2 votes
1 answer
159 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 ...
user avatar
1 vote
0 answers
99 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 ...
user avatar
  • 29
2 votes
0 answers
43 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 ...
user avatar
2 votes
0 answers
55 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 ...
user avatar
  • 143
2 votes
1 answer
87 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 ...
user avatar
  • 91
4 votes
1 answer
636 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: ...
user avatar
5 votes
1 answer
1k 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,...
user avatar
  • 1,360
3 votes
1 answer
335 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 ...
user avatar
  • 33
2 votes
1 answer
125 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 ...
user avatar
-3 votes
1 answer
48 views

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

...
user avatar
  • 97
11 votes
2 answers
1k 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 ...
user avatar
1 vote
2 answers
90 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 ...
user avatar
  • 111
2 votes
1 answer
273 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 ...
user avatar
0 votes
1 answer
399 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: ...
user avatar
  • 171
2 votes
0 answers
42 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 ...
user avatar
  • 875
4 votes
2 answers
71 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 ...
user avatar
2 votes
1 answer
184 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 ...
user avatar
  • 233
2 votes
1 answer
721 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 ...
user avatar
3 votes
1 answer
663 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 ...
user avatar
5 votes
2 answers
152 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 ...
user avatar
3 votes
2 answers
50 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 ...
user avatar
  • 161
2 votes
1 answer
72 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://...
user avatar
0 votes
1 answer
508 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 ...
user avatar
  • 3,329
0 votes
2 answers
291 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 ...
user avatar
  • 3,329
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 ...
user avatar
  • 203
1 vote
0 answers
75 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://...
user avatar
2 votes
1 answer
711 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 ...
user avatar
1 vote
0 answers
115 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 ...
user avatar
  • 239
2 votes
0 answers
69 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 ...
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 <...
user avatar
  • 161
1 vote
0 answers
1k 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 ...
user avatar
2 votes
2 answers
644 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: <...
user avatar
2 votes
0 answers
198 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, ...
user avatar
3 votes
0 answers
118 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 ...
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 ...
user avatar
  • 151
0 votes
1 answer
227 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: ...
user avatar
  • 717
3 votes
1 answer
14k 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
  • 233
5 votes
2 answers
273 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, ...
user avatar
  • 339
0 votes
1 answer
520 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 ...
user avatar
  • 1
6 votes
1 answer
999 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. ...
user avatar
3 votes
1 answer
57 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 ...
user avatar
1 vote
1 answer
454 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 ...
user avatar
6 votes
1 answer
371 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 ...
user avatar
2 votes
2 answers
351 views

Deeper abstracting of code into functions

I created a quiz using the module pattern. My code is as follows: ...
user avatar
2 votes
1 answer
223 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
100 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 ...
user avatar
  • 361
1 vote
1 answer
419 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 ...
user avatar
3 votes
1 answer
397 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 ...
user avatar