Browse Source

Push up generic #detect:ifNone: to Collection.

Herby Vojčík 5 years ago
parent
commit
8f74948394
3 changed files with 24 additions and 50 deletions
  1. 2 0
      lang/API-CHANGES.txt
  2. 20 44
      lang/src/Kernel-Collections.js
  3. 2 6
      lang/src/Kernel-Collections.st

+ 2 - 0
lang/API-CHANGES.txt

@@ -5,6 +5,8 @@
 
 - Array >>
   - numericallyIndexable
+- Set
+  - detect:ifNone: (pushed up to Collection)
 - SequenceableCollection
   - numericallyIndexable
 - String >>

+ 20 - 44
lang/src/Kernel-Collections.js

@@ -944,18 +944,34 @@ var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-$self._subclassResponsibility();
-return self;
+var $1;
+var $early={};
+try {
+$self._do_((function(each){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx2) {
+//>>excludeEnd("ctx");
+$1=$recv(aBlock)._value_(each);
+if($core.assert($1)){
+throw $early=[each];
+}
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1,1)});
+//>>excludeEnd("ctx");
+}));
+return $recv(anotherBlock)._value();
+}
+catch(e) {if(e===$early)return e[0]; throw e}
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"detect:ifNone:",{aBlock:aBlock,anotherBlock:anotherBlock},$globals.Collection)});
 //>>excludeEnd("ctx");
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["aBlock", "anotherBlock"],
-source: "detect: aBlock ifNone: anotherBlock\x0a\x09self subclassResponsibility",
+source: "detect: aBlock ifNone: anotherBlock\x0a\x09self do: [ :each | (aBlock value: each) ifTrue: [ ^each ] ].\x0a\x09^ anotherBlock value",
 referencedClasses: [],
 //>>excludeEnd("ide");
-messageSends: ["subclassResponsibility"]
+messageSends: ["do:", "ifTrue:", "value:", "value"]
 }),
 $globals.Collection);
 
@@ -7968,46 +7984,6 @@ messageSends: ["new", "class", "do:", "add:", "value:"]
 }),
 $globals.Set);
 
-$core.addMethod(
-$core.method({
-selector: "detect:ifNone:",
-protocol: "enumerating",
-fn: function (aBlock,anotherBlock){
-var self=this,$self=this;
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-return $core.withContext(function($ctx1) {
-//>>excludeEnd("ctx");
-var $1;
-var $early={};
-try {
-$self._do_((function(each){
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-return $core.withContext(function($ctx2) {
-//>>excludeEnd("ctx");
-$1=$recv(aBlock)._value_(each);
-if($core.assert($1)){
-throw $early=[each];
-}
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1,1)});
-//>>excludeEnd("ctx");
-}));
-return $recv(anotherBlock)._value();
-}
-catch(e) {if(e===$early)return e[0]; throw e}
-//>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx1) {$ctx1.fill(self,"detect:ifNone:",{aBlock:aBlock,anotherBlock:anotherBlock},$globals.Set)});
-//>>excludeEnd("ctx");
-},
-//>>excludeStart("ide", pragmas.excludeIdeData);
-args: ["aBlock", "anotherBlock"],
-source: "detect: aBlock ifNone: anotherBlock\x0a\x09self do: [ :each | (aBlock value: each) ifTrue: [ ^each ] ].\x0a\x09^ anotherBlock value",
-referencedClasses: [],
-//>>excludeEnd("ide");
-messageSends: ["do:", "ifTrue:", "value:", "value"]
-}),
-$globals.Set);
-
 $core.addMethod(
 $core.method({
 selector: "do:",

+ 2 - 6
lang/src/Kernel-Collections.st

@@ -292,7 +292,8 @@ detect: aBlock
 !
 
 detect: aBlock ifNone: anotherBlock
-	self subclassResponsibility
+	self do: [ :each | (aBlock value: each) ifTrue: [ ^each ] ].
+	^ anotherBlock value
 !
 
 do: aBlock
@@ -1891,11 +1892,6 @@ collect: aBlock
 	^ collection
 !
 
-detect: aBlock ifNone: anotherBlock
-	self do: [ :each | (aBlock value: each) ifTrue: [ ^each ] ].
-	^ anotherBlock value
-!
-
 do: aBlock
 	<inlineJS: '
 		var el, keys, i;