CS-Notes/docs/_style/prism-master/examples/prism-smalltalk.html

92 lines
1.9 KiB
HTML
Raw Normal View History

2018-12-19 14:09:39 +08:00
<h2>Numbers</h2>
<pre><code>3
30.45
-3
0.005
-14.0
13772
8r377
8r153
8r34.1
8r-37
16r106
16rFF
16rAC.DC
16r-1.C
1.586e5
1.586e-3
8r3e2
2r11e6</code></pre>
<h2>Strings and characters</h2>
<pre><code>$a
$M
$-
$$
$1
'hi'
'food'
'the Smalltalk-80 system'
'can''t'</code></pre>
<h2>Symbols</h2>
<pre><code>#bill
#M63
#+
#*</code></pre>
<h2>Arrays</h2>
<pre><code>#(1 2 3)
#('food' 'utilities' 'rent' 'household' 'transportation' 'taxes' 'recreation')
#(('one' 1) ('not' 'negative') 0 -1)
#(9 'nine' $9 (0 'zero' $0 ( ) 'e' $f 'g' $h 'i'))</code></pre>
<h2>Blocks</h2>
<pre><code>sum := 0.
#(2 3 5 7 11) do: [ :primel | sum := sum + (prime * prime)]
sizeAdder := [ :array | total := total + array size].
[ :x :y | (x * x) + (y * y)]
[ :frame :clippingBox | frame intersect: clippingBox]</code></pre>
<h2>Full example</h2>
<pre><code>Object>>method: num
"comment 123"
| var1 var2 |
(1 to: num) do: [:i | |var| ^i].
Klass with: var1.
Klass new.
arr := #('123' 123.345 #hello Transcript var $@).
arr := #().
var2 = arr at: 3.
^ self abc
heapExample
"HeapTest new heapExample"
"Multiline
decription"
| n rnd array time sorted |
n := 5000.
"# of elements to sort"
rnd := Random new.
array := (1 to: n)
collect: [:i | rnd next].
"First, the heap version"
time := Time
millisecondsToRun: [sorted := Heap withAll: array.
1
to: n
do: [:i |
sorted removeFirst.
sorted add: rnd next]].
Transcript cr; show: 'Time for Heap: ' , time printString , ' msecs'.
"The quicksort version"
time := Time
millisecondsToRun: [sorted := SortedCollection withAll: array.
1
to: n
do: [:i |
sorted removeFirst.
sorted add: rnd next]].
Transcript cr; show: 'Time for SortedCollection: ' , time printString , ' msecs'</code></pre>