Referring to next/prev series entries
I have a common usage case where I need to iterate through a series, and do something with the current entry and its immediate neighbors  ie the one before or the one after. Unless I am missing something obvious, neither Rebol nor Red have simple syntax for this. Here's an example where I want to find the difference between consecutive values: ; create a block of objects x: [] loop 10 [append x make object! [r: random 100]] ; iterating through the series is easy forall x [print x/1/r] ; referring to the next entry in the series is possible x: head x forall x [unless tail? next x [print x/2/r  x/1/r]] ; referring to the previous entry in the series is awkward x: next head x forall x [print x/1/r  get in first back x 'r] ; in an ideal world :) x: next head x forall x [print x/1/r  x/0/r] There are other ways  such as using REPEAT and setting a word to one less than the interation counter  any suggestions for more Reddish ways?
posted by: Sunanda 4Nov2018/5:07:468:00
Negative numbers will go back in the series in a similar way in Red and Rebol. forall x [print x/1/r  x/1/r] RenC uses 0 to get the prior value.
posted by: Chris 4Nov2018/12:54:478:00
The 0 was a convention introduced in R3Alpha, which was largely unpopular...and RenC ultimately went ahead and changed to have 1 pick the prior value (circa June): https://github.com/metaeducation/renc/pull/818
posted by: Fork 4Nov2018/18:29:398:00
Thanks Chris  so simple, when you know how :) I was tripped up by expecting Rebol's ordinal number line to have a zeroth.
posted by: Sunanda 5Nov2018/3:23:248:00
My bad HFtested on a slightly older version of RenC
posted by: Chris 5Nov2018/16:23:488:00
I often used loops with a counter for those sorts of routines: repeat i length? x [print [x/(i) x/(i  1)]]
posted by: Nick 5Nov2018/23:32:468:00
Given: x: [] loop 10 [append x make object! [r: random 100]] From the answers above, this does not work: forall x [print x/1/r  x/1/r] And yes, Chris caught his error. Proof: >> forall x [print x/1/r  x/1/r] ** Script Error: Cannot use path on none! value ** Where: forskip ** Near: forall x [print x/1/r  x/1/r] However, this does: out1: copy [] n: 2 until [ append out1 reduce [x/(n)/r  x/(n  1)/r] n: n + 1 n = (1 + length? x) ] Proof: out2: copy [] x: head x forskip x 2 [ append out2 reduce [negate x/1/r '+ x/2/r] if not none? ok: attempt [ reduce [negate x/2/r '+ x/3/r]][append out2 ok] ] print out1 print reduce out2
posted by: Stone Johnson 23Aug2019/18:17:417:00
