closures

Why am I getting a “No signature of method”" error when running the closure recursion example in the Groovy shell?

与世无争的帅哥 提交于 2019-12-07 08:32:28
问题 I'm trying to experiment with the Groovy closure recursion example from http://groovy.codehaus.org/JN2515-Closures . I saved the snippet in a file called recursionTest.groovy and loaded it in the shell, but I'm getting a "No signature of method error": // recursionTest.groovy def results = []; { a, b -> results << a a<10 && call(b, a+b) }(1,1) assert results == [1, 1, 2, 3, 5, 8, 13] groovy:000> load recursionTest.groovy ===> [] ERROR groovy.lang.MissingMethodException: No signature of method

Scala local return?

痞子三分冷 提交于 2019-12-07 07:31:00
问题 I've just discovered that return s in following closure will return from function findPackage def findPackage(name: String, suffix: Option[String] = None): Path = { logger.debug("Looking for package {} with suffix {}", name, suffix) val path: Path = using(Files.newDirectoryStream(appDir)) {dirs => for (val path <- dirs) { val matcher = packagePattern.matcher(path.getFileName.toString) if (matcher.matches() && matcher.group(1).equals(name)) if (suffix.isDefined) { if (matcher.group(2) != null

Python closures and cells (closed-over values)

我的未来我决定 提交于 2019-12-07 07:27:21
问题 What is the Python mechanism that makes it so that [lambda: x for x in range(5)][2]() is 4? What is the usual trick for binding a copy of x to each lamba expression so that the above expression will equal 2? My final solution: for template, model in zip(model_templates, model_classes): def create_known_parameters(known_parms): return lambda self: [getattr(self, p.name) for p in known_parms] model.known_parameters = create_known_parameters(template.known_parms) 回答1: I usually use functools

How to return a generic Map struct?

∥☆過路亽.° 提交于 2019-12-07 07:08:39
问题 I'm not sure how return a generic Map struct now that the declaration for Map has changed. The new declaration is: pub struct Map<A, B, I: Iterator<A>, F: FnMut<(A,), B>> { // some fields omitted } I'm not sure how to return one of these from a function: fn something<A: Clone, I: Iterator<A>>(iter: I) -> Map<A, (A,A), I, **what goes here**> { iter.map(|x| (x.clone(), x)) } I've tried using the signature of the closure I'm using... fn something<A: Clone, I: Iterator<A>>(iter: I) -> Map<A, (A,A

Details on returning an object literal from a closure in JavaScript

左心房为你撑大大i 提交于 2019-12-07 06:57:54
问题 Background : I want to rewrite a library (which I did not write) to avoid having the Closure Compiler generate warnings with the advanced option. Per this question JavaScript “this” keyword and Closure Compiler warnings the answer was to rewrite the code using a closure. The intent is to avoid using the keyword this (which generates the compiler warnings). As the library has a number of functions, I though it would be best for the new closure to return an object literal. I want to understand

Lambda treated as a closed delegate in Visual Studio 2015

别说谁变了你拦得住时间么 提交于 2019-12-07 06:05:36
问题 I was surprised that there would be any runtime difference between these two delegates ( fn1 and fn2 ): static int SomeStaticMethod(int x) { return x; } // fn1.Target == null in this case Func<int, int> fn1 = SomeStaticMethod; // fn2.Target != null in this case Func<int, int> fn2 = x => x; But apparently the second lambda is treated like an instance method, because its Target property is non-null. And it was treated differently before I switched to Visual Studio 2015 (in VS2012, I am pretty

Calling closures from an array in Rust

心已入冬 提交于 2019-12-07 03:50:42
问题 How do I iterate over an array of closures, calling each one in turn? With functions, I discovered I could do this by just iterating over the array, and dereferencing the values that produced: fn square(x: int) -> int { x * x } fn add_one(x: int) -> int { x + 1 } fn main() { let funcs = [square, add_one]; for func in funcs.iter() { println!("{}", (*func)(5i)); } } However, when I try to do the same with closures, I get an error: fn main() { let closures = [|x: int| x * x, |x| x + 1]; for

How to make Groovy / Grails return a List of objects instead of a List of Lists of objects?

大兔子大兔子 提交于 2019-12-07 03:37:19
问题 I have a class like this: class Foo { static hasMany = [bars: Bar] } When I write: Foo.getAll() I get a list of Foo objects like this: [ Foo1, Foo2, Foo3 ] When I write: Foo.getAll().bars I get a list of lists of Bar object like this: [ [ Bar1, Bar2 ], [ Bar2, Bar3 ], [ Bar1, Bar4 ] ] But what I want is a unique list of Bar objects like this: [ Bar1, Bar2, Bar3, Bar4 ] My end goal is to have a unique list of ids of the Bar object in the list above, like this: [ 1, 2, 3, 4 ] I have tried

How can I serialize a closure in Perl?

徘徊边缘 提交于 2019-12-07 03:04:08
问题 I think this might be best asked using an example: use strict; use warnings; use 5.010; use Storable qw(nstore retrieve); local $Storable::Deparse = 1; local $Storable::Eval = 1; sub sub_generator { my ($x) = @_; return sub { my ($y) = @_; return $x + $y; }; } my $sub = sub_generator(1000); say $sub->(1); # gives 1001 nstore( $sub, "/tmp/sub.store" ); $sub = retrieve("/tmp/sub.store"); say $sub->(1); # gives 1 When I dump /tmp/sub.store I see: $VAR1 = sub { package Storable; use warnings; use

Limitations of Java Anonymous Classes compared to Objective-C Blocks

感情迁移 提交于 2019-12-07 02:48:01
问题 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? 回答1: Java anonymous classes are really, really