Loops/Nested: Difference between revisions

→‎{{header|Ruby}}: ++ smalltalk
(Add new section for Lisaac)
(→‎{{header|Ruby}}: ++ smalltalk)
Line 538:
6 19 8 13 ,
1 20 done</pre>
 
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
 
Smalltalk has no ways of escaping from loops (single or nested), even if it is possible to extend its iteration capabilities in several ways. The following code implements a BiArray class with a method that allows iteration over the elements (by columns and then by rows) and execution of a block if a condition is true.
 
<lang smalltalk>"this simple implementation of a bidimensional array
lacks controls over the indexes, but has a way of iterating
over array's elements, from left to right and top to bottom"
Object subclass: BiArray [
|cols rows elements|
BiArray class >> columns: columns rows: howManyRows [
^ super basicNew init: columns per: howManyRows
]
init: columns per: howManyRows [
cols := columns.
rows := howManyRows.
elements := Array new: ( columns * howManyRows )
]
calcIndex: biIndex [ "column, row (x,y) to linear"
^ ( (biIndex at: 1) + (((biIndex at: 2) - 1) * cols) )
]
at: biIndex [ "biIndex is an indexable containing column row"
^ elements at: (self calcIndex: biIndex).
]
directAt: i [ ^ elements at: i ]
at: biIndex put: anObject [
elements at: (self calcIndex: biIndex) put: anObject
]
whileTrue: aBlock do: anotherBlock [
|i lim|
i := 1. lim := rows * cols.
[ ( i <= lim )
& (aBlock value: (self directAt: i) )
] whileTrue: [
anotherBlock value: (self directAt: i).
i := i + 1.
]
]
].
 
|biarr|
biarr := BiArray columns: 10 rows: 10.
 
"fill the array; this illustrates nested loop but not how to
escape from them"
1 to: 10 do: [ :c |
1 to: 10 do: [ :r |
biarr at: {c . r} put: (Random between: 1 and: 20)
]
].
 
"loop searching for 20; each block gets the element passed as argument"
biarr whileTrue: [ :v | v ~= 20 ]
do: [ :v | v displayNl ]</lang>
 
=={{header|Tcl}}==