The things you may call a List
In many other programming languages such as Python, Ruby, Lisp-like etc. you will find a structure called list. This is a beautiful structure where you can put stuff in, also of different kinds, you can remove stuff, search the list. It grows by itself its size to fit your requests. It is really handy.
In Cuis, and in Smalltalk in general, we don't have a data structure named list. The thing that is nearest to it is the class OrderedCollection.
There is one thing that may confuse you so let's get it straight from the beginning. Python, Ruby, Lisp-likes have a way to type in lists directly. In Smalltalk when we type in an object that looks like a list the system store it as an Array. But no worries, Array and OrderedCollection are both descendants of SequenceableCollection so many operations work on both data structures and you can easily convert one into another.
Exercise. Open the Browser and check what messages an object of class SequenceableColletion can understand.
Exercise. Open a Browser and confirm that SequenceableCollection is an ancestor of both Array and OrderedCollection.
In the following, to keep the language compact I will talk about lists, but you have been warned, there is no list in Smalltalk. Also, I will use the variable v
to store intermediate results just because it looks better than l
, which I often confuse with 1
(one).
CAVEAT. We are not going to cover here examples of all possible methods you can use on lists. Use the Terse Guide to see more possibilities and of course the Browser at the relevant Class.
Make a list 'by hand', equivalent of v = [1,2,3]
in Python/Ruby and others.
v _ #(1 2 3). "=> #(1 2 3) " v class. " Array "
or
v _ {1+2. 123. 7/2. 'hello'.}. " #(3 123 7/2 'hello') " v class. " Array "
Observe that in this second case the list has been made with the results of the evaluation of a sequence of commands.
Make a list, programmatically
v _ OrderedCollection new. "=> an OrderedCollection() " v add: 1. v add: 'foo bar'. "let's go higher, put another list into the list" v2 _ OrderedCollection new. v2 add: 'tizio' . oc2 add: 'caio'. oc2 add: 'sempronio'. v add: v2. v "=> an OrderedCollection(1 'foo bar' an OrderedCollection('tizio' 'caio' 'sempronio')) "
The Smalltalk way to see a list
Comparing to Python and Ruby representation of the list you would say that we are really bad. Well, look now, leverage Smalltalk integrated graphical user interface.
v explore .
Check if a list contains an element
#(1 2 3) includes: 2. "=> true "
Return the first element of the list satisfying a property
#(1 2 3 4 5 6) detect: [:a | a > 3] ifNone: nil. "=> 4 " #(1 2 3 4 5 6) detect: [:a | a > 10] ifNone: nil. "=> nil "
Return all elements of a list satisfying a property
#(1 2 3 4 5 6) select: [:a | a > 3] . "=> #(4 5 6) " #(1 2 3 4 5 6) select: [:a | a > 10] . "=> #() "
Iterate over elements of a list for side effects
tmp _ 0 . #(10 20 30) do: [ :x | tmp _ tmp + x ] . tmp. "=> 60 "
map. Apply an operation to all elements of a list and build a new list with the results
#(1 2 3) collect: [:el | 2 * el ] . "=> #(2 4 6) "
remove. all elements from a list if they satisfy a property
se _ Set new. se add: 1. se add: 2. se removeAllSuchThat: [:x | x == 2] .
TODO Return the list of all indexes of elements satisfying a property
NM started on 15-Aug-2021. Examples done in Cuis5.0-4738.image