forked from bonzini/smalltalk
/
blocks.st
155 lines (120 loc) · 4.23 KB
/
blocks.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"======================================================================
|
| Test out block operations
|
|
======================================================================"
"======================================================================
|
| Copyright (C) 1988, 1989, 1999 Free Software Foundation.
| Written by Steve Byrne and Paolo Bonzini
|
| This file is part of GNU Smalltalk.
|
| GNU Smalltalk is free software; you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by the Free
| Software Foundation; either version 2, or (at your option) any later version.
|
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
| details.
|
| You should have received a copy of the GNU General Public License along with
| GNU Smalltalk; see the file COPYING. If not, write to the Free Software
| Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
======================================================================"
Eval [ [45] ] "should return a block"
Eval [ [^#quem] value ] "should return #quem"
Eval [ ['foo'] value ] "should return 'foo'"
Eval [ [:i | i] value: 'juma' ] "should return 'juma'"
Eval [ [:i :j| j] value: 12 value: 17 ] "should return 17"
Object extend [
blockTest1 [
[#foo]
]
blockTest2 [
[^#foo]
]
blockTest3 [
^[#bar]
]
blockTest4 [
^[^#bar]
]
blockTest5: arg [
^[arg]
]
blockTest6: arg [
^[:i | arg at: i]
]
blockTest7: arg [
| temp |
temp := (arg at: 4) + 8.
^[temp]
]
blockTest8: which [
| first second |
first := nil blockTest7: #('one' #two 3.0 4 $5).
second := nil blockTest7: #("You are[,] number" 'six' #seven 8.0 9 $A).
which ifTrue: [ ^first value ]
ifFalse: [ ^second value]
]
"Implements a 'closure'!!! Smalltalk is AMAZING!!!"
blockTest9: initialValue [
| counter |
counter := initialValue.
^[:incr | counter := counter + incr.
counter]
]
"Implements a REAL 'closure'!!! GNU Smalltalk is AMAZING!!!"
blockTest10 [
| counter |
counter := 1.
"If blocks were not real closures, variable would be 1 the
second time the block was called and hence it would not
be modified.
Instead if blocks are closures, variable is still nil the
second time the block is evaluated, and is initialized to
two."
^[ | variable |
variable isNil ifTrue: [ variable := counter ].
counter := counter + 1.
variable
]
]
blockTest11: initialValue [
^[^initialValue]
]
]
Eval [ nil blockTest1 ] "should return nil"
Eval [ nil blockTest2 ] "should return nil"
Eval [ nil blockTest3 ] "should return a BlockClosure"
Eval [ nil blockTest3 value ] "should return #bar"
Eval [ nil blockTest4 value ] "should issue an error, we're returning to
a non-existent context"
Eval [ (nil blockTest5: 'Smalltalk!') value ]
"should return 'Smalltalk!'"
Eval [ (nil blockTest6: #('one' #two 3.0 4 $5)) value: 2 ]
"should return #two"
Eval [ (nil blockTest7: #('you' #are #number 6)) value ]
"should return 14"
Eval [ nil blockTest8: true ] "should return 12"
Eval [ nil blockTest8: false ] "should return 17"
"Create a block with the initial value of 2"
Eval [ Smalltalk at: #testBlock put: (nil blockTest9: 2) ]
Eval [ testBlock value: 3 ] "should return 5"
Eval [ testBlock value: 6 ] "should return 11"
Eval [ testBlock value: 2 ] "should return 13"
Eval [ Smalltalk at: #testBlock put: (nil blockTest10) ]
Eval [ testBlock value ] "should return 1"
Eval [ testBlock value ] "should return 2 (1 if blocks aren't closures)"
"And this is even more amazing!!!"
Eval [
| array |
array := (1 to: 10) collect: [ :each | [each] ].
^array inject: 0 into: [ :sum :each | sum + each value ] "should get 55"
]
Eval [ (nil blockTest11: 3) value ] "should be invalid; we're returning to non-
existent parent"