closures

Is there any difference between closure in Scheme and usual closure in other languages?

橙三吉。 提交于 2019-12-05 06:51:29
I'm studying SICP right now. And I found the definition of closure in SICP is (maybe) different from closure definition in other languages. Here's what SICP says: The ability to create pairs whose elements are pairs is the essence of list structure's importance as a representational tool. We refer to this ability as the closure property of cons. In general, an operation for combining data objects satisfies the closure property if the results of combining things with that operation can themselves be combined using the same operation. Here closure is more close to closure in Mathematics I think,

Variable in a function

白昼怎懂夜的黑 提交于 2019-12-05 06:29:40
I have see the following code... The first call of (next-num) returns 1 , and the second returns 2 . (define next-num (let ((num 0)) (lambda () (set! num (+ num 1)) num))) (next-num) ; 1 (next-num) ; 2 What I can not understand is... num is created by let inside next-num , it is kind of a local variable... How does scheme know that each time next-num is called, the value of num is not erased by let ((num 0)) ; How does scheme know that it is always the same num that we modify whenever next-num is called? It seems that num is both local and static... How can we define a local variable, but not

Javascript Closures Explanation [closed]

99封情书 提交于 2019-12-05 06:15:36
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center . Closed 9 years ago . Question: There seem to be many benefits to Closures, but what are the negatives? Additionally, is my understanding of Closures correct? Finally, once closures are created, can they be destroyed? I've been reading a little bit about Javascript Closures. I hope someone a little more knowledgeable will guide my

Generic completion handler in Swift

允我心安 提交于 2019-12-05 06:14:51
问题 I have a method which has a method named performRequest() . It takes a JSONRequest parameter. JSONRequest looks something like this: public typealias JSONCompletionHandler = ([Entity]?, NSError?) -> Void public class JSONRequest: Request { public var completionHandler: JSONCompletionHandler public var endPoint: String } And performRequest() looks like this: public func performJSONRequest<T where T: Entity>(jsonRequest: JSONRequest, _: Type) { // Make a request which returns a data object var

Limitations of Java Anonymous Classes compared to Objective-C Blocks

白昼怎懂夜的黑 提交于 2019-12-05 06:07:54
I'm just starting to wrap my head around first order functions and closures after discovering blocks in Objective-C. Java is another language where I've heard about closures (or lack thereof) and how anonymous classes make up for this somewhat. I can definitely see the advantages of closures as blocks in Objective-C, but what are the limitations of anonymous Java classes? To what extent do they 'somewhat' make up for the lack of true closures? Java anonymous classes are really, really wordy. Apart from the vast amounts of boilerplate that you need just to define them, some of Java's design

JavaScript: self-calling function returns a closure. What is it for?

南笙酒味 提交于 2019-12-05 06:06:04
Studying one JavaScript library I found following construction: theMethod: function () { var m1 = new SomeClass(); return function (theParameter) { this.someMethod(); m1.methodCall(this.someField1); this.someField2 = 'some value'; } }() theMethod is called as follows: c.theMethod(paramValue); What did the author want to say with this declaration? Why not to use such declaration: theMethod: function (theParameter) { var m1 = new SomeClass(); this.someMethod(); m1.methodCall(this.someField1); this.someField2 = 'some value'; } It's for encapsulation . m1 is hidden to other methods and from

How to create a vector of boxed closures in Rust?

瘦欲@ 提交于 2019-12-05 05:26:17
Previously a question was asked about creating an array of functions where the functions returned integers from a range. The final solution was to do a map/collect into a Vec<_> . I have a similar yet different situation where I have closures with the same signature but different implementations. I tried this: let xs: Vec<_> = vec![ move |(x, y)| (y, x), move |(x, y)| (1 - y, 1 - x), ]; The error I get back: error[E0308]: mismatched types --> src/main.rs:4:9 | 4 | move |(x, y)| (1 - y, 1 - x), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected closure, found a different closure | = note: expected type `

Javascript closures and recursion

て烟熏妆下的殇ゞ 提交于 2019-12-05 04:03:16
问题 I've been reading more specifically into the closure concept of programming, particularly pertaining to Javascript. I'm not quite there yet in understanding how it's any different from the Javascript code I've been writing for years though. I understand the concept of recursion as well, but I'm wondering, how are closures and recursion similar? Do I understand correctly that recursion, in and of itself, is a type of closure? Closure: function init() { var name = "Stack Overflow"; function

Partial application and closures

蹲街弑〆低调 提交于 2019-12-05 03:32:12
I was asked what's the relationship between partial function application and closures. I would say there isn't any, unless I'm missing the point. Let's say I'm writing in python and I have a very simple function MySum defined as follows: MySum = lambda x, y : x + y; Now I'm fixing one parameter to obtain a function with smaller arity which returns the same value that MySum would return if I called it with the same parameters (partial application): MyPartialSum = lambda x : MySum(x, 0); I could do the the very same thing with C: int MySum(int x, int y) { return x + y; } int MyPartialSum(int x)

Swift Sort an array with multiple sort criteria [duplicate]

孤人 提交于 2019-12-05 03:29:45
问题 This question already has answers here : How do I sort an array of structs by multiple values? (3 answers) Closed 4 years ago . How is an array sorted by multiple criteria in Swift? For example, an array of dictionaries as shown: items = [ [ "item":"itemA" "status":"0" "category":"B" ],[ "item":"itemB" "status":"1" "category":"C" ],[ "item":"itemC" "status":"0" "category":"A" ],[ "item":"itemD" "status":"2" "category":"A" ] ] This needs to be sorted as follows: category ASC status DESC I have