Design patterns 在groovy中从闭包返回方法
由于闭包的行为类似于内联方法(我猜,从技术上讲,闭包是编译成类的),所以我在Groovy中没有找到如何从闭包返回方法 例如,如果我没有使用闭包,那么从Design patterns 在groovy中从闭包返回方法,design-patterns,groovy,closures,Design Patterns,Groovy,Closures,由于闭包的行为类似于内联方法(我猜,从技术上讲,闭包是编译成类的),所以我在Groovy中没有找到如何从闭包返回方法 例如,如果我没有使用闭包,那么从main()调用下面的方法应该只打印1,但使用闭包它会打印所有1、2、3: public static returnFromClosure() { [1,2,3].each{ println it if (it == 1) return } } 我怎样
main()
调用下面的方法应该只打印1,但使用闭包它会打印所有1、2、3:
public static returnFromClosure()
{
[1,2,3].each{
println it
if (it == 1)
return
}
}
我怎样才能做到这一点
编辑
这个问题以重复的形式结束了。
我的问题是关于一般的结束。我给出了每个涉及闭包的{}循环的例子。我知道关于在中使用break
和continue
的问题,每个{}
循环都存在,但这将涉及到中断该循环或继续循环的下一次迭代,而不是返回调用代码。这种错误理解背后的罪魁祸首似乎就是我上面给出的例子。但是,在任何循环中使用return
与使用break
是不同的。我最好给出不同的例子。这是一个简单的闭包:
static main(def args)
{
def closure1 = {
println 'hello';
return; //this only returns this closure,
//not the calling function,
//I was thinking if I can make it
//to exit the program itself
println 'this wont print'
}
closure1();
println 'this will also print :('
}
我不熟悉groovy,但这似乎是预期的行为。通常在each语句中,它实际上是为数组中的每个值分别运行一个函数 因此,当值第一次为1时运行它,它将传递if语句,然后返回 然后运行下一个函数,这次值为2。它输出2,值不传递if语句,然后返回,因为它是函数的结尾 如果只想打印与一个匹配的值,可以这样做
public static returnFromClosure()
{
[1,2,3].each{
if (it == 1)
println it
}
}
static main(def args){
done = false;
def closure1 = {
println 'hello';
done = true;
return; //this only returns this closure,
//not the calling function,
//I was thinking if I can make it
//to exit the program itself
println 'this wont print'
}
closure1();
if( done ){
return;
}
// this will no longer print =)
println 'this will also print :('
}
如果您想停止执行each函数,并在找到等于1的值后继续执行,那么您应该查看另一篇文章。
根据您的更新进行编辑:
据我所知,没有一种具体的机制能像你所说的那样起作用。闭包只是在特定上下文中编写的函数,与任何其他函数一样,闭包只能从自己的执行中返回。我想你想要的是这样的东西
public static returnFromClosure()
{
[1,2,3].each{
if (it == 1)
println it
}
}
static main(def args){
done = false;
def closure1 = {
println 'hello';
done = true;
return; //this only returns this closure,
//not the calling function,
//I was thinking if I can make it
//to exit the program itself
println 'this wont print'
}
closure1();
if( done ){
return;
}
// this will no longer print =)
println 'this will also print :('
}
我不熟悉groovy,但这似乎是预期的行为。通常在each语句中,它实际上是为数组中的每个值分别运行一个函数 因此,当值第一次为1时运行它,它将传递if语句,然后返回 然后运行下一个函数,这次值为2。它输出2,值不传递if语句,然后返回,因为它是函数的结尾 如果只想打印与一个匹配的值,可以这样做
public static returnFromClosure()
{
[1,2,3].each{
if (it == 1)
println it
}
}
static main(def args){
done = false;
def closure1 = {
println 'hello';
done = true;
return; //this only returns this closure,
//not the calling function,
//I was thinking if I can make it
//to exit the program itself
println 'this wont print'
}
closure1();
if( done ){
return;
}
// this will no longer print =)
println 'this will also print :('
}
如果您想停止执行each函数,并在找到等于1的值后继续执行,那么您应该查看另一篇文章。
根据您的更新进行编辑:
据我所知,没有一种具体的机制能像你所说的那样起作用。闭包只是在特定上下文中编写的函数,与任何其他函数一样,闭包只能从自己的执行中返回。我想你想要的是这样的东西
public static returnFromClosure()
{
[1,2,3].each{
if (it == 1)
println it
}
}
static main(def args){
done = false;
def closure1 = {
println 'hello';
done = true;
return; //this only returns this closure,
//not the calling function,
//I was thinking if I can make it
//to exit the program itself
println 'this wont print'
}
closure1();
if( done ){
return;
}
// this will no longer print =)
println 'this will also print :('
}