closures

Undefined Behavior with the C++0x Closure: I

我只是一个虾纸丫 提交于 2019-12-19 20:37:24
问题 Consider the example: #include <iostream> #include <functional> // std::function #include <vector> // std::vector #include <algorithm> // std::for_each int main(){ auto adder = [](int x) { return [&](int y) { return x+=y; }; }; std::vector < std::function<int(int)> > vec; vec.push_back(adder(1)); vec.push_back(adder(10)); std::for_each(vec.begin(), vec.end(), [](std::function<int(int)> f){std::cout << f(33) << " ";}); std::cout << std::endl; } One expects the integers 34 and 43 43 and 76 ,

Lambda expressions and higher-order functions

烈酒焚心 提交于 2019-12-19 17:38:07
问题 How can I write with Java 8 with closures support a method that take as argument a function and return function as value? 回答1: In Java Lambda API the main class is java.util.function.Function. You can use a reference to this interface in the same way as you would do with all other references: create that as variable, return it as a result of computation and so on. Here is quite simple example which might help you: public class HigherOrder { public static void main(String[] args) { Function

Lambda expressions and higher-order functions

穿精又带淫゛_ 提交于 2019-12-19 17:37:15
问题 How can I write with Java 8 with closures support a method that take as argument a function and return function as value? 回答1: In Java Lambda API the main class is java.util.function.Function. You can use a reference to this interface in the same way as you would do with all other references: create that as variable, return it as a result of computation and so on. Here is quite simple example which might help you: public class HigherOrder { public static void main(String[] args) { Function

How to understand closures in Javascript? [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 12:27:41
问题 This question already has answers here : How do JavaScript closures work? (86 answers) Closed 6 years ago . How can one understand closures in Javascript? In general terms, a closure is a function bound to one or more external variables. When it is called, the function is able to access these variables. In JavaScript, closures are often implemented when functions are declared inside another function. The inner function accesses variables of the parent one, even after the parent function has

Closure for setInterval function in javascript

一个人想着一个人 提交于 2019-12-19 09:54:14
问题 How to use setInterval without using global variables? I'd prefer to wrap all variables of function invoked by setInerval in some kind of closure, like so: var wrap = function (f){ var local1, local2, ...; return function () { return f(); } } This doesn't work, but the idea is that I'd pass wrap(f) instead of f to setInterval , so that locals for f are nicely wrapped and don't pollute the global scope. 回答1: javascript don't have dynamic binding.(except this keyword) use anonymous function can

Capturing a struct reference in a closure doesn't allow mutations to occur

我是研究僧i 提交于 2019-12-19 09:47:38
问题 I am trying to see if I can use structs for my model and was trying this. When I call vm.testClosure() , it does not change the value of x and I am not sure why. struct Model { var x = 10.0 } var m = Model() class ViewModel { let testClosure:() -> () init(inout model: Model) { testClosure = { () -> () in model.x = 30.5 } } } var vm = ViewModel(model:&m) m.x vm.testClosure() m.x 回答1: An inout argument isn't a reference to a value type – it's simply a shadow copy of that value type, that is

how to throw errors in a closure in swift?

六月ゝ 毕业季﹏ 提交于 2019-12-19 09:27:38
问题 Please look at the following code: override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler: { (action : UITableViewRowAction, indexPath : NSIndexPath) -> Void in if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext{ let restaurantToDelete = self

Function count calls

纵然是瞬间 提交于 2019-12-19 07:52:48
问题 I'm a beginner with JavaScript so please be patient =) I am trying to write a function that counts the number of times it is called. What I have so far is a function with a counter that is incremented explicitly: var increment = function () { var i = 0; this.inc = function () {i += 1;}; this.get = function () {return i;}; }; var ob = new increment(); ob.inc(); ob.inc(); alert(ob.get()); But I'm wondering how to call only ob(); , so the function could increment calls made to itself

dealing with loops in javascript, only last item gets affected? [duplicate]

寵の児 提交于 2019-12-19 07:37:21
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 5 years ago . Im working with the gm npm module that deals with image manipulation. and i have this code. for(i=0;i < 4;i++){ gm("www/img/" + image[i]).crop(550, 406, 0, 0).write(function(err) { console.log(this.outname + " created :: " + arguments[3]); //success }); } this loop is meant to loop through the images array and crop each photo, but it only crops the last one. i

What is Closures/Lambda in PHP or Javascript in layman terms? [duplicate]

為{幸葍}努か 提交于 2019-12-19 06:58:13
问题 This question already has answers here : What is the difference between a 'closure' and a 'lambda'? (11 answers) Closed 5 years ago . What are Closures/Lambda in PHP or JavaScript in layman terms? An Example would be great to aid my understanding. I am assumning Lambda and Closures are the same thing? 回答1: SO already has the answers: What is a lambda (function)? How do JavaScript closures work? 回答2: A lambda is an anonymous function. A closure is a function that carries its scope with it. My