Thanks to both Avi and Richard for taking the time to help me out. I was confused with ifTrue: and IfFalse, I, (half), remembered that it was considered better style to bracket your boolean conditions to make things slightly more readable; but a whileTrue: is not the same as ifTrue: though... ;-)
Once running I found that it was extremely slow with Transcript show: (probably because the chars had to be converted to strings and then flushed), changing things to nextPut: really sped things up. The current method code is below. Can anyone suggest anything else I could improve to make it more 'Squeak-Like' - I was thinking that sunit tests and a little refactoring would be useful next step.
"Generate a MandelBrot image using only ASCII characters." | d r n h | Transcript show: Character cr. 1.1 to: -1.1001 by: -0.1 do: [:e | -2.0 to: 0.9601 by: 0.04 do: [:b | r := 0.0. n := 0.0. h := 127. [r * r + (n * n) < 4.0 and: [h > 32]] whileTrue: [h := h - 1. d := r. r := r * r - (n * n) + b. n := 2 * d * n + e]. Transcript nextPut: (Character value: (b > 1 ifTrue: [13] ifFalse: [h]))]. Transcript show: Character cr]
Thanks again :-)
Chris
On Wed, 27 Oct 2004 13:36:40 +1000, Christopher Barham cbarham@pobox.com wrote:
Once running I found that it was extremely slow with Transcript show: (probably because the chars had to be converted to strings and then flushed), changing things to nextPut: really sped things up. The current method code is below. Can anyone suggest anything else I could improve to make it more 'Squeak-Like' - I was thinking that sunit tests and a little refactoring would be useful next step.
"Generate a MandelBrot image using only ASCII characters." | d r n h | Transcript show: Character cr. 1.1 to: -1.1001 by: -0.1 do: [:e | -2.0 to: 0.9601 by: 0.04 do: [:b | r := 0.0. n := 0.0. h := 127. [r * r + (n * n) < 4.0 and: [h > 32]] whileTrue: [h := h - 1. d := r. r := r * r - (n * n) + b. n := 2 * d * n + e]. Transcript nextPut: (Character value: (b > 1 ifTrue: [13] ifFalse: [h]))]. Transcript show: Character cr]
You are still using #show inside the loop. Just change it to a #nextPut: or just Transcript cr. Then at the end of your code do a #endEntry to "flush".
Christopher Barham wrote:
Thanks to both Avi and Richard for taking the time to help me out. I was confused with ifTrue: and IfFalse, I, (half), remembered that it was considered better style to bracket your boolean conditions to make things slightly more readable; but a whileTrue: is not the same as ifTrue: though... ;-)
Once running I found that it was extremely slow with Transcript show: (probably because the chars had to be converted to strings and then flushed), changing things to nextPut: really sped things up. The current method code is below. Can anyone suggest anything else I could improve to make it more 'Squeak-Like' - I was thinking that sunit tests and a little refactoring would be useful next step.
Chris,
As you start to think about SUnit tests, one of the problems you'll encounter is that the images generated are pushed right to the transcript. Might I suggest that you write to a Stream? Basically you'd make the changes I suggest below. Then, if you want to write it to the transcript you can (see the end). Anyway, nice example, fun to play with...thanks for putting it out there!
"Generate a MandelBrot image using only ASCII characters." | d r n h |
add s to your list of temps
Transcript show: Character cr.
Change the previous line to: s := WriteStream on: String new.
1.1 to: -1.1001 by: -0.1 do: [:e | -2.0 to: 0.9601 by: 0.04 do: [:b | r := 0.0. n := 0.0. h := 127. [r * r + (n * n) < 4.0 and: [h > 32]] whileTrue: [h := h - 1. d := r. r := r * r - (n * n) + b. n := 2 * d * n + e].
Change Transcript on the next line to s
Transcript nextPut: (Character value: (b > 1 ifTrue: [13] ifFalse: [h]))]. Transcript show: Character cr]
Change the previous line to s nextPut: Character cr]
Now, if you want to display your output on the Transcript you could:
Transcript show: s contents.
But you could also use s in a test case since it contains the pattern generated by your program.
self assert: s = '~~~~~~~~~}}}}}}}}|||||||||||||||||||||{{{{{zylszzz{{{{|||||}}}}}}}}}}}}}}}} ~~~~~~~}}}}}}||||||||||||||||||||{{{{{{{{zzzyxuqvtVz{{{{{||||||}}}}}}}}}}}}'
or you could push s out to a web page...it is really trivial to turn your code into a Seaside application so you can produce ASCII Mandlebrot for the masses :-) Let us know if you want some guidance.
David
C. David Shaffer wrote:
But you could also use s in a test case since it contains the pattern generated by your program.
self assert: s = '~~~~~~~~~}}}}}}}}|||||||||||||||||||||{{{{{zylszzz{{{{|||||}}}}}}}}}}}}}}}}
Oops, that should have been "s contents =" not "s = "
squeak-dev@lists.squeakfoundation.org