For loop for array in Pharo Smalltalk

I am trying to create an array with random numbers (total 0 or 1), but when I run it, it just prints this: column of the completed list →

This is my code:

GenList
 | lista |
  lista := Array new: 31.
  1 to: 30 do: [ :i | lista at: i put: 2 atRandom - 1]
  ^lista

What can I do?

+4
source share
1 answer

Some interesting things:


1. The method selector does not start with a lowercase letter


The tradition for selectors begins with a lowercase letter. In this sense, it genListawill be more correct than genLista.


2. The method selector includes the shortened word 'gen'


For example, genListayou can rename to genereListao listaAlAzar(if you decide to use Spanish)


3. Array lista 31 , 30


Array new: 31 - 31 . 30 , (.. nil). : lista := Array new: 30.


4. ,


  1 to: 30 do: [ :i | lista at: i put: 2 atRandom - 1]
  ^lista

, , . , (.. ), ^ (.. ).

, . . ():

1 to: 10 do: [:i | self somethingWith: i]     "<- missing dot here"
self somethingElse

self not understood by block.


5. 0s 1s


2 atRandom - 1 . , .

#(0 1) atRandom

6.


atRandom , ​​ "". , , -, . . ( , , , , (, ) - .)

+12

Source: https://habr.com/ru/post/1680734/


All Articles