Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: On ordinal and cardinal numbers...

From: larry:ecotope at: 8-Jul-2001 15:59

Hi Joel, Volker Here is a different approach to circular addressing of a REBOL series. The heart of the matter is to be able to skip forward or backward by an arbitray number of steps. Here are two functions which serve that purpose. Rather than copying the the list being operated on, they create and return a series reference with the current index pointing to the desired position. No new series are created, just references. The original series is untouched. Neither of the functions does ANY numeric indexing into the original series. If you want to operate on a copy of the original series just do s: ring-skip n copy b which creates a new series and returns the desired reference. The first function uses iteratation: ring-skip: func [s [series!] n [integer!]] [ either positive? n [ loop n [if tail? s: next s [s: head s]] ][ loop abs n [s: back either head? s [tail s][s]] ] s ] The second uses recursion: ring-skip2: func [s [series!] n [integer!]] [ if n = 0 [return s] either positive? n [ if tail? s: next s [s: head s] ring-skip s n - 1 ][ s: back either head? s [tail s][s] ring-skip s n + 1 ] ] Examples:
>> b: "abcde"
== "abcde"
>> ring-skip b 24
== "e"
>> ring-skip b -1
== "e"
>> s: ring-skip b -1
== "e"
>> ring-skip s -1
== "de"
>> ring-skip b -2
== "de" b: [1 2 3 4 5]
>> b: [1 2 3 4 5]
== [1 2 3 4 5]
>> ring-skip2 b -1
== [5]
>> ring-skip2 b -2
== [4 5]
>> ring-skip2 b 344
== [5] Cheers -Larry