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

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

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

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

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

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

    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:

    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:

    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:

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

    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:

    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.

Category: testing

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

    For example:

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

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

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

    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:

    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:

    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:

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:

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

    For example:

    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:

    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:

    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:

    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:

    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:

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

    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:

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

Last updated