Questions tagged [closures]

1

votes
1

answer
108

Views

Given a church encoded numeral as closure result from a CEK-machine, how to get back the number?

I have implemented the CEK-machine. Given a closure result from this algorithm and the knowledge that this closure is a Church-encoded numeral, what is the best way to print out the numeral? Using the following types: data Term = Var String | Abs String Term | App Term Term data Clos = Clos String T...
Labbekak
1

votes
2

answer
47

Views

Trying to understand what this JS function does and what its parameters are, can someone explain?

I'm reading through some code from a library I recently downloaded to do matrix math for WebGL. However, I'm having a hard time wrapping my head around what this function does. This comes from the glMatrix.js library. (function (global, factory) { typeof exports === 'object' && typeof module !== 'un...
Louis Aldous
1

votes
2

answer
55

Views

Process flow of a Closure example | Java 8

Was going through a closure example for the first time , but I'm having a hard time wrapping my head around the control flow. public class TestLambdaClosure { public static void main(String[] args) { int a= 10; int b=20; //doProcess(a, i-> System.out.println(i+b)); doProcess(a, new Process() { @Over...
Soumava Das
1

votes
1

answer
2.9k

Views

Closure in Velocity template macros

I've got a couple Velocity macros like this: #macro(Alpha) #set($p = 1) #@Beta() $p // 1 $bodyContent #end #end #macro(Beta $params) #set($p = 2) $p // 2 $bodyContent #end And I'm using them like so: #set($p = 0) #@Alpha(...) $p // 3 #end I believe this render...
gzak
1

votes
0

answer
39

Views

whats the difference between these patterns?

I use the following pattern all of the time: ;(function(ns){ var _str = 'hello'; ns.hello = function(){ console.log(str) } })(this.app = this.app || {}); app.hello(); // logs 'hello' to the console I'm trying to to do more OO type things and want to reuse this pattern. What I've come up with is the...
user1990962
1

votes
1

answer
27

Views

best way for add scope reference in function

hi guy what the best way for add scope obj reference to a function. my example , i have a super object. var SuperObj = new PIXI.sprite(); SuperObj.action = new subObj(); SuperObj.action.actionFunction = function(){// need to acces to SuperObj}; when i add function stuf to action, i need add a scoped...
jon
1

votes
1

answer
30

Views

Displaying which list item was clicked

I've been trying to wrap my head around closures in Javascript and how things are executed (or rather - in which order they are executed). Here is my example: lets say I have an unordered list of items and when I click on one of them I want to print out which item was clicked. I know the solution bu...
wasteland_
1

votes
3

answer
79

Views

How to reference this in closure

I am using PHP Version 7.1.1. In index.php of my MVC I create a route collection ($routes). To it I want to add a route group (with addGroup). This addition process relies on a closure (e.g. an anonymous function), passed as argument to addGroup. Inside of closure I am trying to reference the collec...
1

votes
0

answer
308

Views

System.Net.WebSockets.ClientWebSocket and closure in C# .Net Framework?

I'm new to Windows development, coming from macOS and iOS development. I've to work with System.Net.WebSockets.ClientWebSocket to implement some sort of private push notification service. I other languages I'm used to closure / blocks / functional programming to handle events like a WebSocket messag...
ygini
1

votes
0

answer
132

Views

Reference equals of two closures [duplicate]

This question already has an answer here: How do you test functions and closures for equality? 8 answers In Swift 3, what is a way to compare two closures? 2 answers I'm in an odd state where it would help me a lot if I could compare 2 closures to see if they are the same reference object. I trie...
Kevin DiTraglia
1

votes
0

answer
54

Views

Optimizing closure to include only variables used by spawnees

m := 1 n := 2 fun D { d := 3 e := 4 fun A { a := 5 fun C { c := 6 log d D() } C() D() } fun B { b := 7 ret A } B()() } Consider the above code, intentionally made hard to analyze 'by hand', in function B, function A returned, and at that moment it must capture the environment, that would be m n d e...
exebook
1

votes
3

answer
52

Views

JS closure application issue

function multFn(num){ return function(factor){ return factor*num } } var multFive=multFn(5) console.log(multFive(2)) // outputs 10 The question?: How does JS know that when I console.log(multFive(2)) that the 2 should be given to the factor parameter? I know that multFn will return a pointer to t...
Lams
1

votes
1

answer
55

Views

Laravel pagination not working with closure

I am trying to implement search functionality using Laravel. Here is my method that should return results of search: $keyword = request('q'); $posts = Post::where('deleted', false) ->where(function($q) use($keyword) { $q->where('title', 'like', '%' . $keyword . '%') ->orWhere('body', 'like', '%' . $...
Filip Egeric
1

votes
0

answer
21

Views

Detect outputs in closure compiler's gulp plugin

I am using gulp and google closure compiler e.g.: var compilerPackage = require('google-closure-compiler'); var closureCompiler = compilerPackage.gulp(); gulp.task('js-compile', function () { return closureCompiler({ js: './src/js/**.js', externs: compilerPackage.compiler.CONTRIB_PATH + '/externs/jq...
zed
1

votes
0

answer
88

Views

JS closure example explained

We have the following examples that I'm trying to understand: function makeFunctionArray(){ const arr=[]; for(var i=0;i
DCR
1

votes
0

answer
193

Views

How can I find all closures?

We've totally forgotten to capture self and its properties when referencing it within a closure. (Note: the compiler didn't warn us.) Now our application is full with strong reference cycles. To fix them, we have to add the capture list to each closure one-by-one. How can we find them all? I thought...
poima
1

votes
1

answer
80

Views

How to extract a closure into typealias?

I'm a bit lost with typealias, the following chunk of code works fine, which I would like to refactor into using typealias. NEVPNManager.shared().loadFromPreferences(completionHandler: vpnLoadHandler) func vpnLoadHandler(_: (Error?)) -> Void { } I would like to create a typealias instead: typealias...
Houman
1

votes
1

answer
103

Views

Frank copula parameter minimization

I am attempting to calculate the Frank copula parameter given Kendall's tau. Here is the problem I am attempting to solve numerically in R: So far, this is the code I have: copula
StatGuy45
1

votes
2

answer
156

Views

How to escape a value with a throw '() throws -> Bool' is not convertible to 'Bool'

I'm new in swift and I've been probably for more than an hour around this. I make a request to a webservice, and now i want to act according to the response code (200 Ok) or other but I can't understand the syntax for returning a value and throwing the exception. typealias ThrowableCallBack = () th...
João Serra
1

votes
0

answer
26

Views

Guaranteed to have no more than one closure object per local function?

I wonder if I can make multiple delegates from a single local function and rely on them to be all equal? In other words, if I make multiple delegates referencing the same local function, are they all equivalent? Or should I make sure to only create one if I need them to be equal? Of course, assuming...
relatively_random
1

votes
0

answer
45

Views

How to use local variables in closures during performDrop

I'm working my way through the Stanford CS193P Course on iTunesU. Stuck on this one part - There's one section where the instructor has the following comment: When a drop happens, you'll have to collect both the aspect ratio (from the UIImage) and the URL before you can add an item. You could do th...
Eduard Lev
1

votes
0

answer
91

Views

Zend Framework Serialization of Closure not allowed

I'm having difficulty finding an answer to this issue specifically for zend framework on the internet. I'm getting an error of Serialization of Closure is not allowed. The issue I have is that its just the zend internal code, not any of my code. I know this because i used the zend skeleton applicati...
jkushner
1

votes
1

answer
71

Views

Spock reusing generic closure

I have this code in Spock: then: 1 * dao.getByValue(Something.ONE, _ as String) >> {Something smth, String value -> return createSomething(smth).withValue(value).build() } It doesn't look exactly like that, but you get the point. I want to return an object based on arguments passed to the method, in...
Shadov
1

votes
2

answer
37

Views

Re:Javascript closure. How the variable inside of the mother object become the same as the mother object?

I have a question about public variable and closure. In the following code, how come the 'temp' variable inside of the mother object can be used like the mother object? For example, in the following code, if you type console.log(pizzaA.getToppings()); You can get '3' A bit confusing because if y...
1

votes
1

answer
70

Views

How can Java versions older than 8 not support Closures? [duplicate]

This question already has an answer here: Closure in Java 7 [closed] 7 answers I read that Java 8 supports Closures, but I just wanted to know that as any function inside a class can access a global variable, so how can Java previously didn't support Closures ?? See below example. public class Hell...
Sudhanshu Gaur
1

votes
0

answer
25

Views

Why am i not able to see “Closure” in chrome dev tool?

BTW this is the chunk i have used to check: function outerFunction () { const outer = `I see the outer variable!` function innerFunction() { console.log(outer) } return innerFunction } outerFunction()(); Do i need to enable something in settings or any extension is required or whatever. Any help wou...
indar
1

votes
0

answer
33

Views

What mean *@param array|string|\Closure $middleware

I'm in Laravel 5.6, Controller.php abstract class with Illuminate\Routing namespace and I would like know what kind of parameter is specified in line 19 to declared method in line 23. I know what closure and middleware are but I don't know what this line means. 16 /* 17 * Register middleware on the...
Fahico98
1

votes
0

answer
30

Views

what's the diff. between the nested and Closure function in python

can you please tell me the diff. between the nested and Closure function?? With simple examples?? Thank you! I can't not understand perfectly the difference.
Enami
1

votes
1

answer
261

Views

MVVM - using closures to bind a ViewModel with a DataSource: capture list needed?

I have a viewController keeping a (strong) reference to its viewModel lazy private var viewModel: ListViewModel = { return ListViewModel() }() override func viewDidLoad() { super.viewDidLoad() initViewModel() } private func initViewModel() { viewModel.onModelChange = { [weak self] () in DispatchQueu...
AppsDev
1

votes
1

answer
31

Views

The below closure example not returning properly

I am trying to implement the below javascript code using closures and I get the output like this. Here the whole function is printed in the browser `function apple(){ var test1 = ' '+ ' '+ ' This is apple'+ ' '+ ' '; return test1; }` Desired output: I only want 'This is apple' to be printed and not...
Jeril CJ
1

votes
1

answer
38

Views

What is the difference between retain cycle in these closures? Standard closure vs object initialized closure

I understand the typical retain cycle with a closure when referencing self within the closure, but is there a reason the label does not produce a retain cycle? I've tested commenting the testClosure out vs commenting the label out and only the testClosure produced a retain cycle class TargetVie...
jo1717a
1

votes
1

answer
33

Views

Remove array index if instance object value and array value are the same

I have an array named var geoLocList = [GeolocationModel]() and an object named var geoLocs = GeolocationModel() The values of geoLocs appended to geoLocList. The problem is, I'm trying to replace index in geoLocList with the latest value of geoLocs according to the same id. I'm still a newbie in i...
1

votes
3

answer
55

Views

jQuery Closure in For Loop [duplicate]

This question already has an answer here: JavaScript closure inside loops – simple practical example 42 answers I am familiar with JavaScript closures. I discovered closure issues while working on my own projects, years ago. I have also included the famous for-loop-button question while interview...
Dev
1

votes
2

answer
23

Views

Set class/prototype variable from jQuery function

Can somebody help me to understand how in this situation I can push values from $.each function into this.books array? let Library = function ( name ) { this.name = name; this.books = Array(); }; Library.prototype.addBook = function ( book ) { if( Array.isArray( book ) ) { $.each(book, function() {...
1

votes
0

answer
106

Views

What is the Javascript [[Environment]] Property?

I am reading Secrets of the Javascript Ninja and am trying to figure out where the closure variables of a function are stored.[[Environment]] property available on the function identifier: Whenever a function is created, a reference to the lexical environment in which the function was created is sto...
VSO
1

votes
3

answer
32

Views

Javascript Closures. Lexical vs function context. [duplicate]

This question already has an answer here: What is the scope of variables in JavaScript? 25 answers My question is why the following code prints 1 instead of 500; When the callback is invoked and it doesnt find the variable count, doesnt it first looked at the context of func1 ? function func1(cb)...
sco
1

votes
0

answer
274

Views

Programmatically accessing [[Scopes]] (closure variables) through Chrome DevTools API or panel? (Not at breakpoint)

In 2014, it was not possible for JS code to access variables inside a closure from outside the closure. Since then, Chrome's internals have changed to use [[Scopes]] instead of Closure. Is it now (2018) possible for Chrome DevTools to read [[Scopes]] programmatically? If so, is there an existing...
cxw
1

votes
0

answer
39

Views

JSON issue with looping inside closure

I have to pull JSON data from a few different website endpoints in order to get a full view of the objects I'm working with. In order to do so, I am using the downloadJSON code below. This works OK, except whenever I create a for loop within downloadJSON(){ }, the amount of iterations in the loop...
greg42627
1

votes
0

answer
28

Views

Unable to save data from anonymous function [duplicate]

This question already has an answer here: How do I convert an existing callback API to promises? 18 answers How do I return the response from an asynchronous call? 34 answers I'm currently refactoring some code, and I'm running into a problem. I am trying to return the value from inside an anonymo...
Ollie
1

votes
0

answer
78

Views

Are closures implemented as classes in the Swift compiler? Is a common closure a subclass of a throwing closure?

In Swift, the only reference type available to a programmer is a class. We know that closures are objects as they store references to variables captured in the parent scope. Closures are also reference types, according to Apple's Swift Programming Language Guide. You can see that assigning a closure...
Max Desiatov

View additional questions