在 Coffeescript 中中断/继续嵌套 for 循环

发布于 2024-12-08 09:03:15 字数 531 浏览 0 评论 0原文

如何在 Coffeescript 中中断/继续嵌套循环?例如,我有类似的内容:

for cat in categories
  for job in jobs
    if condition
      do(this)
      ## Iterate to the next cat in the first loop

另外,有没有办法将整个第二个循环包装为第一个循环中另一个函数的条件?例如

for cat in categories
  if conditionTerm == job for job in jobs
    do(this)
    ## Iterate to the next cat in the first loop
  do(that) ## Execute upon eliminating all possibilities in the second for loop,
           ## but don't if the 'if conditionTerm' was met

How can I break/continue nested loops in Coffeescript? E.g. I have something like:

for cat in categories
  for job in jobs
    if condition
      do(this)
      ## Iterate to the next cat in the first loop

Also, is there a way to wrap the whole second loop as a conditional to another function within the first loop? E.g.

for cat in categories
  if conditionTerm == job for job in jobs
    do(this)
    ## Iterate to the next cat in the first loop
  do(that) ## Execute upon eliminating all possibilities in the second for loop,
           ## but don't if the 'if conditionTerm' was met

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

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

发布评论

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

评论(7

乖乖兔^ω^ 2024-12-15 09:03:15

break 就像 js 一样工作:

for cat in categories
  for job in jobs
    if condition
      do this
      break ## Iterate to the next cat in the first loop

你的第二种情况不是很清楚,但我假设你想要这个:

for cat in categories
    for job in jobs
      do this
      condition = job is 'something'
    do that unless condition

break works just like js:

for cat in categories
  for job in jobs
    if condition
      do this
      break ## Iterate to the next cat in the first loop

Your second case is not very clear, but I assume you want this:

for cat in categories
    for job in jobs
      do this
      condition = job is 'something'
    do that unless condition
余生一个溪 2024-12-15 09:03:15

使用标签。由于 CoffeeScript 不支持它们,因此您需要这样进行破解:

0 && dummy
`CAT: //`
for cat in categories
  for job in jobs
    if conditionTerm == job
      do this
      `continue CAT` ## Iterate to the next cat in the first loop
  do that ## Execute upon eliminating all possibilities in the second for loop,
          ## but don't if the 'if conditionTerm' was met

Use labels. Since CoffeeScript doesn't support them, you need to hack as such:

0 && dummy
`CAT: //`
for cat in categories
  for job in jobs
    if conditionTerm == job
      do this
      `continue CAT` ## Iterate to the next cat in the first loop
  do that ## Execute upon eliminating all possibilities in the second for loop,
          ## but don't if the 'if conditionTerm' was met
束缚m 2024-12-15 09:03:15

Coffescript 的“break”仅中断立即循环,无法识别外部循环是否中断(烦人!)。在某些情况下,以下黑客可以在满足条件时打破多个循环:

ar1 = [1,2,3,4]
ar2 = [5,6,7,8]

for num1 in ar1
  for num2 in ar2
    console.log num1 + ' : ' + num2
    if num2 == 6
      breakLoop1 = true; break 
  break if breakLoop1

# Will print:
# 1 : 5
# 1 : 6

Coffescript's "break" only breaks the immediate loop and has no way of identifying an outer loop for breakage (annoying!). This following hack works in some instances for breaking out of multiple loops when a condition is met:

ar1 = [1,2,3,4]
ar2 = [5,6,7,8]

for num1 in ar1
  for num2 in ar2
    console.log num1 + ' : ' + num2
    if num2 == 6
      breakLoop1 = true; break 
  break if breakLoop1

# Will print:
# 1 : 5
# 1 : 6
似狗非友 2024-12-15 09:03:15

使用带有 return 的匿名循环

do ->
  for a in A
    for b in B 
      for c in C
        for d in D
          for e in E
            for f in F
              for g in G
                for h in H
                  for i in I
                    #DO SOMETHING
                    if (condition)
                      return true

Using anonymous loop with return

do ->
  for a in A
    for b in B 
      for c in C
        for d in D
          for e in E
            for f in F
              for g in G
                for h in H
                  for i in I
                    #DO SOMETHING
                    if (condition)
                      return true
朦胧时间 2024-12-15 09:03:15

Coffeescript 永远不会有多个中断/继续语句,您必须坚持使用丑陋且过多的标志来污染您的代码,或者尝试用 lambda 替换它,并使用 return 作为多重休息。

https://github.com/jashkenas/coffeescript/issues/4254

Coffeescript would never have multiple breaking/continuing statements, you have to stick to ugly and excessive flags polluting your code or try to replace it by do with a lambda and use return as a multiple break.

https://github.com/jashkenas/coffeescript/issues/4254

抽个烟儿 2024-12-15 09:03:15

为了检查数组中的所有元素,也许 lodash 的 every 会有用?

https://lodash.com/docs#every

for cat in categories
  if _.every jobs, conditionTerm
...

For checking all elements in an array, maybe lodash's every would be of use?

https://lodash.com/docs#every

for cat in categories
  if _.every jobs, conditionTerm
...
赠佳期 2024-12-15 09:03:15

我想如果你想使用内部中断/继续,你的代码设计不是很好。
在我看来,任何编程语言都不允许这样做。

按照某人的建议使用标签也被认为是不好的风格。

I suppose the design of your code is not very good if you want to use inner break/continue.
It seems to me that any programming language doesn`t allow that.

Using labels as someone suggested is also considered as bad style.

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