> For the complete documentation index, see [llms.txt](https://mind-expression-docs.gitbook.io/home/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mind-expression-docs.gitbook.io/home/how-to-guides/intelligent-process/package-collections-unordered.md).

# Package: Collections Unordered

## Class: Bag

### Introduction

**Environment: `container`**

A Bag is an unordered collection which can hold any type of item. It can have multiple copies of the same item (unlike sets).

It has the following common used methods:

* `size` return the size of the list.
* `first` return the first element of the list.
* `rest` return the rest of the list.
* `at:` get the element at the given index.
* `do:` iterate over the elements of the list.

For example:

```smalltalk
aBag := {1.'a string'} asBag.
aBag size.  "2"

aBag do: [ :each | Console print: each, '. '].
```

### Class Method

#### Category: instance creation

* **`new`** - Answer a new instance of the receiver.

  ```smalltalk
  bg := Bag new.
  bg add: 'a'. "bg contains 'a'"
  ```
* **`new:`** - Answer a new instance of the receiver, with space for size distinct items.

  ```smalltalk
  bg := Bag new: 5. "bg is 5 long, all empty values"
  ```

### Instance Method

#### Category: accessing

* **`add:`** - newObject Add an occurrence of newObject to the receiver. Answer newObject. Fail if newObject is nil.

  For example:

  ```smalltalk
  bg := Bag new.
  bg add: 1. "bg now contains { 1 }"
  ```
* **`includes:`** - Answer whether we include anObject.

  For example:

  ```smalltalk
  bg := { 'a'. 'a'. 'b' } asBag.
  (bg includes: 'a') = true.
  (bg includes: 'c') = false.
  ```
* **`size`** - Answer the total number of objects found in the receiver anObject.

#### Category: comparing

* **`=`** - Answer whether the receiver and aBag contain the same objects.

  For example:

  ```smalltalk
  bg1 := { 2. 3. 2. 1 } asBag.
  bg2 := { 3. 2. 1. 2 } asBag.
  "This is true:"
  bg1 = bg2.
  ```

#### Category: converting

* **`asSet`** - Answer as an array with the set as the distinct elements within the receiver.

  For example:

  ```smalltalk
  bg := { 2. 2 } asBag.
  bv := bg asSet. "bv = { 2 }"
  ```

#### Category: enumerating

* **`allSatisfy:`** - Search the receiver for an element for which aBlock returns false. Answer true if none does, false otherwise

  For example:

  ```smalltalk
  (({ 1. 2 } asBag) allSatisfy: [ :each | each even ]) = false.
  (({ 2. 4 } asBag) allSatisfy: [ :each | each even ]) = true.
  (({ 2. 4 } asBag) allSatisfy: [ :each | each < 5 ]) = true.
  (({ } asBag) allSatisfy: [false]) = true.
  ```
* **`anySatisfy:`** - Evaluate a block with each of the receiver's elements as the argument. Answer true if any block answers true; otherwise, answer false.

  For example:

  ```smalltalk
  ({ 1. 2. 3 } asBag) anySatisfy: #even. "Return true."
  ({ 1. 2. 3 } asBag) anySatisfy:  [ :x | x = 4 ]. "Return false."
  ```
* **`count:`** - Count the elements of the receiver for which aBlock returns true, and return their number.

  For example:

  ```smalltalk
  (({1. 2. 3. 4} asBag) count: [:each | each even]) = 2.
  (({1. 2. 3. 4} asBag) count: #even) = 2.
  ```
* **`detect:`** - Search the receiver for an element for which aBlock returns true. If some does, answer it. If none does, fail.

  For example:

  ```smalltalk
  ({ 1. 2. 3 } asBag) detect: #even. "Return 2."
  ({ 1. 2. 3 } asBag) detect: [ :x | x = 1 ]. "Return 1."
  ```
* **`do:`** - aBlock Evaluate the block for all members in the collection.

  For example:

  ```smalltalk
  sc := 'x:'.
  bv := { 'a'. 'a'. 'v' }.
  bv do: [ :each | sc := sc + each]. "sc = 'x:aav'"
  ```
* **`inject:into:`** - First, pass to binaryBlock thisValue and the first element of the receiver; for each subsequent element, pass the result of the previous evaluation and an element. Answer the result of the last invocation.

  For example:

  ```smalltalk
  lc := { '1'. '2'. '3'. } asBag.
  ls := lc inject: 's' into: [ :vin :nxt | nxt + '/' + vin + '+'].
  ls = '3/2/1/s+++'.
  ```
* **`reject:`** - Answer a new instance of a Collection containing all the elements in the receiver which, when passed to aBlock, don’t answer true.

  For example:

  ```smalltalk
  lc := { 3. 2. 3. } asBag.
  lv := lc reject: [ :val | val = 3 ].
  lv = ({ 2 } asBag).
  ```
* **`select:`** - Answer a new instance of a Collection containing all the elements in the receiver which, when passed to aBlock, answer true.

  For example:

  ```smalltalk
  lc := { 3. 2. 1. } asBag.
  lv := lc select: [ :val | val = 3 ].
  lv assert: ({ 3 } asBag).
  ```

#### Category: removing

* **`remove:ifAbsent:`** - Remove oldObject from the collection and return it. If can’t be found, answer instead the result of evaluating exceptionBlock

#### Category: testing

* **`includesAllOf:`** - Answer whether we include all of the objects in aCollection.

  For example:

  ```smalltalk
  ({ 1. 2. 3 } asBag) includesAllOf: ({ 3. 1 } asBag) "Return true."
  ({ 1. 2. 3 } asBag) includesAllOf: { 4. 1 } "Return false."
  ```
* **`includesAnyOf:`** - Answer whether we include any of the objects in aCollection.

  For example:

  ```smalltalk
  ({ 1. 2. 3 } asBag) includesAnyOf: ({ 3. 4 } asList). "Return true."
  ({ 1. 2. 3 } asBag) includesAnyOf: { 4. 5 } "Return false."
  ```
* **`isEmpty`** - Answer whether we are (still) empty.

  For example:

  ```smalltalk
  lt := { 'a'. 'c' } asBag.
  lv := lt isEmpty. "lv = false"
  lt := { } asBag.
  lv := lt isEmpty. "lv = true"
  ```
* **`isSequenceable`** - Answer whether the receiver can be accessed by a numeric index with #at:/#at:put:.
* **`notEmpty`** - Answer whether we include at least one object.

  For example:

  ```smalltalk
  lt := { 'a'. 'c' } asBag.
  lv := lt notEmpty "lv = true
  lt := { } asBag.
  lv := lt notEmpty "lv = false"
  ```
* **`occurrencesOf:`** - Answer how many occurrences of anObject we include.

  For example:

  ```smalltalk
  al := { 'x'. 'y'. 'z'. 'y' } asBag.
  num := al occurrencesOf: 'y'. "num = 2"
  num := al occurrencesOf: 'z'. "num = 1"
  ```
