Browse Source

add atLyst:ifAbsent:put:

Herbert Vojčík 8 years ago
parent
commit
e3ba12697f
2 changed files with 34 additions and 1 deletions
  1. 28 1
      src/Lyst.js
  2. 6 0
      src/Lyst.st

+ 28 - 1
src/Lyst.js

@@ -1,4 +1,4 @@
-define("lyst/Lyst", ["amber/boot", "amber_core/Kernel-Objects", "amber_core/Kernel-Collections"], function($boot){
+define("lyst/Lyst", ["amber/boot", "amber_core/Kernel-Objects", "amber_core/Kernel-Collections"], function($boot){"use strict";
 var $core=$boot.api,nil=$boot.nil,$recv=$boot.asReceiver,$globals=$boot.globals;
 $core.addPackage('Lyst');
 $core.packages["Lyst"].innerEval = function (expr) { return eval(expr); };
@@ -497,6 +497,33 @@ messageSends: ["inject:into:", "atYndexIn:ifAbsent:", "value"]
 }),
 $globals.Object);
 
+$core.addMethod(
+$core.method({
+selector: "atLyst:ifAbsent:put:",
+protocol: '*Lyst',
+fn: function (aCollection,aBlock,value){
+var self=this;
+var penultimate;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+return $core.withContext(function($ctx1) {
+//>>excludeEnd("ctx");
+var $1;
+penultimate=self._atLyst_ifAbsent_($recv(aCollection)._allButLast(),aBlock);
+$1=$recv($recv(aCollection)._last())._atYndexIn_ifAbsent_put_(penultimate,aBlock,value);
+return $1;
+//>>excludeStart("ctx", pragmas.excludeDebugContexts);
+}, function($ctx1) {$ctx1.fill(self,"atLyst:ifAbsent:put:",{aCollection:aCollection,aBlock:aBlock,value:value,penultimate:penultimate},$globals.Object)});
+//>>excludeEnd("ctx");
+},
+//>>excludeStart("ide", pragmas.excludeIdeData);
+args: ["aCollection", "aBlock", "value"],
+source: "atLyst: aCollection ifAbsent: aBlock put: value\x0a\x09| penultimate |\x0a\x09penultimate := self atLyst: aCollection allButLast ifAbsent: aBlock.\x0a\x09^ aCollection last atYndexIn: penultimate ifAbsent: aBlock put: value",
+referencedClasses: [],
+//>>excludeEnd("ide");
+messageSends: ["atLyst:ifAbsent:", "allButLast", "atYndexIn:ifAbsent:put:", "last"]
+}),
+$globals.Object);
+
 $core.addMethod(
 $core.method({
 selector: "atYndexIn:ifAbsent:",

+ 6 - 0
src/Lyst.st

@@ -78,6 +78,12 @@ atLyst: aCollection ifAbsent: aBlock
 		segment atYndexIn: soFar ifAbsent: [ ^ aBlock value ]]
 !
 
+atLyst: aCollection ifAbsent: aBlock put: value
+	| penultimate |
+	penultimate := self atLyst: aCollection allButLast ifAbsent: aBlock.
+	^ aCollection last atYndexIn: penultimate ifAbsent: aBlock put: value
+!
+
 atYndexIn: anObject ifAbsent: aBlock
 	^ aBlock value
 !