#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Thu, 18 Jan 2018 20:05:56 +0100
changeset 22450 d3ec08e36f6c
parent 22449 8e4bad4c954a
child 22451 0748418b3412
#FEATURE by cg class: SequenceableCollection added: #findFirst:startingAt:ifNone: changed: #findFirst:startingAt:
SequenceableCollection.st
--- a/SequenceableCollection.st	Thu Jan 18 19:44:02 2018 +0100
+++ b/SequenceableCollection.st	Thu Jan 18 20:05:56 2018 +0100
@@ -7334,6 +7334,24 @@
      This is much like #detect:startingAt:, however, here an INDEX is returned,
      whereas #detect: returns the element."
 
+    ^ self findFirst:aBlock startingAt:startIndex ifNone:0
+
+    "
+     #(1 4 3 4 3 6) findFirst:[:x | (x > 3)] startingAt:4    
+     'one.two.three' findFirst:[:c | (c == $.)] startingAt:5 
+     'one.two.three' findFirst:[:c | (c == $.)] startingAt:9 
+    "
+
+    "Modified: / 21.10.1998 / 18:48:22 / cg"
+!
+
+findFirst:aBlock startingAt:startIndex ifNone:exceptionalValue
+    "find the first element, for which evaluation of the argument, aBlock returns true.
+     Start the search at startIndex.
+     Return its index or 0 if none detected.
+     This is much like #detect:startingAt:, however, here an INDEX is returned,
+     whereas #detect: returns the element."
+
     |start "{ Class: SmallInteger }"
      stop  "{ Class: SmallInteger }" |
 
@@ -7342,11 +7360,13 @@
     start to:stop do:[:index |
         (aBlock value:(self at:index)) ifTrue:[^ index].
     ].
-    ^ 0
+    ^ exceptionalValue value
 
     "
      #(1 4 3 4 3 6) findFirst:[:x | (x > 3)] startingAt:4
      'one.two.three' findFirst:[:c | (c == $.)] startingAt:5
+     'one.two.three' findFirst:[:c | (c == $.)] startingAt:10
+     'one.two.three' findFirst:[:c | (c == $.)] startingAt:10 ifNone:nil
     "
 
     "Modified: / 21.10.1998 / 18:48:22 / cg"