I am creating a couple of Squeak (and Pharo) classes that I will eventually release. I am deciding on syntax and would like to know Squeakers opinions given that any Squeakers who use these classes will be stuck with the syntax.
The first class contains a sorted list of cardinals (non-negative integers) and ranges of cardinals. an instance of this class can be created from a string where the string has as examples:
'0,3-5,9,11,17-89' (1) '7-11,15,30-' (2)
Note that the second example is correct: The '30-' substring at the end implies the list includes all the cardinals from 30 to plus infinity. I am happy with this syntax.
My problem is my second class which is a similar sorted list but which also allows negative integers, The question is how should I store a sorted list of integers and integer ranges in a string such that I can read them in?
The obvious way would be as indicated by the example:
'-9,-5--3,-1-4,7,11-15' (3) (method 0)
This is the same as for the first class except negative numbers are now allowed. However, I consider this syntax difficult to read and it presents difficulties in including a range starting with minus infinity. For example does '-9' indicate the range from minus infinity to 9 or just the number -9?
Some possible alternative solutions:
1) Use a different character to indicate ranges such as: '0,3@5,9,11,17@89' [example 1 again] (method 1) and: -9,-5@-3,-1@4,7,11@15 [example 3 again] (method 1)
This goes against convention and requires more learning by the user to learn the syntax.
2) Mark the list of negative numbers such as: '[9,5-3,1],0-4,7,11-15' [example 3 again] (method 2a) or: '[1,3-5,9],0-4,7,11-15' [example 3 again] (method 2b)
Both methods have the problem that a range starting with a negative and ending with a cardinal must be split into two parts. Method 2b has the advantage that the code from the first class can be used to read the negative numbers in and then again for the cardinal numbers.
3) Use method 1 for negatives but continue to use the minus sign for ranges of cardinals: -9,-5@-3,-1@4,7,11-15 [example 3 again] (method 3)
I want to know which of methods 0,1,2a,2b,3 is the best and am also interested in other proposals. Opinions much appreciated.
If there is a conventional solution to this problem I would of course like to know what it is and where it is used.
Regards,
Ralph Boland
On 2011-06-06 7:51 AM, Ralph Boland wrote:
If there is a conventional solution to this problem I would of course like to know what it is and where it is used.
SML's syntax forbids use of the same operator in unary and binary contexts, so it distinguishes between unary minus and binary minus. SML's unary minus operator is tilde "~", so you could write:
'~9,~5-~3,~1-4,7,11-15'
for the problem example.
Another option could be to use interval syntax:
'-9 [-5 -3] [-1 4] 7 [11 15]'
with or without commas.
Regards, Tony
Use something else instead of the "-" for ranges, like ".." or ":". E.g.
-9,-5..-3,-1..4,7,11..15
-9,-5:-3,-1:4,7,11:15
On 11/06/06 05:48, Tony Garnock-Jones wrote:
On 2011-06-06 7:51 AM, Ralph Boland wrote:
If there is a conventional solution to this problem I would of course like to know what it is and where it is used.
SML's syntax forbids use of the same operator in unary and binary contexts, so it distinguishes between unary minus and binary minus. SML's unary minus operator is tilde "~", so you could write:
'~9,~5-~3,~1-4,7,11-15'
for the problem example.
Another option could be to use interval syntax:
'-9 [-5 -3] [-1 4] 7 [11 15]'
with or without commas.
Regards, Tony
Smalltalk already has an interval syntax. I suggest you start with that. Try:
{ -9. (-5 to: -3). (-1 to: 4). 7. (11 to: 15)} do: [:each | each asOrderedCollection do: [ :item | Transcript show: item; space]].
Glen
-- View this message in context: http://forum.world.st/converting-lists-of-integers-and-ranges-of-integers-fr... Sent from the Squeak - Dev mailing list archive at Nabble.com.
The comma operator is not currently used for numbers. It's a natural fit for what you're doing. Something like: -9, (-5 to: -3), (-1 to: 4), 7, (11 to: 15). would work cleanly.
-- View this message in context: http://forum.world.st/converting-lists-of-integers-and-ranges-of-integers-fr... Sent from the Squeak - Dev mailing list archive at Nabble.com.
squeak-dev@lists.squeakfoundation.org