Collection.st
changeset 23713 84ba0efb57f7
parent 23707 d7ceab043a39
child 23730 79e4b8567d49
--- a/Collection.st	Sun Feb 10 15:40:54 2019 +0100
+++ b/Collection.st	Sun Feb 10 19:32:03 2019 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
               All Rights Reserved
@@ -295,8 +297,6 @@
     ^ self newWithSize:n
 ! !
 
-
-
 !Collection class methodsFor:'Signal constants'!
 
 emptyCollectionSignal
@@ -358,7 +358,6 @@
     ^ self == Collection
 ! !
 
-
 !Collection methodsFor:'Compatibility-ANSI'!
 
 identityIncludes:anObject
@@ -368,7 +367,6 @@
     ^ self includesIdentical:anObject.
 ! !
 
-
 !Collection methodsFor:'Compatibility-Squeak'!
 
 , aCollection
@@ -529,7 +527,6 @@
     ^ self ifEmpty:ifEmptyValue ifNotEmpty:ifNotEmptyValue
 ! !
 
-
 !Collection methodsFor:'accessing'!
 
 anElement
@@ -2526,6 +2523,31 @@
     "Created: / 15-03-2017 / 18:19:03 / cg"
 !
 
+and:finalValue inject:thisValue into:binaryBlock
+    "starting with thisValue for value, pass this value and each element
+     to binaryBlock, replacing value with the result returned from the block
+     in the next iteration. 
+     As a last step, inject finalValue.
+     This last injection is useful to signal end-of-input to the block;
+     typically, a nil or other marker is injected as finalValue.
+
+     See also: #fold: #reduce:"
+
+    |nextValue|
+
+    nextValue := thisValue.
+    self do: [:each | nextValue := binaryBlock value:nextValue value:each].
+    ^ binaryBlock value:nextValue value:finalValue.
+
+    "
+     #(1 2 3 4) and:5 inject:0 into:[:accu :element | accu + element]   
+     (1 to:10) and:1000 inject:0 into:[:accu :element | accu + element]     
+     (1 to:10) and:1000 inject:0 into:#+     
+    "
+
+    "Created: / 10-02-2019 / 19:30:37 / Claus Gittinger"
+!
+
 collect:aBlockOrSymbol
     "for each element in the receiver, evaluate the argument, aBlock
      and return a new collection with the results"
@@ -4745,7 +4767,7 @@
     aStream nextPut:$)
 
     "
-     #(1 2 3 'hello' $a $ü) printOn:Transcript
+     #(1 2 3 'hello' $a $ü) printOn:Transcript
      (Array new:100000) printOn:Transcript
      (Array new:100000) printOn:Stdout
      (Array new:100000) printString size
@@ -4921,7 +4943,7 @@
     "return true if the receiver includes all elements of
      the argument, aCollection; false if any is missing.
      Notice: depending on the concrete collection,
-             this method may have O² runtime behavior,
+             this method may have O² runtime behavior,
              and may be slow for big receivers/args.
              Think about using a Set, or Dictionary."
 
@@ -4958,7 +4980,7 @@
      Uses #= (value compare)
      Notice: 
         depending on the concrete collection,
-        this method may have O² runtime behavior,
+        this method may have O² runtime behavior,
         and may be slow for big receivers/args.
         Think about using a Set, or Dictionary.
 
@@ -5040,7 +5062,7 @@
      Use identity compare for comparing.
      Notice:
         depending on the concrete collection,
-        this method may have O² runtime behavior for some subclasses
+        this method may have O² runtime behavior for some subclasses
         and may be slow for big receivers/args.
         Think about using a Set or Dictionary.
         Some speedup is also possible, by arranging highly
@@ -6457,7 +6479,6 @@
     ^ aVisitor visitCollection:self with:aParameter
 ! !
 
-
 !Collection class methodsFor:'documentation'!
 
 version