My code is clunky:
length: length? items
count: 0
foreach item items [
count: count + 1
if count = length [
print "last item"
]
]
Is there something better ?
My code is clunky:
length: length? items
count: 0
foreach item items [
count: count + 1
if count = length [
print "last item"
]
]
Is there something better ?
Typically people use FORALL (which would be much better named FOR-NEXT) which moves the series position as opposed to giving an item, then you can test it with TAIL?. The downside is you have to pick the item out of the series at the current position:
forall items [
probe items/1 ;-- how to access current item
if tail? next items [ ;-- could use LAST? ITEMS in Rebol 3
print "last item"
]
]
This is approximately equivalent to:
if not tail? items [
original: items
until [
probe items/1
if tail? next items [
print "last item"
]
items: next items
tail? items
]
items: original
]
Be forewarned: FORALL mutates its input series and tries to put it back to the initial position at the end. But it has poorly-defined behavior in the case of errors being raised, so you could leave your input in mid-iteration if there is a problem.