Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
8
Task/Loops-Nested/Smalltalk/loops-nested-1.st
Normal file
8
Task/Loops-Nested/Smalltalk/loops-nested-1.st
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
|i|
|
||||
|
||||
i := 1.
|
||||
[:exit |
|
||||
Transcript showCR:i.
|
||||
i == 5 ifTrue:[ exit value:'stopped' ].
|
||||
i := i + 1.
|
||||
] loopWithExit
|
||||
15
Task/Loops-Nested/Smalltalk/loops-nested-2.st
Normal file
15
Task/Loops-Nested/Smalltalk/loops-nested-2.st
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
|i|
|
||||
|
||||
i := 1.
|
||||
[:exit1 |
|
||||
|j|
|
||||
|
||||
j := 0.
|
||||
[:exit2 |
|
||||
Transcript showCR:('i is %1 / j is %2' bindWith:i with:j).
|
||||
j == 5 ifTrue:[ exit2 value: nil ].
|
||||
i == 5 ifTrue:[ exit1 value: nil ].
|
||||
j := j + 1.
|
||||
] loopWithExit.
|
||||
i := i + 1
|
||||
] loopWithExit
|
||||
10
Task/Loops-Nested/Smalltalk/loops-nested-3.st
Normal file
10
Task/Loops-Nested/Smalltalk/loops-nested-3.st
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
!Block methodsFor:'looping'!
|
||||
loopWithExit
|
||||
"the receiver must be a block of one argument. It is evaluated in a loop forever,
|
||||
and is passed a block, which, if sent a value:-message, will exit the receiver block,
|
||||
returning the parameter of the value:-message. Used for loops with exit in the middle."
|
||||
|
||||
|exitBlock|
|
||||
|
||||
exitBlock := [:exitValue | ^ exitValue].
|
||||
[true] whileTrue:[ self value:exitBlock ]
|
||||
24
Task/Loops-Nested/Smalltalk/loops-nested-4.st
Normal file
24
Task/Loops-Nested/Smalltalk/loops-nested-4.st
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
|v result|
|
||||
|
||||
v := 1 to:20 collect:[:i |
|
||||
1 to:20 collect:[:j | Random nextIntegerBetween:1 and:20 ]
|
||||
].
|
||||
|
||||
result :=
|
||||
[:exit |
|
||||
1 to:20 do:[:row |
|
||||
1 to:20 do:[:col |
|
||||
|element|
|
||||
|
||||
(element := (v at:row) at:col) printCR.
|
||||
element == 20 ifTrue:[ exit value:(row @ col) ].
|
||||
]
|
||||
].
|
||||
nil
|
||||
] valueWithExit.
|
||||
|
||||
result isNil ifTrue:[
|
||||
'ouch - no 20 found' printCR.
|
||||
] ifFalse:[
|
||||
'20 found at ' print. result printCR
|
||||
]
|
||||
49
Task/Loops-Nested/Smalltalk/loops-nested-5.st
Normal file
49
Task/Loops-Nested/Smalltalk/loops-nested-5.st
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
"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 ]
|
||||
Loading…
Add table
Add a link
Reference in a new issue