CoffeeScript... 中是否省略了“do...while”循环?

发布于 2024-11-08 15:37:05 字数 345 浏览 0 评论 0原文

在 CoffeeScript 中,while 循环是标准的:

while x()
   y()

但是,以下1 不起作用:

do
  y()
while x()

这只是第一个示例的糖衣:

y() while x()

CoffeeScript 是否带有内置的in 循环至少执行一次?

1顺便说一句,do 一个关键字 - 它用于调用匿名函数。

In CoffeeScript, the while loop comes standard:

while x()
   y()

However, the following1 doesn't work:

do
  y()
while x()

And this is simply sugar for the first example:

y() while x()

Does CoffeeScript come with a built-in loop that executes at least once?

1As an aside, do is a keyword — it's used to call anonymous functions.

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

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

发布评论

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

评论(5

红玫瑰 2024-11-15 15:37:05

CoffeeScript 文档说:

CoffeeScript 提供的唯一低级循环是 while 循环。

我不知道至少执行一次的内置循环,所以我想替代方案是

loop
  y()
  break if x()

The CoffeeScript documentation says:

The only low-level loop that CoffeeScript provides is the while loop.

I don't know of a built-in loop that executes at least once, so I guess the alternative is

loop
  y()
  break if x()
独留℉清风醉 2024-11-15 15:37:05

我知道这个答案很旧,但自从我通过谷歌进入这里,我想其他人也可能会这样。

要在 CoffeeScript 中构建等效的 do...while 循环,我认为这种语法模拟它是最好、最简单的,并且非常易读:

while true
   # actions here
   break unless # conditions here

I know that this answer is very old, but since I entered here via Google, I thought someone else might as well.

To construct a do...while loop equivalent in CoffeeScript I think that this syntax emulates it the best and easiest and is very readable:

while true
   # actions here
   break unless # conditions here
柳若烟 2024-11-15 15:37:05

您的猜测是正确的:CoffeeScript 中没有等效的 do-while 。所以你通常会写

y()
y() while x()

如果你发现自己经常这样做,你可以定义一个辅助函数:

doWhile = (func, condition) ->
  func()
  func() while condition()

Your guess is correct: There is no do-while equivalent in CoffeeScript. So you'd typically write

y()
y() while x()

If you find yourself doing this often, you might define a helper function:

doWhile = (func, condition) ->
  func()
  func() while condition()
北方。的韩爷 2024-11-15 15:37:05

我发现这可以通过短路条件来完成:

flag = y() while not flag? or x()

I found this could be accomplished through a short circuit conditional:

flag = y() while not flag? or x()
东走西顾 2024-11-15 15:37:05

我一直在研究一个项目,我只是强制条件在循环结束时进行评估,然后在开始时终止。

# set the 'do' variable to pass the first time
do = true
while do

  # run your intended code
  x()

  # evaluate condition at the end of
  # the while code block
  do = condition

# continue code

它不是很优雅,但它确实阻止您仅为 while 代码块定义一个新函数并运行它两次。通常有一种方法可以围绕 do...while 语句进行编码,但对于那些无法做到这一点的情况,您可以有一个简单的解决方案。

I've been working on a project where I simply force the condition to evaluate at the end of the loop, then terminate at the beginning.

# set the 'do' variable to pass the first time
do = true
while do

  # run your intended code
  x()

  # evaluate condition at the end of
  # the while code block
  do = condition

# continue code

It's not very elegant, but it does keep you from defining a new function just for your while code block and running it twice. There generally is a way to code around the do...while statements, but for those time that you can't you have a simple solution.

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