diff -r a27a279701f8 -r 6526dde5f3ac Interval.st --- a/Interval.st Fri Jul 16 11:39:45 1993 +0200 +++ b/Interval.st Mon Oct 04 11:32:33 1993 +0100 @@ -39,25 +39,21 @@ "return a new interval with elements from start to stop by 1" - ^ self new - setFrom:start - to:stop - by:1 + ^ self new setFrom:start to:stop by:1 ! from:start to:stop by:step "return a new interval with elements from start to stop by step" - ^ self new - setFrom:start - to:stop - by:step + ^ self new setFrom:start to:stop by:step ! ! !Interval methodsFor:'private'! setFrom:startInteger to:stopInteger by:stepInteger + "set start, stop and step components" + start := startInteger. stop := stopInteger. step := stepInteger @@ -71,6 +67,12 @@ ^ start ! +last + "return the last element of the collection" + + ^ stop +! + start "return the first number of the range" @@ -123,6 +125,8 @@ ! at:index + "return (i.e. compute) the index'th element" + (index between:1 and:self size) ifTrue:[ ^ start + (step * (index - 1)) ]. @@ -130,16 +134,22 @@ ! at:index put:anObject + "catch at:put: message - intervals cannot store elements" + self error:'you cannot store into an interval' ! ! !Interval methodsFor:'adding/removing elements'! add:newObject + "catch add message - intervals cannot add elements" + self error:'elements cannot be added to an interval' ! remove:anObject + "catch remove message - intervals cannot remove elements" + self error:'elements cannot be removed from an interval' ! ! @@ -149,9 +159,49 @@ ^ OrderedCollection ! ! +!Interval methodsFor:'printing & storing'! + +printString + "return a string for informal printing" + + ^ self storeString +! + +displayString + ^ 'Interval(' , self storeString , ')' +! + +storeString + "return a string for storing" + + step = 1 ifTrue:[ + ^ start storeString , ' to:' , stop storeString + ]. + ^ start storeString , ' to:' , stop storeString , ' by:' , step storeString + + "(1 to:10) storeString" + "(1 to:10 by:2) storeString" +! + +storeOn:aStream + start storeOn:aStream. + aStream nextPutAll:' to:'. + stop storeOn:aStream. + step ~= 1 ifTrue:[ + aStream nextPutAll:' by:'. + step storeOn:aStream. + ]. + + "(1 to:10) storeOn:Transcript" + "(1 to:10 by:2) storeOn:Transcript" +! ! + !Interval methodsFor:'enumeration'! do:aBlock + "evaluate the argument, aBlock for every element in the + receiver-interval" + |aValue| aValue := start. @@ -176,7 +226,7 @@ |newColl| - newColl := OrderedCollection new:self size. + newColl := self species new:self size. self do:[:each | (aBlock value:each) ifTrue:[newColl add:each] ].