Browse Source

SequenceableCollection >> copyWithFirst:.

Also, clean uses of with: / addAll:.
Herby Vojčík 4 years ago
parent
commit
ff4307218e

+ 8 - 8
cli/src/AmberCli.js

@@ -93,11 +93,11 @@ selector: "handleArguments:",
 protocol: "commandline",
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["args"],
-source: "handleArguments: args\x0a\x09| selector |\x0a\x0a\x09selector := self selectorForCommandLineSwitch: (args first).\x0a\x09args remove: args first.\x0a\x09self perform: selector  withArguments: (Array with: args)",
-referencedClasses: ["Array"],
+source: "handleArguments: args\x0a\x09| selector |\x0a\x0a\x09selector := self selectorForCommandLineSwitch: (args first).\x0a\x09args remove: args first.\x0a\x09self perform: selector  withArguments: { args }",
+referencedClasses: [],
 //>>excludeEnd("ide");
 pragmas: [],
-messageSends: ["selectorForCommandLineSwitch:", "first", "remove:", "perform:withArguments:", "with:"]
+messageSends: ["selectorForCommandLineSwitch:", "first", "remove:", "perform:withArguments:"]
 }, function ($methodClass){ return function (args){
 var self=this,$self=this;
 var selector;
@@ -111,7 +111,7 @@ $ctx1.sendIdx["first"]=1;
 //>>excludeEnd("ctx");
 selector=$self._selectorForCommandLineSwitch_($1);
 $recv(args)._remove_($recv(args)._first());
-$self._perform_withArguments_(selector,$recv($globals.Array)._with_(args));
+$self._perform_withArguments_(selector,[args]);
 return self;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"handleArguments:",{args:args,selector:selector})});
@@ -1803,11 +1803,11 @@ selector: "createServerWithArguments:",
 protocol: "initialization",
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["options"],
-source: "createServerWithArguments: options\x0a\x09\x22If options are empty return a default FileServer instance.\x0a\x09 If options are given loop through them and set the passed in values\x0a\x09 on the FileServer instance.\x0a\x09 \x0a\x09 Commanline options map directly to methods in the 'accessing' protocol\x0a\x09 taking one parameter.\x0a\x09 Adding a method to this protocol makes it directly settable through\x0a\x09 command line options.\x0a\x09 \x22\x0a\x09| server popFront front optionName optionValue switches |\x0a\x0a\x09switches := self commandLineSwitches.\x0a\x0a\x09server := self new.\x0a\x0a\x09options ifEmpty: [^server].\x0a\x0a\x09(options size even) ifFalse: [\x0a\x09\x09console log: 'Using default parameters.'.\x0a\x09\x09console log: 'Wrong commandline options or not enough arguments for: ' , options.\x0a\x09\x09console log: 'Use any of the following ones: ', switches.\x0a\x09\x09^server].\x0a\x0a\x09popFront := [:args |\x0a\x09\x09front := args first.\x0a\x09\x09args remove: front.\x0a\x09\x09front].\x0a\x0a\x09[options notEmpty] whileTrue: [\x0a\x09\x09optionName  := popFront value: options.\x0a\x09\x09optionValue := popFront value: options.\x0a\x0a\x09\x09(switches includes: optionName) ifTrue: [\x0a\x09\x09\x09optionName := self selectorForCommandLineSwitch: optionName.\x0a\x09\x09\x09server perform: optionName withArguments: (Array with: optionValue)]\x0a\x09\x09\x09ifFalse: [\x0a\x09\x09\x09\x09console log: optionName, ' is not a valid commandline option'.\x0a\x09\x09\x09\x09console log: 'Use any of the following ones: ', switches ]].\x0a\x09^ server.",
-referencedClasses: ["Array"],
+source: "createServerWithArguments: options\x0a\x09\x22If options are empty return a default FileServer instance.\x0a\x09 If options are given loop through them and set the passed in values\x0a\x09 on the FileServer instance.\x0a\x09 \x0a\x09 Commanline options map directly to methods in the 'accessing' protocol\x0a\x09 taking one parameter.\x0a\x09 Adding a method to this protocol makes it directly settable through\x0a\x09 command line options.\x0a\x09 \x22\x0a\x09| server popFront front optionName optionValue switches |\x0a\x0a\x09switches := self commandLineSwitches.\x0a\x0a\x09server := self new.\x0a\x0a\x09options ifEmpty: [^server].\x0a\x0a\x09(options size even) ifFalse: [\x0a\x09\x09console log: 'Using default parameters.'.\x0a\x09\x09console log: 'Wrong commandline options or not enough arguments for: ' , options.\x0a\x09\x09console log: 'Use any of the following ones: ', switches.\x0a\x09\x09^server].\x0a\x0a\x09popFront := [:args |\x0a\x09\x09front := args first.\x0a\x09\x09args remove: front.\x0a\x09\x09front].\x0a\x0a\x09[options notEmpty] whileTrue: [\x0a\x09\x09optionName  := popFront value: options.\x0a\x09\x09optionValue := popFront value: options.\x0a\x0a\x09\x09(switches includes: optionName) ifTrue: [\x0a\x09\x09\x09optionName := self selectorForCommandLineSwitch: optionName.\x0a\x09\x09\x09server perform: optionName withArguments: { optionValue } ]\x0a\x09\x09\x09ifFalse: [\x0a\x09\x09\x09\x09console log: optionName, ' is not a valid commandline option'.\x0a\x09\x09\x09\x09console log: 'Use any of the following ones: ', switches ]].\x0a\x09^ server.",
+referencedClasses: [],
 //>>excludeEnd("ide");
 pragmas: [],
-messageSends: ["commandLineSwitches", "new", "ifEmpty:", "ifFalse:", "even", "size", "log:", ",", "first", "remove:", "whileTrue:", "notEmpty", "value:", "ifTrue:ifFalse:", "includes:", "selectorForCommandLineSwitch:", "perform:withArguments:", "with:"]
+messageSends: ["commandLineSwitches", "new", "ifEmpty:", "ifFalse:", "even", "size", "log:", ",", "first", "remove:", "whileTrue:", "notEmpty", "value:", "ifTrue:ifFalse:", "includes:", "selectorForCommandLineSwitch:", "perform:withArguments:"]
 }, function ($methodClass){ return function (options){
 var self=this,$self=this;
 var server,popFront,front,optionName,optionValue,switches;
@@ -1880,7 +1880,7 @@ optionValue=$recv(popFront)._value_(options);
 $6=$recv(switches)._includes_(optionName);
 if($core.assert($6)){
 optionName=$self._selectorForCommandLineSwitch_(optionName);
-return $recv(server)._perform_withArguments_(optionName,$recv($globals.Array)._with_(optionValue));
+return $recv(server)._perform_withArguments_(optionName,[optionValue]);
 } else {
 $7=console;
 $8=$recv(optionName).__comma(" is not a valid commandline option");

+ 2 - 2
cli/src/AmberCli.st

@@ -38,7 +38,7 @@ handleArguments: args
 
 	selector := self selectorForCommandLineSwitch: (args first).
 	args remove: args first.
-	self perform: selector  withArguments: (Array with: args)
+	self perform: selector  withArguments: { args }
 !
 
 selectorForCommandLineSwitch: aSwitch
@@ -954,7 +954,7 @@ createServerWithArguments: options
 
 		(switches includes: optionName) ifTrue: [
 			optionName := self selectorForCommandLineSwitch: optionName.
-			server perform: optionName withArguments: (Array with: optionValue)]
+			server perform: optionName withArguments: { optionValue } ]
 			ifFalse: [
 				console log: optionName, ' is not a valid commandline option'.
 				console log: 'Use any of the following ones: ', switches ]].

+ 5 - 7
lang/src/Compiler-AST.js

@@ -1783,17 +1783,17 @@ selector: "dagChildren",
 protocol: "accessing",
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: [],
-source: "dagChildren\x0a\x09self receiver ifNil: [ ^ self arguments copy ].\x0a\x09\x0a\x09^ (Array with: self receiver)\x0a\x09\x09addAll: self arguments;\x0a\x09\x09yourself",
-referencedClasses: ["Array"],
+source: "dagChildren\x0a\x09self receiver ifNil: [ ^ self arguments copy ].\x0a\x09\x0a\x09^ self arguments copyWithFirst: self receiver",
+referencedClasses: [],
 //>>excludeEnd("ide");
 pragmas: [],
-messageSends: ["ifNil:", "receiver", "copy", "arguments", "addAll:", "with:", "yourself"]
+messageSends: ["ifNil:", "receiver", "copy", "arguments", "copyWithFirst:"]
 }, function ($methodClass){ return function (){
 var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-var $1,$2,$3,$receiver;
+var $1,$2,$receiver;
 $1=$self._receiver();
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 $ctx1.sendIdx["receiver"]=1;
@@ -1807,9 +1807,7 @@ return $recv($2)._copy();
 } else {
 $1;
 }
-$3=$recv($globals.Array)._with_($self._receiver());
-$recv($3)._addAll_($self._arguments());
-return $recv($3)._yourself();
+return $recv($self._arguments())._copyWithFirst_($self._receiver());
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"dagChildren",{})});
 //>>excludeEnd("ctx");

+ 1 - 3
lang/src/Compiler-AST.st

@@ -450,9 +450,7 @@ arguments: aCollection
 dagChildren
 	self receiver ifNil: [ ^ self arguments copy ].
 	
-	^ (Array with: self receiver)
-		addAll: self arguments;
-		yourself
+	^ self arguments copyWithFirst: self receiver
 !
 
 index

+ 3 - 6
lang/src/Compiler-IR.js

@@ -1761,19 +1761,17 @@ selector: "locals",
 protocol: "accessing",
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: [],
-source: "locals\x0a\x09^ self arguments copy\x0a\x09\x09addAll: (self tempDeclarations collect: [ :each | each name ]);\x0a\x09\x09yourself",
+source: "locals\x0a\x09^ self arguments, (self tempDeclarations collect: [ :each | each name ])\x0a",
 referencedClasses: [],
 //>>excludeEnd("ide");
 pragmas: [],
-messageSends: ["addAll:", "copy", "arguments", "collect:", "tempDeclarations", "name", "yourself"]
+messageSends: [",", "arguments", "collect:", "tempDeclarations", "name"]
 }, function ($methodClass){ return function (){
 var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-var $1;
-$1=$recv($self._arguments())._copy();
-$recv($1)._addAll_($recv($self._tempDeclarations())._collect_((function(each){
+return $recv($self._arguments()).__comma($recv($self._tempDeclarations())._collect_((function(each){
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx2) {
 //>>excludeEnd("ctx");
@@ -1782,7 +1780,6 @@ return $recv(each)._name();
 }, function($ctx2) {$ctx2.fillBlock({each:each},$ctx1,1)});
 //>>excludeEnd("ctx");
 })));
-return $recv($1)._yourself();
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"locals",{})});
 //>>excludeEnd("ctx");

+ 1 - 3
lang/src/Compiler-IR.st

@@ -434,9 +434,7 @@ arguments: aCollection
 !
 
 locals
-	^ self arguments copy
-		addAll: (self tempDeclarations collect: [ :each | each name ]);
-		yourself
+	^ self arguments, (self tempDeclarations collect: [ :each | each name ])
 !
 
 requiresSmalltalkContext

+ 4 - 4
lang/src/Compiler-Inlining.js

@@ -232,17 +232,17 @@ selector: "internalVariables",
 protocol: "accessing",
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: [],
-source: "internalVariables\x0a\x09^ Array with: self receiverInternalVariable",
-referencedClasses: ["Array"],
+source: "internalVariables\x0a\x09^ { self receiverInternalVariable }",
+referencedClasses: [],
 //>>excludeEnd("ide");
 pragmas: [],
-messageSends: ["with:", "receiverInternalVariable"]
+messageSends: ["receiverInternalVariable"]
 }, function ($methodClass){ return function (){
 var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-return $recv($globals.Array)._with_($self._receiverInternalVariable());
+return [$self._receiverInternalVariable()];
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"internalVariables",{})});
 //>>excludeEnd("ctx");

+ 1 - 1
lang/src/Compiler-Inlining.st

@@ -82,7 +82,7 @@ I represent an inlined `#ifNil:ifNotNil:` message send instruction.!
 !IRInlinedIfNilIfNotNil methodsFor: 'accessing'!
 
 internalVariables
-	^ Array with: self receiverInternalVariable
+	^ { self receiverInternalVariable }
 !
 
 receiverInternalVariable

+ 9 - 13
lang/src/Kernel-Classes.js

@@ -152,11 +152,11 @@ selector: "allSuperclasses",
 protocol: "accessing",
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: [],
-source: "allSuperclasses\x0a\x09\x0a\x09self superclass ifNil: [ ^ #() ].\x0a\x09\x0a\x09^ (OrderedCollection with: self superclass)\x0a\x09\x09addAll: self superclass allSuperclasses;\x0a\x09\x09yourself",
-referencedClasses: ["OrderedCollection"],
+source: "allSuperclasses\x0a\x09\x0a\x09self superclass ifNil: [ ^ #() ].\x0a\x09\x0a\x09^ self superclass allSuperclasses copyWithFirst: self superclass",
+referencedClasses: [],
 //>>excludeEnd("ide");
 pragmas: [],
-messageSends: ["ifNil:", "superclass", "addAll:", "with:", "allSuperclasses", "yourself"]
+messageSends: ["ifNil:", "superclass", "copyWithFirst:", "allSuperclasses"]
 }, function ($methodClass){ return function (){
 var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
@@ -176,9 +176,8 @@ $3=$self._superclass();
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 $ctx1.sendIdx["superclass"]=2;
 //>>excludeEnd("ctx");
-$2=$recv($globals.OrderedCollection)._with_($3);
-$recv($2)._addAll_($recv($self._superclass())._allSuperclasses());
-return $recv($2)._yourself();
+$2=$recv($3)._allSuperclasses();
+return $recv($2)._copyWithFirst_($self._superclass());
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"allSuperclasses",{})});
 //>>excludeEnd("ctx");
@@ -789,20 +788,17 @@ selector: "withAllSubclasses",
 protocol: "accessing",
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: [],
-source: "withAllSubclasses\x0a\x09^ (Array with: self) addAll: self allSubclasses; yourself",
-referencedClasses: ["Array"],
+source: "withAllSubclasses\x0a\x09^ self allSubclasses copyWithFirst: self",
+referencedClasses: [],
 //>>excludeEnd("ide");
 pragmas: [],
-messageSends: ["addAll:", "with:", "allSubclasses", "yourself"]
+messageSends: ["copyWithFirst:", "allSubclasses"]
 }, function ($methodClass){ return function (){
 var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-var $1;
-$1=$recv($globals.Array)._with_(self);
-$recv($1)._addAll_($self._allSubclasses());
-return $recv($1)._yourself();
+return $recv($self._allSubclasses())._copyWithFirst_(self);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"withAllSubclasses",{})});
 //>>excludeEnd("ctx");

+ 2 - 4
lang/src/Kernel-Classes.st

@@ -39,9 +39,7 @@ allSuperclasses
 	
 	self superclass ifNil: [ ^ #() ].
 	
-	^ (OrderedCollection with: self superclass)
-		addAll: self superclass allSuperclasses;
-		yourself
+	^ self superclass allSuperclasses copyWithFirst: self superclass
 !
 
 applySuperConstructorOn: anObject withArguments: anArray
@@ -132,7 +130,7 @@ theNonMetaClass
 !
 
 withAllSubclasses
-	^ (Array with: self) addAll: self allSubclasses; yourself
+	^ self allSubclasses copyWithFirst: self
 ! !
 
 !Behavior methodsFor: 'enumerating'!

+ 70 - 0
lang/src/Kernel-Collections.js

@@ -3874,6 +3874,32 @@ return self;
 }; }),
 $globals.SequenceableCollection);
 
+$core.addMethod(
+$core.method({
+selector: "copyWithFirst:",
+protocol: "copying",
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["anObject"],
+source: "copyWithFirst: anObject\x0a\x09^ (self class with: anObject) addAll: self; yourself",
+referencedClasses: [],
+//>>excludeEnd("ide");
+pragmas: [],
+messageSends: ["addAll:", "with:", "class", "yourself"]
+}, function ($methodClass){ return function (anObject){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1;
+$1=$recv($self._class())._with_(anObject);
+$recv($1)._addAll_(self);
+return $recv($1)._yourself();
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"copyWithFirst:",{anObject:anObject})});
+//>>excludeEnd("ctx");
+}; }),
+$globals.SequenceableCollection);
+
 $core.addMethod(
 $core.method({
 selector: "endsWith:",
@@ -5775,6 +5801,50 @@ return self;
 }; }),
 $globals.String);
 
+$core.addMethod(
+$core.method({
+selector: "copyWithFirst:",
+protocol: "copying",
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["anObject"],
+source: "copyWithFirst: anObject\x0a\x09(anObject isString and: [ anObject size = 1 ]) \x22character is one-char string in JS\x22\x0a\x09\x09ifFalse: [ self error: 'Cannot put ', anObject class name, ' in a String' ].\x0a\x09^ anObject, self",
+referencedClasses: [],
+//>>excludeEnd("ide");
+pragmas: [],
+messageSends: ["ifFalse:", "and:", "isString", "=", "size", "error:", ",", "name", "class"]
+}, function ($methodClass){ return function (anObject){
+var self=this,$self=this;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1,$3,$2;
+$1=$recv($recv(anObject)._isString())._and_((function(){
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx2) {
+//>>excludeEnd("ctx");
+return $recv($recv(anObject)._size()).__eq((1));
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx2) {$ctx2.fillBlock({},$ctx1,1)});
+//>>excludeEnd("ctx");
+}));
+if(!$core.assert($1)){
+$3="Cannot put ".__comma($recv($recv(anObject)._class())._name());
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx[","]=2;
+//>>excludeEnd("ctx");
+$2=$recv($3).__comma(" in a String");
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+$ctx1.sendIdx[","]=1;
+//>>excludeEnd("ctx");
+$self._error_($2);
+}
+return $recv(anObject).__comma(self);
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"copyWithFirst:",{anObject:anObject})});
+//>>excludeEnd("ctx");
+}; }),
+$globals.String);
+
 $core.addMethod(
 $core.method({
 selector: "crlfSanitized",

+ 10 - 0
lang/src/Kernel-Collections.st

@@ -974,6 +974,10 @@ reversed
 
 copyFrom: anIndex to: anotherIndex
 	self subclassResponsibility
+!
+
+copyWithFirst: anObject
+	^ (self class with: anObject) addAll: self; yourself
 ! !
 
 !SequenceableCollection methodsFor: 'enumerating'!
@@ -1388,6 +1392,12 @@ copyFrom: anIndex to: anotherIndex
 	<inlineJS: 'return self.substring(anIndex - 1, anotherIndex)'>
 !
 
+copyWithFirst: anObject
+	(anObject isString and: [ anObject size = 1 ]) "character is one-char string in JS"
+		ifFalse: [ self error: 'Cannot put ', anObject class name, ' in a String' ].
+	^ anObject, self
+!
+
 deepCopy
 	^ self shallowCopy
 !