rebol/red: 有没有一种优雅的方法可以知道它是否是 foreach 中的最后一项?

rebol/red: Is there an elegant way to know if it is last item in foreach?

我的代码很笨拙:

  length: length? items
  count: 0
  foreach item items [
     count: count + 1
     if count = length [
        print "last item"
     ]
  ]

还有更好的吗?

通常人们使用 FORALL (最好命名为 FOR-NEXT) 移动系列位置而不是给出一个项目,那么你可以用 TAIL 测试它吗? .缺点是您必须从当前位置的系列中选择项目:

forall items [
    probe items/1 ;-- how to access current item
    if tail? next items [ ;-- could use LAST? ITEMS in Rebol 3
        print "last item"
    ]
]

这大约相当于:

 if not tail? items [
     original: items
     until [
         probe items/1
         if tail? next items [
             print "last item"
         ]
         items: next items
         tail? items
     ]
     items: original
 ]

预先警告:FORALL 对其输入系列进行变异,并尝试在最后将其放回初始位置。但在出现错误的情况下,它的行为定义不明确,因此如果出现问题,您可以在迭代中期保留输入。