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.

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.

    array := Array new.
    array add: 'a'.
  • new: - answer a new array with specified size.

    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:

    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:

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

Category: combining

  • , - Concatenate two arrays.

    For example:

    {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

  • 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:

    (#( 1 2 ) allSatisfy: [ :each | each even ]) = false.
    (#( 2 4 ) allSatisfy: [ :each | each even ]) = true.
    (#( 2 4 ) allSatisfy: [ :each | each < 5 ]) = true.
    (#() allSatisfy: [false]) = true.
  • collect: - Evaluate the given block for each element of the receiver and return a new array with the results.

    For example:

    ({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:

    ({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:

    {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:

    { 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:

    { 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:

    { 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.

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:

    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:

    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:

    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:

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

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:

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

  • at: - answer the element at the given index.

  • at:put: - Set the element at the given index.

  • first - answer the first element of the receiver.

  • rest - answer the rest of the receiver.

  • size - answer the number of elements in the receiver.

Category: converting

  • asString - Convert a list into a string by join them with ',' by default

Category: enumerating

  • collect: - Evaluate the given block for each element of the receiver and return a new list with the results.

    For example:

    ({1. 2. 3} asList collect: [:each | each + 1]) = {2. 3. 4} asList.
  • 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:

    {1. 2. 3} asList detect: #even. "Return 2."
    { 1. 2. 3 } asList 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:

    { 1. 2. 3 } asList 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:

    { 1. 2. 3 } asList 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:

    { 1. 2. 3 } asList detect: [ :x | x > 10 ] ifNone:  4 . "Return 4."
    { 1. 2. 3 } asList detect: [ :x | x > 10 ] ifNone: [ 4 ]. "Return 4."
  • do: - Evaluate the given block for each element of the receiver.

Category: removing

  • removeDuplicates - Answer a modified copy of sequence from which any element that matches another element occurring in sequence has been removed.

    For example:

    "Returns {2. 1. 3} asList"
    { 1. 2. 1. 3 } asList removeDuplicates.

Last updated