closures

What is the benefit of wrapping angular controller/service/factory declarations in an anonymous function [duplicate]

浪子不回头ぞ 提交于 2019-12-11 20:35:47
问题 This question already has answers here : What is the purpose of a self executing function in javascript? (16 answers) Closed 4 years ago . I've seen a few developers tout 'best practices' when wrapping angular components in anonymous functions. For example: (function(){ angular.controller('MyCtrl', [function(){ // ... controller logic }]); })() What is the benefit of wrapping angularjs components in anonymous functions, if at all any? 回答1: This is an immediately invoked function. (function(){

Using Alloy functions in a recursive way through transitive closures

眉间皱痕 提交于 2019-12-11 20:19:35
问题 I am doing a model in Alloy to represent a subset of Java language. Below we have some elements of this model: sig Method { id : one MethodId, param: lone Type, return: one Type, acc: lone Accessibility, b: one Block } abstract sig Expression {} abstract sig StatementExpression extends Expression {} sig MethodInvocation extends StatementExpression{ pExp: lone PrimaryExpression, id_methodInvoked: one Method, param: lone Type } sig Block { statements: seq StatementExpression } pred

Closure as function parameter “cannot infer an appropriate lifetime due to conflicting requirements”

╄→гoц情女王★ 提交于 2019-12-11 19:26:36
问题 I am trying to use a closure as function parameter: fn foo(f: Box<Fn() -> bool>) -> bool { f() } fn main() { let bar = 42; foo(Box::new(|| bar != 42)); } but I get this lifetime error: src/main.rs:7:24: 7:36 error: cannot infer an appropriate lifetime due to conflicting requirements src/main.rs:7 let n = foo(Box::new(|| bar != 42)); ^~~~~~~~~~~~ src/main.rs:7:15: 7:23 note: first, the lifetime cannot outlive the expression at 7:14... src/main.rs:7 let n = foo(Box::new(|| bar != 42)); ^~~~~~~~

How can we pass the closures to any ViewController in the app flow?

心不动则不痛 提交于 2019-12-11 18:42:20
问题 In View Controller A, var completionBlock: (((String) -> ()))? = nil & I am calling the completion block like(ViewController A): if let block = completionBlock { block("block data to pass") } I don't want to pass the completion data to ViewController B, instead i want to pass to ViewController C which is presenting from ViewController B. In simple words, i want to pass the closure data to from ViewController A to ViewController C.I know how to pass data with delegates, just curious with

XmlSlurper in Groovy Script — Insert nodes to GpathResult using external closures

跟風遠走 提交于 2019-12-11 18:02:56
问题 I have a problem with the below scenario: -- I have a GPathResult "body" to which I want to append some more xml (nodes and children) -- Some parts are common so I am trying to have them kept in an outer closure "commonNode" I can insert wherever I need // some more code here to get body def commonNode = { return { node2() { child("childValue") } } } body.appendNode( { node1("value1") commonNode() node3("value3") } ) What I want to get after I would call XmlUtil.serialize(body) is this: ...

Create swift closure and pass it to objective-c class as completion block

≡放荡痞女 提交于 2019-12-11 17:37:13
问题 Now I'm adding Swift code on objective-C based iOS application. But I have a problem in calling the objective-C method from Swift side. I'd like to get and draw image on UIImageView in View or ViewController which is written with Swift. On the other hand my wrapper class of Photos Library is written with objective-C. My wrapper class of Photos Library is below. ・MyPhotoLibraryMgr.h typedef void (^AppPhotoLibraryGetImageCompletion)(UIImage *image, NSError *error); - (void)getImageWithTarget:

Accessing variables of parent function in Javascript event handlers

夙愿已清 提交于 2019-12-11 17:15:42
问题 I have an event handler which is bound to a 'change' event. The problem is, functions within that function cannot access any elements of the parent function Process.prototype.handleCheckboxChange = function(event) { var rgbs = [] $(':checked').each(function(index,element) { var color = [0,3,4]; rgbs.push(color); }) } I have been reading all around the place about closures, but everything I have seen seems to indicate that an inner function should be able to access its parents local variables,

ASP.NET Core MvcOptions dependency injection without modified closure?

风流意气都作罢 提交于 2019-12-11 16:59:46
问题 In my ASP.NET Core project, I currently inject an IFilterMetadata dependency into MvcOptions in the ConfigureServices method in the following way: public override IServiceProvider ConfigureServices(IServiceCollection services) { services.AddProjectSpecificStuff(); IExceptionFilter exceptionFilter = null; services.AddMvc(options => { options.Filters.Add(exceptionFilter); }); var provider = base.ConfigureServices(services); exceptionFilter = provider.GetService<IExceptionFilter>(); return

How to create closure (protect globals) in an animation loop?

ぃ、小莉子 提交于 2019-12-11 16:14:06
问题 I'm writing and animation loop using three.js and all the examples (mrdoob, stemkoski) I see online use unprotected globals at the beginning of the script. I tried to enclose these in the init() function and then pass them as arguments through the animation loop. However, the renderer is coming up undefined. I'm not sure what I'm missing below. My primary question is how to understand best practice for setting up an animation loop with good closure (protecting variables that would otherwise

JavaScript closure inside loops – simple practical example

萝らか妹 提交于 2019-12-11 15:52:17
问题 var funcs = []; // let's create 3 functions for (var i = 0; i < 3; i++) { // and store them in funcs funcs[i] = function() { // each should log its value. console.log("My value: " + i); }; } for (var j = 0; j < 3; j++) { // and now let's run each one to see funcs[j](); } It outputs this: My value: 3 My value: 3 My value: 3 Whereas I'd like it to output: My value: 0 My value: 1 My value: 2 The same problem occurs when the delay in running the function is caused by using event listeners: var