Collection.st
changeset 24727 41db8f85cade
parent 24722 ca69eab2b18e
child 24730 f4482f8c792e
--- a/Collection.st	Wed Sep 04 09:01:52 2019 +0200
+++ b/Collection.st	Wed Sep 04 16:16:06 2019 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
               All Rights Reserved
@@ -602,8 +604,7 @@
 
 at:aKey ifAbsent:absentBlock
     "return the element at aKey if valid.
-     If the key is not present, return the result of evaluating
-     the exceptionblock.
+     If the key is not present, return the result from evaluating the absentBlock.
      NOTICE:
         in ST-80, this message is only defined for Dictionaries,
         however, having a common protocol with indexed collections
@@ -679,6 +680,32 @@
     "Modified: / 28-04-2017 / 14:09:43 / stefan"
 !
 
+at:aKey ifPresent:presentBlock
+    "try to retrieve the value stored at aKey.
+     If there is nothing stored under this key, do nothing.
+     Otherwise, evaluate aBlock, passing the retrieved value as argument."
+
+    |v|
+
+    v := self at:aKey ifAbsent:[^ nil].
+    ^ presentBlock value:v.
+
+    "
+     |d|
+     d := Dictionary new.
+     d at:'foo' put:'bar'.   
+     d at:'foo' ifPresent:[:val | Transcript showCR:'foo is: %1' with:val]. 
+     d at:'bla' ifPresent:[:val | Transcript showCR:'bla is: %1' with:val].
+    "
+
+    "
+     |a|
+     a := #(10 20 30).
+     a at:2 ifPresent:[:val | Transcript showCR:'[2] is: %1' with:val]. 
+     a at:4 ifPresent:[:val | Transcript showCR:'[4] is: %1' with:val]. 
+    "
+!
+
 atAll:indexCollection put:anObject
     "put anObject into all indexes from indexCollection in the receiver.
      This abstract implementation requires that the receiver supports
@@ -4976,7 +5003,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
@@ -5155,7 +5182,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(n²) runtime behavior,
+             this method may have O(n²) runtime behavior,
              and may be slow for big receivers/args.
              Think about using a Set, or Dictionary."
 
@@ -5197,7 +5224,7 @@
      Uses #= (value compare)
      Notice: 
         depending on the concrete collection,
-        this method may have O(n²) runtime behavior,
+        this method may have O(n²) runtime behavior,
         and may be slow for big receivers/args.
         Think about using a Set, or Dictionary.
 
@@ -5279,7 +5306,7 @@
      Use identity compare for comparing.
      Notice:
         depending on the concrete collection,
-        this method may have O(n²) runtime behavior for some subclasses
+        this method may have O(n²) 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
@@ -5306,7 +5333,7 @@
      false if none is present.
      Notice:
         depending on the concrete collection,
-        this method may have O(n²) runtime behavior for some subclasses
+        this method may have O(n²) runtime behavior for some subclasses
         and may be slow for big receivers/args.
         Think about using a Set or Dictionary."