Interval.st
changeset 11477 2a46e08433d3
parent 11409 82874ed12ab4
child 11503 f41906ac7352
--- a/Interval.st	Fri Jan 16 17:22:32 2009 +0100
+++ b/Interval.st	Sat Jan 17 11:39:27 2009 +0100
@@ -164,6 +164,35 @@
     stop := aNumber
 ! !
 
+!Interval methodsFor:'bulk operations'!
+
+sum
+    "sum up all elements."
+
+    start < stop ifTrue:[
+        step == 1 ifTrue:[
+            start == 1 ifTrue:[
+                ^ (stop * (stop + 1) / 2)
+            ].
+            ^ (stop * (stop + 1) / 2) - ((start-1) * (start ) / 2)
+        ]
+    ].
+    "/ could add more optimizations here, but who needs them?
+
+    ^ super sum
+
+    "
+     (1 to:10) sum        
+     (1 to:10) asArray sum
+
+     (2 to:10) sum           
+     (2 to:10) asArray sum   
+
+     (5 to:10) sum    
+     (5 to:10) asArray sum     
+    "
+! !
+
 !Interval methodsFor:'comparing'!
 
 = anInterval
@@ -618,5 +647,5 @@
 !Interval class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Interval.st,v 1.47 2008-12-15 11:48:15 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Interval.st,v 1.48 2009-01-17 10:39:27 cg Exp $'
 ! !