Collection.st
changeset 23378 f9a3eb83bd81
parent 23377 bd1fedce09ef
child 23396 a5cda5279311
--- a/Collection.st	Sat Sep 22 11:21:23 2018 +0200
+++ b/Collection.st	Sat Sep 22 11:24:41 2018 +0200
@@ -2718,6 +2718,24 @@
     "Created: / 22-09-2018 / 11:18:37 / Claus Gittinger"
 !
 
+collectColumn:columnNumberOrKey ifAbsent:replacementValue
+    "for each row-element in the receiver (which ought to be indexable by columnNumberOrKey), 
+     retrieve the indexed element at columnNumberOrKey,
+     and return a new collection with those column values"
+
+    (replacementValue isBlock and:[replacementValue numArgs == 1]) ifTrue:[ 
+        ^ self collect:[:el | el at:columnNumberOrKey ifAbsent:[replacementValue value:el]].
+    ].    
+    ^ self collect:[:el | el at:columnNumberOrKey ifAbsent:replacementValue].
+
+    "
+     #((1 one) (2 two) (3) (4 four)) collectColumn:2 ifAbsent:'foo'
+     #((1 one) (2 two) (3) (4 four)) collectColumn:2 ifAbsent:[:e | e at:1]
+    "
+
+    "Created: / 22-09-2018 / 11:22:57 / Claus Gittinger"
+!
+
 collectWithIndex:aTwoArgBlock
     "for each element in the receiver and a running index, 
      evaluate the argument, aTwoArgBlock.