#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Tue, 31 Jul 2018 17:48:24 +0200
changeset 23250 8a8ff6bd0cf2
parent 23249 599b1ef2acbe
child 23251 fde6384b96e4
#FEATURE by cg class: Collection added: #atAny:ifAbsent:
Collection.st
--- a/Collection.st	Tue Jul 31 17:07:36 2018 +0200
+++ b/Collection.st	Tue Jul 31 17:48:24 2018 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
               All Rights Reserved
@@ -358,7 +360,6 @@
     ^ self == Collection
 ! !
 
-
 !Collection methodsFor:'Compatibility-ANSI'!
 
 identityIncludes:anObject
@@ -626,6 +627,34 @@
     "Modified: / 20.5.1998 / 14:50:46 / cg"
 !
 
+atAny:aCollectionOfKeysTriedInSequence ifAbsent:absentBlock
+    "try aCollectionOfKeysTriedInSequence and return the element at 
+     the first found valid key.
+     If none of the keys is not present, return the result of evaluating
+     the exceptionblock."
+
+    aCollectionOfKeysTriedInSequence do:[:eachTriedKey |
+        |present value|
+
+        present := true.
+        value := self at:eachTriedKey ifAbsent:[present := false].
+        present ifTrue:[^ value].
+    ].    
+    ^ absentBlock value.
+
+    "
+     |d|
+     d := Dictionary new.
+     d at:'$foo' put:'yes'.
+     d at:'#foo' put:'yes2'.
+     d atAny:#('$foo' '#foo') ifAbsent:['no'].
+     d atAny:#('#foo' '$foo') ifAbsent:['no'].
+     d atAny:#('#bar' '$bar') ifAbsent:['no'].
+    "
+
+    "Created: / 31-07-2018 / 17:47:00 / Claus Gittinger"
+!
+
 decrementAt:aKey 
     "remove 1 from the count stored under aKey.
      If not yet present, assume 0 as initial counter value."
@@ -4612,7 +4641,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
@@ -5971,7 +6000,7 @@
 includesAll:aCollection
     "return true if the receiver includes all elements of
      the argument, aCollection; false if any is missing.
-     Notice: this method has O² runtime behavior and may be
+     Notice: this method has O² runtime behavior and may be
              slow for big receivers/args.
              Think about using a Set, or Dictionary."
 
@@ -5991,7 +6020,7 @@
      Return false if it includes none.
      Uses #= (value compare)
      Notice:
-        this method has O² runtime behavior for some subclasses and may be slow for big receivers/args.
+        this method has 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
         probable elements towards the beginning of aCollection, to avoid useless searches.
@@ -6068,7 +6097,7 @@
      Return false if it includes none.
      Use identity compare for comparing.
      Notice:
-        this method has O² runtime behavior for some subclasses and may be slow for big receivers/args.
+        this method has 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
         probable elements towards the beginning of aCollection, to avoid useless searches."