closures

Compose example in Paul Graham's ANSI Common Lisp

百般思念 提交于 2019-12-03 05:41:36
Can anybody explain an example in Paul Graham's ANSI Common Lisp page 110? The example try to explain the use &rest and lambda to create functional programming facilities. One of them is a function to compose functional arguments. I cannot find anything explaining how it worked. The code is as follows: (defun compose (&rest fns) (destructuring-bind (fn1 . rest) (reverse fns) #'(lambda (&rest args) (reduce #'(lambda (v f) (funcall f v)) rest :initial-value (apply fn1 args))))) The usage is: (mapcar (compose #'list #'round #'sqrt) '(4 9 16 25)) The output is: ((2) (3) (4) (5)) Line 2 and 6 look

Can someone explain Anonymous methods to me?

巧了我就是萌 提交于 2019-12-03 05:36:15
问题 Delphi 2009, among some cool stuff, has also just got Anonymous methods. I've seen the examples, and the blog posts regarding anonymous methods, but I don't get them yet. Can someone explain why I should be excited? 回答1: Just think of typical callback code where you need to have data available to the callback. Often this data is needed for the callback only , yet you have to jump through a number of hoops to get it there without having to resign to un-OOP-friendly practices like global

What does a block of code in parentheses mean in JavaScript/jQuery? [duplicate]

允我心安 提交于 2019-12-03 05:24:08
This question already has answers here : What does (function($) {})(jQuery); mean? (6 answers) Possible Duplicate: What does (function($) {})(jQuery); mean? I've seen a lot of jQuery code with the following sort of syntax, but I don't really understand what it means. It shows up in this answer and this answer on a question about code organization. Both talk about namespacing, so I'm guessing that's what it accomplishes. var foo = (function () { var someVar; function someFunc() { return true; } })(); Is this for namespacing, and how does it work? Sometimes there is a name (the namespace?) in

Define a method that is a closure in Ruby

雨燕双飞 提交于 2019-12-03 05:17:51
问题 I'm re-defining a method in an object in ruby and I need the new method to be a closure. For example: def mess_it_up(o) x = "blah blah" def o.to_s puts x # Wrong! x doesn't exists here, a method is not a closure end end Now if I define a Proc, it is a closure: def mess_it_up(o) x = "blah blah" xp = Proc.new {|| puts x # This works end # but how do I set it to o.to_s. def o.to_s xp.call # same problem as before end end Any ideas how to do it? Thanks. 回答1: This works (tested in irb): NOTE: This

What is so special about closures?

你说的曾经没有我的故事 提交于 2019-12-03 05:17:27
问题 I've been reading this article about closures in which they say: "all the plumbing is automatic" the compiler "creates a wrapper class" and "extends the life of the variables" "you can use local variables without worry" the .NET compiler takes care of the plumbing for you, etc. So I made an example based on their code and to me, it seems as though closures just act similarly to regular named methods which also "take care of the local variables without worry" and in which "all the plumbing is

iOS Swift: Closures (Callbacks) versus Delegates, when to use which? [closed]

允我心安 提交于 2019-12-03 05:01:08
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . Personally I prefer callback over delegate in Swift for simple logical correlations, because it's pretty straight-forward and easy to understand. At the same time, some prefers delegate, since delegation is a popular pattern in other languages, such as C#. There are some

Swift use selector argument like a closure

谁说胖子不能爱 提交于 2019-12-03 04:46:08
I was just wondering if it was possible to pass a function to a button action (which is usually a selector). For example, normally I'd say: UIBarButtonItem(title: "Press", style: .Done, target: self, action: "functionToCall") func functionToCall() { // Do something } But I was wondering if it's possible to do something like: UIBarButtonItem(title: "Press", style: .Done, target: self, action: { // Do Something }) Reason I want to do this is because my function is super simple and it seems like it would be neater and more Swift-like what with the emphasis they are placing on closures. Here's an

Getting data out of completionHandler in Swift in NSURLConnection

时间秒杀一切 提交于 2019-12-03 04:32:36
问题 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,

Anonymous functions in WordPress hooks

本小妞迷上赌 提交于 2019-12-03 04:27:23
WordPress hooks can be used in two ways: using callback function name and appropriate function add_action( 'action_name', 'callback_function_name' ); function callback_function_name() { // do something } using anonymous function (closure) add_action( 'action_name', function() { // do something } ); Is there any difference for WordPress what way to use? What is prefered way and why? TeeDeJee The disadvantage of the anonymous function is that you're not able to remove the action with remove_action . Important: To remove a hook, the $function_to_remove and $priority arguments must match when the

Are closures a violation of the functional programming paradigm?

↘锁芯ラ 提交于 2019-12-03 04:15:17
问题 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? 回答1: 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. 回答2: Closures are not a violation because all bindings in