closures

Are closures a violation of the functional programming paradigm?

六眼飞鱼酱① 提交于 2019-12-02 18:07:31
Functional programming "avoids state and mutable data". Closures hide state by binding their lexical environment and are thus closed over their free variables . How is Haskell purely-functional if it supports closures? Don't they break referential transparency? In Haskell, closures have free variables in the same way that in math you can write f x = x^2 - it doesn't mutate state. I would say that Haskell avoids mutable state. Closures are not a violation because all bindings in Haskell are immutable. What closures really mean is that a lambda with free variables doesn't denote one unique

Getting data out of completionHandler in Swift in NSURLConnection

夙愿已清 提交于 2019-12-02 17:42:20
I am trying to write a function that will execute an asynchronous GET request, and return the response (as any data type, but here it is as NSData). This question is based on: How to use NSURLConnection completionHandler with swift func getAsynchData() -> NSData { var dataOutput : NSData let url:NSURL = NSURL(string:"some url") let request:NSURLRequest = NSURLRequest(URL:url) let queue:NSOperationQueue = NSOperationQueue() NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in /* this next line

extjs - how correctly call a controller method from another controller or closure

删除回忆录丶 提交于 2019-12-02 17:40:41
I'm new to extjs and I'm using the MVC architecture. When my application references a method of a controller, I do it that way (in MyApp.Application ): Mb.app.getController('Main').myMethod(); It is already long, but I think this is the way to do. When a controller calls it's own method in a closure, I was led to use this code (in MyApp.controller.Main : controllerMethodOne: function(){ Ext.Ajax.request({ url: ..., params: ..., success: (function(response){ list = Ext.JSON.decode(response.responseText); list.forEach(function(item){ storeMenu.add( Ext.create('Ext.menu.Item', { text: item.text,

Why are there memory allocations when calling a func

天大地大妈咪最大 提交于 2019-12-02 17:24:45
I have the following program which construct a local Func from two static methods. But strangely, when I profile the program, it allocated close to a million Func objects. Why invoking Func object is also creating Func instances? public static class Utils { public static bool ComparerFunc(long thisTicks, long thatTicks) { return thisTicks < thatTicks; } public static int Foo(Guid[] guids, Func<long, long, bool> comparerFunc) { bool a = comparerFunc(1, 2); return 0; } } class Program { static void Main(string[] args) { Func<Guid[], int> func = x => Utils.Foo(x, Utils.ComparerFunc); var guids =

How do I create a memory leak in JavaScript?

北城以北 提交于 2019-12-02 17:09:25
I would like to understand what kind of code causes memory leaks in JavaScript and created the script below. However, when I run the script in Safari 6.0.4 on OS X the memory consumption shown in the Activity Monitor does not really increase. Is something wrong with my script or is this no longer an issue with modern browsers? <html> <body> </body> <script> var i, el; function attachAlert(element) { element.onclick = function() { alert(element.innerHTML); }; } for (i = 0; i < 1000000; i++) { el = document.createElement('div'); el.innerHTML = i; attachAlert(el); } </script> </html> The script

What is the advantage of using a function over loops?

我们两清 提交于 2019-12-02 17:07:50
问题 It seems that functional iterators are replacing the use of for loops in JS. What is the advantage of passing a function such as map or reduce compared to a for/while loop? var numbers = [1, 4, 9]; var doubles = numbers.map(function(num) { return num * 2; }); var doubles = []; for (i = 0; i < numbers.length; i++) { doubles[i] = numbers[i] * 2; } 回答1: I have no idea why you would call the use of map a "closure". Closures are something else entirely. map is a higher-order function--defined as a

I cannot understand the example of a closure

纵然是瞬间 提交于 2019-12-02 16:46:24
问题 Please me to understand the closures. Why does the counter work in the first variant, but in the second version there is not? var counter = (function(){ var count=0; return function(){ return count++; } }()); console.log(counter()); console.log(counter()); console.log(counter()); The counter outputs 0,1,2 var counter = function(){ var count=0; return function(){ return count++; } }; console.log(counter()()); console.log(counter()()); console.log(counter()()); The counter outputs 0,0,0 What is

Scala advantages after Java having closures [closed]

北城余情 提交于 2019-12-02 16:19:09
With closures being added to Java, what is Scala's advantage over Java as a language choice? Can someone elaborate on any advantages? Tom Crockett Apart from closures (which Java doesn't appear all that close to having), here's a list of features in Scala that are missing from Java. I'll omit libraries here and concentrate on the features of the language itself. This is not comprehensive by any means, but I think it contains the big ticket items. Implicit parameters / conversions Pattern matching, case classes Type inferencing (some) Higher-kinded types (abstraction over type constructors)

How golang's “defer” capture closure's parameter?

喜欢而已 提交于 2019-12-02 16:14:46
Here is my code ( run ): package main import "fmt" func main() { var whatever [5]struct{} for i := range whatever { fmt.Println(i) } // part 1 for i := range whatever { defer func() { fmt.Println(i) }() } // part 2 for i := range whatever { defer func(n int) { fmt.Println(n) }(i) } // part 3 } Output: 0 1 2 3 4 4 3 2 1 0 4 4 4 4 4 Question: What's the difference between part 2 & part 3? Why part 2 output "44444" instead of "43210"? The 'part 2' closure captures the variable 'i'. When the code in the closure (later) executes, the variable 'i' has the value which it had in the last iteration of

Closures in PHP… what, precisely, are they and when would you need to use them?

心已入冬 提交于 2019-12-02 15:45:33
So I'm programming along in a nice, up to date, object oriented fashion. I regularly make use of the various aspects of OOP that PHP implements but I am wondering when might I need to use closures. Any experts out there that can shed some light on when it would be useful to implement closures? dirtside PHP will support closures natively in 5.3. A closure is good when you want a local function that's only used for some small, specific purpose. The RFC for closures give a good example: function replace_spaces ($text) { $replacement = function ($matches) { return str_replace ($matches[1], ' ', '