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.
Class Method
Category: instance creation
new
- answer a new empty array.new:
- answer a new array with specified size.
Instance Method
Category: accessing
add:
- Add an element to the end of the array.For example:
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:
Category: combining
,
- Concatenate two arrays.For example:
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:
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:
collect:
- Evaluate the given block for each element of the receiver and return a new array with the results.For example:
count:
- Evaluate aBlock with each of the receiver's elements as the argument. Answer the number of elements that answered true.For example:
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:
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:
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 exceptionBlockFor example:
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:
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:
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:
sorted
- Return a copy of the receiver sorted according to the default sort block, which uses #<= to compare items.For example:
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:
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:
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:
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:
removeDuplicates
- Answer a modified copy of sequence from which any element that matches another element occurring in sequence has been removed.For example:
Category: testing
includes:
- Answer true if the array includes the element; otherwise, answer false.For example:
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:
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:
Instance Method
Category: accessing
add:
- Add aLink at the end of the list; return aLink.For example:
addFirst:
- Add aLink at the head of the list; return aLink.For example:
removeFirst
- Remove the first element from the list and return it, or error if the list is empty.For example:
removeLast
- Remove the final element from the list and return it, or error if the list is empty.For example:
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:
select:
- Answer a new instance of a Collection containing all the elements in the receiver which, when passed to aBlock, answer true.For example:
sorted
- Return a copy of the receiver sorted according to the default sort block, which uses #<= to compare items.For example:
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:
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:
Category: testing
isSequenceable
- Answer whether the receiver can be accessed by a numeric index with #at:/#at:put:.
Last updated