PDA

View Full Version : Circular Next/Previous



ErosOlmi
11-10-2008, 12:28
I've developed 2 new functions to simplify circular numbers that is numbers that must have a range of values and be incremented/decremented by something but when they reach their bounds (upper or lower) they restart from the other side.

For example imagine you have a set of options from 1 to 4 and want always add 1 but when it reach 4, adding 1 will restart from 1. The same when subtracting 1 if it will reach 1 it will restart from 4.

I've developed the following 2 functions:

MyNumber = NNEXT(MyNumber, nMin, nMax, nIncr)
MyNumber = NPREV(MyNumber, nMin, nMax, nDecr)
but I do not like the names: NNEXT and NPREV.

Do you have something to suggest? I was thinking about CIRCULAR_NEXT and CIRCULAR_PREV

Ciao
Eros

catventure
11-10-2008, 13:29
Hi Eros,

This is similar to a request for a cyclic string I made some time ago - so how about:

CYCLENEXT ; CYCLEPREVIOUS

or CYCLE_FORWARD; CYCLE_BACK

Cheers,
catventure

ErosOlmi
11-10-2008, 13:32
Yes, you are right :-[ http://community.thinbasic.com/index.php?topic=1754.0
I will revamp it while I'm working on that.

Thanks for remembering to me.
Eros

catventure
11-10-2008, 13:45
I had forgotten all about it... :)

But maybe you are already on the right track to make it with similar adaptation to the new "StrFormat$" command?
"StrFormat$" looks very interesting new string command.

Bye,
catventure

Petr Schreiber
11-10-2008, 14:25
Hi Eros,

very useful function.
Hard to invent the name, maybe we could use that CYCLE_NEXT, CYCLE_PREV or INTERVAL_NEXT, INTERVAL_PREV.


Petr

ErosOlmi
11-10-2008, 14:42
I like CYCLE_NEXT and CYCLE_PREV

kryton9
12-10-2008, 04:28
I like the idea of this new command a lot.

CYCLE_NEXT / CYCLE_PREV does not look too BASIC like to me, especially if it will be a core language command and not a special module command like tbgl.

It seems to match BASIC more it should be 2 separate words or one word.

Robert Hodge
10-06-2013, 19:33
Well, I am way WAY late to this party, but since I had considered this feature myself some years ago, I thought you might like my two cents.

First, ideally you'd like to be able to define an enumeration of items for your prev/next operation. Maybe:

ENUM myEnum() AS WORD = MyNumber, nMin, nMax, nIncr

This is different that BEGIN/END CONST, because that *defines* the constants, but the enumeration defines a particular *use* of those constants.

Of course, such an ENUM list would be static as coded above. You would be able to assign an ENUM at runtime:

myEnum = (MyNumber, nMin, nMax, nIncr)

You would also be able to clear the enum:

myEnum = ()

and add new items:

myEnum += (nDecr)

or remove them:

myEnum -= (nMin)

Now, as part of the enumeration, you have an implied "iterator" (in the C++ sense of the word), so that when you are going forward or backward, the iterator gets incremented or decremented. In the case of this example, there are 4 initial entries. Assuming the iterator was 0-based, you would get an index to the 'next' item by

iterator = (iterator + 1) MOD 4

and if it was 1-based,

iterator = (iterator MOD 4) + 1

The fact that a modulus operation is implied suggests a good name for this.

So, when you wanted a 'circular next' for your enumeration, it could be:

MOD INCR myEnum

and

MOD DECR myEnum

You'd need some kind of syntax to set the iterator to the first or last entry of the enumeration. Obvious syntax might be:

MOD FIRST myEnum
MOD LAST myEnum

but I am less certain about this part of it.

If you wanted to get the "location" where the iterator is currently pointing (as an index value between 1 and the number of entries currently held in the enumeration), MOD LOC myEnum would do it. It is possible that all of the enumerated entries might have been deleted, so MOD LOC might return 0.

Finally, when you just the currently-set value where the iterator is pointing to, you would simply use myEnum as a plain value.


Well, that's my 2¢ worth

ErosOlmi
10-06-2013, 22:44
This is quite old: 2008. Almost 5 years ago discussion.

Well, 5 years ago we had much less options we have now in thinBasic.
if I had to develop this feature right now I would use thinBasic module classes in order to have something like the following:


...
Dim MyIter as cIterator
MyIterator = New cIterator(MinVal, MaxVal, CurrentVal)
MyIterator.Incr[(<AnyNumberIfNotOne>)]
MyIterator.Decr[(<AnyNumberIfNotOne>)]
MyIterator.GetCurVal
MyIterator.Set(<any number>)
...

So it would be a class with a create method and some additional methods in order to use it.

See cTimer class example/help