在 Grails 中的 every() 的最后一项上运行命令

发布于 2024-11-18 20:02:59 字数 284 浏览 0 评论 0原文

我有一组“任务”,我从正在迭代的 JSON 响应中返回这些“任务”,并对每个任务进行一些处理。这是一些伪代码:

def tasks = grails.converters.JSON.parse(json response)
tasks.each() {task ->
  //do some processing here
}

在列表中的最后一个任务中,我想运行一个附加操作。我正在寻找一种内置的方法来在 grails/groovy 中执行此操作。到目前为止,我的搜索还没有结果。

I have an array of 'tasks' that I am getting back from a JSON response that I am iterating through, and doing some processing on each task. Here is some psudocode:

def tasks = grails.converters.JSON.parse(json response)
tasks.each() {task ->
  //do some processing here
}

On the very last task in the list, I want to run an additional operation. I'm looking for a built in way to do this in grails/groovy. My searches have thus far yielded no results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

℡Ms空城旧梦 2024-11-25 20:02:59

另一种可能的解决方案是

tasks.eachWithIndex() { task, i ->
//whatever
   if(i == tasks.size() - 1){
      //something else
   }
}

Another possible solution is

tasks.eachWithIndex() { task, i ->
//whatever
   if(i == tasks.size() - 1){
      //something else
   }
}
葬花如无物 2024-11-25 20:02:59

另一种方法可能是执行以下操作:

tasks[0..-1].each {
  // Processing for all but last element
}
tasks[-1   ].each {
  // Processing for last element
}

当然,如果 tasks 列表中只有一个元素,那么它将同时应用两种处理(并且如果 tasks 列表中没有元素)列表,它会崩溃):-/


编辑

另一种选择(可能不被认为易于阅读)如下:

// A list of 'tasks' in our case Strings
tasks = [
  'a', 'b', 'c'
]

// Create a list of Closures the length of our list of tasks - 1
processing = (1..<tasks.size()).collect { { task -> "Start $task" } }
// Append a Closure to perform on the last item in the list
processing << { task -> "Final $task" }

// Then, transpose these lists together, and execute the Closure against the task
def output = [tasks,processing].transpose().collect { task, func -> func( task ) }

运行此命令后,输出等于:

[Start a, Start b, Final c]

这适用于只有一项的任务列表

Another method might be to do something along the lines of:

tasks[0..-1].each {
  // Processing for all but last element
}
tasks[-1   ].each {
  // Processing for last element
}

Of course, if there's only one element in the tasks list, then it will get both processing applied to it (and if there are no elements in the list, it will crash) :-/


edit

An alternative (which may not be considered as easily readable), is as follows:

// A list of 'tasks' in our case Strings
tasks = [
  'a', 'b', 'c'
]

// Create a list of Closures the length of our list of tasks - 1
processing = (1..<tasks.size()).collect { { task -> "Start $task" } }
// Append a Closure to perform on the last item in the list
processing << { task -> "Final $task" }

// Then, transpose these lists together, and execute the Closure against the task
def output = [tasks,processing].transpose().collect { task, func -> func( task ) }

After running this, output is equal to:

[Start a, Start b, Final c]

And this works with task lists with just one item

贱贱哒 2024-11-25 20:02:59

我认为你需要的是:(

tasks.each() {task ->
  //do some processing here
}
tasks[-1].doSomethingElse()

提供的tasks.size()> 0)。

[-1] 语法表示列表中的最后一个。

I think what you need is along the lines of:

tasks.each() {task ->
  //do some processing here
}
tasks[-1].doSomethingElse()

(provided tasks.size() > 0).

The [-1] syntax means last in the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文