this documentation is work-in-progress, edit here (md).

Lists

sections in this chapter:
chaining »
[ List ] type »
[ List ] append: [ String ] »
[ List ] minimum »
[ List ] maximum »
[ List ] each: [ Block ] »
[ List ] new ; [ Object ] »
[ List ] prepend: [ String ] »
[ List ] combine: [ String ] »
[ List ] position: [ Number ] »
[ List ] first »
[ List ] last »
[ List ] second last »
[ List ] put: [ Object ] at: [ Number ] »
[ List ] pop »
[ List ] shift »
[ List ] count »
[ List ] from: [ Number ] length: [ Number ] »
[ List ] replace: [ Number ] length: [ Number ] with: [ List ] »
[ List ] + [ List ] »
[ List ] by: [ List ] »
[ List ] copy »
[ List ] sort: [ Block ] »
[ List ] string »
[ List ] fill: [ Number ] with: [ Object ] »
[ List ] find: [ Object ] »

Xoscript knows two types of collections: lists and dicts. Lists are enumerations of objects in a fixed order. Dicts have no order, instead, resemble a legenda involving a key (or term) and its corresponding value. Lists are comparable to arrays (PHP, Java, C) and lists (Python). Dicts are comparable to associative arrays (PHP) or dictionaries (Python) in other programming languages. In order to create a new List write:

>> fibonacci := List new.

This empty sequence can be filled using append: :

fibonacci append: 0.
fibonacci append: 1.
fibonacci append: 1.
fibonacci append: 2.
fibonacci append: 3.
fibonacci append: 5.
fibonacci append: 8.
fibonacci append: 13.

If we write the sequence to screen:

Out write: fibonacci, stop.

We will see:

List ← 0 ; 1 ; 1 ; 2 ; 3 ; 5 ; 8 ; 13

Instead of append you can also use ; to add elements to a list:

>> fibonacci := List new ; 0 ; 1 ; 1 ; 2 ; 3 ; 5 ; 8 ; 13.

The semicolons (;) are all binary messages which put a value into the sequence.

Chaining

Suppose you have a list like:

>> x := List new ; 1 ; 2 ; 3.

If you would like to delete the first and the last element, then your message would be:

x shift pop.

Unfortunately, the previous notation will not function correctly. The message shift returns the first element of the sequence, which, in turn, will become the receiver of the message pop. So, the message pop is not sent to x, but instead to 2. A possible solution to this problem would be to create two separate sentences, such as:

x shift.
x pop.

However, this is rather unpractical, in particular when you need to delete more than two elements. Therefore, Xoscript offers an alternative message structure for these kinds of situations. In alternative message structures, the answers of objects are ignored and you will get the receiving object back as answer, time and again. An alternative message structure is initiated by sending the message do to an object and the alternative message structure can be ended by sending the message done. In the example illustrated, this could be applied as follows:

>> x := List new ; 1 ; 2 ; 3.
x do shift pop done.

[ List ] type

Example:


 >> x := List new.
 >> y := x type.
 Out write: y, stop.
 
	

Result:

List

	

[ List ] append: [ String ]

Example:


 >> x := List new.
 x append: 3.
 x ; 3.
 x ~ 3.
 Out write: x, stop.
 
	

Result:

List ← 3 ; 3 ; 3

	

[ List ] minimum

Example:


 >> x := List ← 8 ; 4 ; 2 ; 16.
 >> y := x minimum.
 Out write: y, stop.
 
	

Result:

2

	

[ List ] maximum

Example:


 >> x := List ← 8 ; 4 ; 2 ; 16.
 >> y := x maximum.
 Out write: y, stop.
 
	

Result:

16

	

[ List ] each: [ Block ]

Example:


 >> x := List ← 1 ; 2 ; 3.
 x each: { :x
   Out write: x, stop.
 }.
 
	

Result:

0
1
2

	

[ List ] new ; [ Object ]

Example:


 >> x := List new ;  1 ; ['2'] ; False ; None.
 Out write: x, stop.
 
	

Result:

List ← 1 ; ['2'] ; False ; None

	

[ List ] prepend: [ String ]

Example:


 >> x := List ← 1 ; 2 ; 3.
 x append: 0.
 x prepend: 9.
 Out write: x, stop.
 
	

Result:

List ← 9 ; 1 ; 2 ; 3 ; 0

	

[ List ] combine: [ String ]

Example:


 >> x := List ← 1 ; 2 ; 3.
 Out write: (x combine: [',']), stop.
 
	

Result:

1,2,3

	

[ List ] position: [ Number ]

Example:


 >> x  := List new ; ['A'] ; ['B'] ; ['C'].
 
 # get element by its position
 >> y  := x position: 1.
 
 # shorter notation
 >> z  := x ? 2.
 
 Out write: y, stop.
 
 Out write: z, stop.
 
	

Result:

B
C

	

[ List ] first

Example:


 >> x := List ← 1 ; 2 ; 3 ; 4.
 >> y := x first.
 Out write: x, stop.
 Out write: y, stop.
 
	

Result:

List ← 1 ; 2 ; 3 ; 4
1

	

[ List ] last

Example:


 >> x := List ← 1 ; 2 ; 3 ; 4.
 >> y := x last.
 Out write: x, stop.
 Out write: y, stop.
 
	

Result:

List ← 1 ; 2 ; 3 ; 4
4

	

[ List ] second last

Example:


 >> x := List ← 1 ; 2 ; 3 ; 4.
 >> y := x penultimate.
 Out write: x, stop.
 Out write: y, stop.
 
	

Result:

List ← 1 ; 2 ; 3 ; 4
3

	

[ List ] put: [ Object ] at: [ Number ]

Example:


 >> x := List new.
 >> x put: ['a'] at: 5.
 Out write: x, stop.
 
	

Result:

List ← None ; None ; None ; None ; None ; ['a']

	

[ List ] pop

Example:


 >> x := List ← 1 ; 2 ; 3.
 Out write: x, stop.
 x pop.
 Out write: x, stop.
 
	

Result:

List ← 1 ; 2 ; 3
List ← 1 ; 2

	

[ List ] shift

Example:


 >> x := List ← 1 ; 2 ; 3.
 >> y := x shift.
 Out write: y, stop.
 Out write: x, stop.
 
	

Result:

1
List ← 2 ; 3

	

[ List ] count

Example:


 >> x := List ← 1 ; 2 ; 3.
 Out write: x count, stop.
 
	

Result:

3

	

[ List ] from: [ Number ] length: [ Number ]

Example:


 >> x := List ← 1 ; 2 ; 3 ; 4 ; 5.
 >> y := x from: 2 length: 2.
 Out write: y, stop.
 
	

Result:

List ← 3 ; 4

	

[ List ] replace: [ Number ] length: [ Number ] with: [ List ]

Example:


 >> x := List ← 1 ; 2 ; 3 ; 4 ; 5.
 >> z := List ← 9.
 >> y := x replace: 2 length: 1 with: z.
 Out write: y, stop.
 
	

Result:

List ← 1 ; 2 ; 9 ; 4 ; 5

	

[ List ] + [ List ]

Example:


 >> x := List ← 1 ; 2.
 >> y := List ← 3 ; 4.
 >> z := x + y.
 Out write: z, stop.
 
	

Result:

List ← 1 ; 2 ; 3 ; 4

	

[ List ] by: [ List ]

Example:


 >> x := List ← ['A'] ; ['B'] ; ['C'].
 >> y := List ← 1 ; 2 ; 3.
 >> z := x by: y.
 Out write: z, stop.
 
	

Result:

(Dict new) put:['C'] at:['3'], put:['B'] at:['2'], put:['A'] at:['1']

	

[ List ] copy

Example:


 >> a := List ← 1 ; 2 ; 3.
 >> b := a copy.
 b put: 999 at: 1.
 Out write: a, stop.
 Out write: b, stop.
 
	

Result:

List ← 1 ; 2 ; 3
List ← 1 ; 999 ; 3

	

[ List ] sort: [ Block ]

Example:


 >> x := List ← 2 ; 1 ; 3.
 x sort: { :a :b <- a < b.}.
 Out write: x, stop.
 
	

Result:

List ← 3 ; 2 ; 1

	

[ List ] string

Example:


 >> x := List ← ['a'] ; ['b'] ; ['c'].
 >> y := x string.
 Out write: y, stop.
 
	

Result:

List ← ['a'] ; ['b'] ; ['c']

	

[ List ] fill: [ Number ] with: [ Object ]

Example:


 >> x := List new
 fill: 10 with: ['X'].
 Out write: x, stop.
 
	

Result:

List ← ['X'] ; ['X'] ; ['X'] ; ['X'] ; ['X'] ; ['X'] ; ['X'] ; ['X'] ; ['X'] ; ['X']

	

[ List ] find: [ Object ]

Example:


 >> x := List ← 1 ; 2 ; 3.
 >> y := x find: 2.
 Out write: y, stop.
 
	

Result:

1