> 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-and-sequenceable.md).

# Package: Collections and Sequenceable

## Class: Array

### Introduction

**Environment: `container`**

Array is a fixed-sized collection of elements accessed by integer indices.

It has the following common used methods:

* `size` return the size of the array.
* `at:put:` set the element at the given index.
* `at:` get the element at the given index.
* `do:` iterate over the elements of the array.

```smalltalk
anArray := {1.'a string'}.
anArray size.  "2"
anArray at: 0.  "1"
anArray at: 1 put: 2.
```

### Class Method

#### Category: instance creation

* **`new`** - answer a new empty array.

  ```smalltalk
  array := Array new.
  array add: 'a'.
  ```
* **`new:`** - answer a new array with specified size.

  ```smalltalk
  array := Array new: 10.
  array at: 1 put: 'a'.
  ```

### Instance Method

#### Category: accessing

* **`add:`** - Add an element to the end of the array.

  For example:

  ```smalltalk
  array := {}.
  array add: 1.
  ```
* **`at:`** - answer the element at the given index.

  The index is zero based.
* **`at:put:`** - Set the element at the given index.

  The index is zero based.
* **`first`** - answer the first element of the receiver.
* **`size`** - answer the number of elements in the receiver.

  For example:

  ```smalltalk
  {1. 2. 3} size = 3.
  '123' size = 3.
  ```

#### Category: combining

* **`,`** - Concatenate two arrays.

  For example:

  ```smalltalk
  {1. 2. 3} , {4. 5. 6} = {1. 2. 3. 4. 5. 6}.
  'ab', 'cd' = 'abcd'.
  ```

#### Category: comparing

* **`=`** - answer whether the receiver sorts equally.

#### Category: converting

* **`asBag`** - Answer a Bag containing the elements of the receiver.
* **`asList`** - answer a list containing the elements of the receiver.
* **`asString`** - Convert an array into a string by join them with ',' by default

#### Category: enumerating

* **`allSatisfy:`** - Evaluate aBlock with the elements of the receiver. If aBlock returns false for any element return false. Otherwise return true.

  For example:

  ```smalltalk
  (#( 1 2 ) allSatisfy: [ :each | each even ]) = false.
  (#( 2 4 ) allSatisfy: [ :each | each even ]) = true.
  (#( 2 4 ) allSatisfy: [ :each | each < 5 ]) = true.
  (#() 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} anySatisfy: #even. "Return true."
  { 1. 2. 3 } anySatisfy:  [ :x | x = 4 ]. "Return false."
  ```
* **`collect:`** - Evaluate the given block for each element of the receiver and return a new array with the results.

  For example:

  ```smalltalk
  ({1. 2. 3} collect: [:each | each + 1]) = {2. 3. 4}.
  ```
* **`count:`** - Evaluate aBlock with each of the receiver's elements as the argument. Answer the number of elements that answered true.

  For example:

  ```smalltalk
  ({1. 2. 3. 4} count: [:each | each even]) = 2.
  ({1. 2. 3. 4} count: #even) = 2.
  ```
* **`detect:`** - Evaluate aBlock with each of the receiver's elements as the argument until aBlock evaluates to true. Answer the first element for which aBlock evaluates to true.

  For example:

  ```smalltalk
  {1. 2. 3} detect: #even. "Return 2."
  { 1. 2. 3 } detect: [ :x | x = 1 ]. "Return 1."
  ```
* **`detect:ifFound:`** - Evaluate aBlock with each of the receiver's elements as the argument until aBlock evaluates to true. If some element evaluates aBlock to true, then cull this element into foundBlock and answer the result of this evaluation.

  For example:

  ```smalltalk
  { 1. 2. 3 } detect: [ :x | x > 1 ] ifFound: [ :x | x + 10]. "Return 12."
  ```
* **`detect:ifFound:ifNone:`** - Evaluate aBlock with each of the receiver's elements as the argument until aBlock evaluates to true. If some element evaluates aBlock to true, then cull this element into foundBlock and answer the result of this evaluation. If none evaluate to true, then evaluate exceptionBlock

  For example:

  ```smalltalk
  { 1. 2. 3 } detect: [ :x | x > 1 ] ifFound: [ :x | x + 10] ifNone:  4 . "Return 12."
  ```
* **`detect:ifNone:`** - Evaluate aBlock with each of the receiver's elements as the argument until aBlock evaluates to true. Answer the first element for which aBlock evaluates to true, or answer the value in none block.

  For example:

  ```smalltalk
  { 1. 2. 3 } detect: [ :x | x > 10 ] ifNone:  4 . "Return 4."
  { 1. 2. 3 } detect: [ :x | x > 10 ] ifNone: [ 4 ]. "Return 4."
  ```
* **`do:`** - Evaluate the given block for each element of the receiver.
* **`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'. }.
  ls := lc inject: 'r' into: [ :vin :nxt | nxt + '/' + vin + '+'].
  ls = '3/2/1/r+++'.
  ```
* **`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. }.
  lv := lc reject: [ :val | val = 3 ].
  lv assert: { 2 }.
  ```
* **`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. }.
  lv := lc select: [ :val | val = 3 ].
  lv assert: { 3 }.
  ```
* **`sorted`** - Return a copy of the receiver sorted according to the default sort block, which uses #<= to compare items.

  For example:

  ```smalltalk
  { 2. 4. 7. 3 } sorted = { 2. 3. 4. 7 }
  ```
* **`sorted:`** - Return a copy of the receiver sorted according to the given sort block, which accepts pair of items and returns true if the first item is less than the second one.

  For example:

  ```smalltalk
  sl := { 2. 4. 7. 3 } sorted: [ :x :y | x > y ].
  sl = { 7. 4. 3. 2 }.
  sl := { 'd'. 'a'. 'h'. '3' } sorted: [ :x :y | x < y ].
  sl = { '3'. 'a'. 'd'. 'h'}.
  ```

#### Category: instance creation

* **`new`** - Create a new array object by copy current array.

#### Category: removing

* **`remove:`** - Removed the given element in the array and return the removed element.

  For example:

  ```smalltalk
  x := { 'a'. 'b'. 'c'. 'b'. 'd'}.
  y := x remove: 'b'.
  x = { 'a'. 'c'. 'b'. 'd'}.
  y = b.
  ```

  Only first matched element will be removed, please use `removeAll:` if you want to remove all matched elements.
* **`removeAll:`** - Removed the given element in the array for all occurrences.

  For example:

  ```smalltalk
  x := { 'a'. 'b'. 'c'. 'b'. 'd'}.
  x removeAll: 'b'.
  x = { 'a'. 'c'. 'd'}.
  ```

  Only first matched element will be removed, please use `removeAll:` if you want to remove all matched elements.
* **`removeAt:`** - Remove the item at the specified position and return the removed item's value.

  The index is zero based.

  For example:

  ```smalltalk
  x := { 1. 2. 1. 3 }.
  y := x removeAt: 1.
  x = {1. 1. 3}.
  y = 2.
  ```
* **`removeDuplicates`** - Answer a modified copy of sequence from which any element that matches another element occurring in sequence has been removed.

  For example:

  ```smalltalk
  "Returns {1. 2. 3}"
  { 1. 2. 1. 3 } removeDuplicates.
  "Returns 'cabde'"
  'abcdabde' removeDuplicates.
  ```

#### Category: testing

* **`includes:`** - Answer true if the array includes the element; otherwise, answer false.

  For example:

  ```smalltalk
  {1. 2. 3} includes: 3. "Return true."
  { 1. 2. 3 } includes:  4. "Return false."
  ```
* **`includesAllOf:`** - Answer whether we include all of the objects in aCollection.

  For example:

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

  For example:

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

  For example:

  ```smalltalk
  lt := { 'a'. 'c' } asList.
  lv := lt isEmpty. "lv = false"
  lv := lt removeLast. "lv = 'c', lt = { 'a' } asList"
  lv := lt isEmpty. "lv = false"
  lv := lt removeLast. "lv = 'a', lt = { } asList"
  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' }.
  lv := lt notEmpty. "lv = true"
  lv := lt remove: 'c'. "lv = 'c', lt = { 'a' }"
  lv := lt notEmpty. "lv = true"
  lt removeAll: 'a'. "lt = { }"
  lv := lt notEmpty. "lv = false"
  ```
* **`occurrencesOf:`** - Answer how many occurrences of anObject we include.

  For example:

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

## Class: LinkedList

### Introduction

**Environment: `container`**

I am a sequential collection where adjecent objects are linked. I can store any kind of objects that I will wrap into a Link.

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
aList := {1.'a string'} asList.
aList size.  "2"
aList first.  "1"
aList rest.  "{'a string'}"
aList at: 1.  "'a string'"
aList at: 1 put: 'another string'.

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

### Instance Method

#### Category: accessing

* **`add:`** - Add aLink at the end of the list; return aLink.

  For example:

  ```smalltalk
  lt := { 'a'. 'c' } asList.
  lt add: 'd'. "lt = { 'a'. 'c'. 'd' }"
  ```
* **`addFirst:`** - Add aLink at the head of the list; return aLink.

  For example:

  ```smalltalk
  lt := { 'a'. 'c' } asList.
  lt addFirst: 'd'. "lt = { 'd'. 'a'. 'c' }"
  ```
* **`removeFirst`** - Remove the first element from the list and return it, or error if the list is empty.

  For example:

  ```smalltalk
  lt := { 'a'. 'c' } asList.
  lv := lt removeFirst. "lv = 'a', lt = { 'c' } asList"
  lv := lt removeFirst. "lv = 'c', lt = { } asList"
  lv := lt removeFirst.
  "Error: List is empty!"
  ```
* **`removeLast`** - Remove the final element from the list and return it, or error if the list is empty.

  For example:

  ```smalltalk
  lt := { 'a'. 'c' } asList.
  lv := lt removeLast. "lv = 'c', lt = { 'a' } asList"
  lv := lt removeLast. "lv = 'a', lt = { } asList"
  lv := lt removeLast.
  "Error: List is empty!"
  ```

#### Category: converting

* **`asBag`** - Answer a Bag containing the elements of the receiver.

#### Category: enumerating

* **`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. } asList.
  lv := lc reject: [ :val | val = 3 ].
  lv = ({ 2 } asList).
  ```
* **`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
  dict := { ('x' -> 'aaa'). ('y' -> 'bbb'). ('z' -> 'ccc') } asDictionary.
  dict2 := dict select: [ :val | val = 'aaa' ].
  dict2 associations. "{ ('x' -> 'aaa') }"
  ```
* **`sorted`** - Return a copy of the receiver sorted according to the default sort block, which uses #<= to compare items.

  For example:

  ```smalltalk
  { 2. 4. 7. 3 } asList sorted = { 2. 3. 4. 7 } asList
  ```
* **`sorted:`** - Return a copy of the receiver sorted according to the given sort block, which accepts pair of items and returns true if the first item is less than the second one.

  For example:

  ```smalltalk
  il := { 2. 4. 7. 3 } asList.
  sl := il sorted: [ :x :y | x > y ].
  sl = { 7. 4. 3. 2 } asList.
  il := { 'd'. 'a'. 'h'. '3' } asList.
  sl := il sorted: [ :x :y | x < y ].
  sl = { '3'. 'a'. 'd'. 'h'} asList.
  ```

#### Category: reflective operations

* **`doesNotUnderstand:`** - Forward the functionality to its appropriate implementation.

#### Category: removing

* **`remove:ifAbsent:`** - Remove aLink from the list and return that list, or invoke aBlock if it’s not found in the list.

  For example:

  ```smalltalk
  lt := { 'a'. 'c' } asList.
  lv := lt remove: 'd' ifAbsent: [ 'xyz' ]. "lv = 'xyz'"
  lv := lt remove: 'c' ifAbsent: [ 'zyx' ]. "lv = { 'a' } asList"
  ```

#### Category: testing

* **`isSequenceable`** - Answer whether the receiver can be accessed by a numeric index with #at:/#at:put:.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mind-expression-docs.gitbook.io/home/how-to-guides/intelligent-process/package-collections-and-sequenceable.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
