Adding collections from different instances

I am new to Smalltalk, and I am trying to make a very simple program that will create a single collection of several collections in this way:

Let's say I have a Set of Armory, and each Armory has its own Set of Weapons, what I would like to do is write a method that will return a single collection with all the Arms from each Armory.

Thanks in advance for your help!

+4
source share
3 answers

Try something like this:

armories inject: OrderedCollection new into: [:collection :armory |
   collection addAll: armory weapons; yourself].
+7
source

I would like to improve David's answer by proposing this solution:

armories inject: OrderedCollection new into: [:allWeapons :armory |
  allWeapons, armory weapons]

As ,returns concatenation from 2 collections.

"" OrderedCollection. fold: reduce: 'reduceLeft: `, . , :

(armories collect: #weapons) fold: [allWeapons :weapons |
  allWeapons, weapons]

, armories collect: #weapons . fold: 1- 2- . .....

, , flatCollect:. , , , . , collect:, . , , , :

armories flatCollect: #weapons

Enjoy

+4

- . , :

OrderedCollection fromStream: [:allWeapons |
    allWeapons nextPutAll: armories collect: [:armory | armory weapons]]
+1

All Articles