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:

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.

  • new: - Answer a new instance of the receiver, with space for size distinct items.

Instance Method

Category: accessing

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

    For example:

  • includes: - Answer whether we include anObject.

    For example:

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

Category: converting

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

    For example:

Category: enumerating

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

    For example:

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

  • count: - Count the elements of the receiver for which aBlock returns true, and return their number.

    For example:

  • detect: - Search the receiver for an element for which aBlock returns true. If some does, answer it. If none does, fail.

    For example:

  • do: - aBlock Evaluate the block for all members in the collection.

    For example:

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

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

  • select: - Answer a new instance of a Collection containing all the elements in the receiver which, when passed to aBlock, answer true.

    For example:

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:

  • includesAnyOf: - Answer whether we include any of the objects in aCollection.

    For example:

  • isEmpty - Answer whether we are (still) empty.

    For example:

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

  • occurrencesOf: - Answer how many occurrences of anObject we include.

    For example:

Last updated

Was this helpful?