[REBOL] Re: On ordinal and cardinal numbers...
From: joel:neely:fedex at: 7-Jul-2001 12:31
[agem--crosswinds--net] wrote:
> reading Joel he really complains two things:
>
Me??? Complain??? 8-O
> i suggest a ring!
>
Handily done with an object...
ring: make object! [
_data: [0 1]
_pos: 1
_len: length? _data
data: func [b [block!]] [_len: length? _data: copy b skip 0]
curr: does [pick _data _pos]
next: does [pick _data _pos: _pos // _len + 1]
prev: does [pick _data _pos: _pos + _len - 2 // _len + 1]
skip: func [n [integer!]] [
pick _data _pos: _pos - 1 + n // _len + _len // _len + 1
]
at: func [n [integer!]] [
pick _data _pos: n - 1 // _len + _len // _len + 1
]
at?: does [_pos]
]
which behaves as:
>> nursery-rhyme: make ring []
>> nursery-rhyme/data ["eenie" "meenie" "mienie" "moe"]
== "eenie"
>> loop 5 [prin [nursery-rhyme/next " "]] print ""
meenie mienie moe eenie meenie
>> loop 5 [prin [nursery-rhyme/prev " "]] print ""
eenie moe mienie meenie eenie
>> nursery-rhyme/curr
== "eenie"
>> nursery-rhyme/at?
== 1
>> nursery-rhyme/skip 7
== "moe"
>> nursery-rhyme/at 38
== "meenie"
>> nursery-rhyme/at?
== 2
>> nursery-rhyme/skip -5
== "eenie"
The slight awkwardness of using /DATA to configure the content
(instead of e.g. MAKE RING [...]) is the price for having
LENGTH? _DATA cached instead of being re-evaluated all over
the place.
> ;-) Volker
> --zero is the highest number
>
ROFL!!!
--
---------------------------------------------------------------
There are two types of science: physics and stamp collecting!
-- Sir Arthur Eddington
joel-dot-neely-at-fedex-dot-com