closures

groovy: safely find a key in a map and return its value

风格不统一 提交于 2019-12-31 17:37:54
问题 I want to find a specific key in a given map. If the key is found, I then want to get the value of that key from the map. This is what I managed so far: def mymap = [name:"Gromit", likes:"cheese", id:1234] def x = mymap.find{ it.key == "likes" } if(x) println x.value This works, the output is "cheese" as expected. Great, but I don't want to do x.value at the end, and I don't want to do if(x) . I want x to directly contain the value somehow. I can't get the value directly into x like this: def

Are there any good reasons why closures aren't immutable in C#?

≯℡__Kan透↙ 提交于 2019-12-31 16:46:44
问题 I've been going over and over this in my head, and I can't seem to come up with a good reason why C# closures are mutable. It just seems like a good way to get some unintended consequences if you aren't aware of exactly what's happening. Maybe someone who is a little more knowledgeable can shed some light on why the designers of C# would allow state to change in a closure? Example: var foo = "hello"; Action bar = () => Console.WriteLine(foo); bar(); foo = "goodbye"; bar(); This will print

Why don't ruby methods have lexical scope?

馋奶兔 提交于 2019-12-31 13:57:51
问题 For example def test a = "a is for apple" def inner_method a = "something" # this will refer to a different "a" end inner_method puts a end Are there any reasons for this? Blocks have lexical scope, so why don't methods? Is this going to be fixed? 回答1: It's because Ruby's methods aren't first class objects (as they would be in IO, for example). So when you define the inner method, what is the receiver? Presumably the method itself, or the binding or something, but Ruby doesn't have that deep

How to dynamically define a class method which will refer to a local variable outside?

一个人想着一个人 提交于 2019-12-31 12:59:34
问题 class C end var = "I am a local var outside" C.class_eval do def self.a_class_method puts var end end I know, this is not correct, because the def created a new scope. I also know that use define_method can create a instance method without creating a new scope, but my point is how to define a class method . 回答1: Class methods don't really exist in Ruby, they are just singleton methods of the class object. Singleton methods don't really exist, either, they are just ordinary instance methods of

Closures (in Haskell)

大兔子大兔子 提交于 2019-12-31 10:41:32
问题 To me a Closure is a (nested?) function with co-located data. When you write software in Haskell and look it through afterwards, you frequently find closures that you have created unintentionally. I do not quite get this right for myself. In what situations would I intentionally want to code closures? After all, in all examples I find the amount of co-located data is trivial/small and thus it does not quite seem to me as if in practice that would ever justify their (intentional) creation. Is

Closures (in Haskell)

左心房为你撑大大i 提交于 2019-12-31 10:41:10
问题 To me a Closure is a (nested?) function with co-located data. When you write software in Haskell and look it through afterwards, you frequently find closures that you have created unintentionally. I do not quite get this right for myself. In what situations would I intentionally want to code closures? After all, in all examples I find the amount of co-located data is trivial/small and thus it does not quite seem to me as if in practice that would ever justify their (intentional) creation. Is

How are Scala closures transformed to Java objects?

我是研究僧i 提交于 2019-12-31 10:23:12
问题 I'm currently looking at closure implementations in different languages. When it comes to Scala, however, I'm unable to find any documentation on how a closure is mapped to Java objects. It is well documented that Scala functions are mapped to FunctionN objects. I assume that the reference to the free variable of the closure must be stored somewhere in that function object (as it is done in C++0x, e.g.). I also tried compiling the following with scalac and then decompiling the class files

How are Scala closures transformed to Java objects?

独自空忆成欢 提交于 2019-12-31 10:23:05
问题 I'm currently looking at closure implementations in different languages. When it comes to Scala, however, I'm unable to find any documentation on how a closure is mapped to Java objects. It is well documented that Scala functions are mapped to FunctionN objects. I assume that the reference to the free variable of the closure must be stored somewhere in that function object (as it is done in C++0x, e.g.). I also tried compiling the following with scalac and then decompiling the class files

Scala advantages after Java having closures [closed]

霸气de小男生 提交于 2019-12-31 08:37:06
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . With closures being added to Java, what is Scala's advantage over Java as a language choice? Can someone elaborate on any advantages? 回答1: 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

Is there any particular use of closure in swift? and what's the benefit?

二次信任 提交于 2019-12-31 07:31:28
问题 I have learnt Swift for a while and I have read the Swift language guide. https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-ID94 The concept closure is new to me. I think I can understand how to use it, but where I can use it? what is the benefit of it? I googled and get the answer When to use closures in swift? I do not thinks the answer is satisfying. The language guide writes so much about