From: 73300.2046@compuserve.COM (Jim Pannozzi 73300.2046@compuserve.com)
Subject: GNU Smalltalk SampleSpaceWithoutReplacem
Organization: GNUs Not Usenet
Distribution: gnu
Date: Wed, 6 Jan 1993 23:05:18 GMT

To:  INTERNET: bug-gnu-smalltalk-request@prep.ai.mit.edu


		"Error: the method is the responsibility of the subclass".

Date 1-6-93.

Smaltalk Version 1.1.1

Bug: Fails to do a ^data remove: super next 
		where "data" is an instance variable in a parent class holding 
			a collection;

		      "next" is a message in a parent class which randomly
			returns an element from a collection;

		      "^data remove: super next" is the method in the subclass
			that fails.	

	1. Congragulations on a remarkable achievment.  I've waited 11 years
to try out Smalltalk-80 (ever since the Aug. '81 Byte special issue on 
Smalltalk).

	2. I ported your source code to MS-DOS using the DeLorie version of
the GNU C/C++ compiler and had to make almost no changes except for activating
the shortname flag and shortening the names of the system classes (and 
altering the names accordingly in main.c).  

	   Running on a 386 with 8 Meg under DOS 3.3, all of the included test
suite ran OK and gave the expected results. The only odd thing is that when 
Smalltalk comes up it says "GC flipping to space 1... copied space is 23.5% 
done" rather than 100% done as you might expect.  This is followed by the usual
Smalltalk 1.1.1 Ready message.
 	   
      	   I tried a few things from the Robson book and everything went OK
until I tried the SampleSpaceWithoutReplacement class given as an example in 
the book ("Smalltalk-80 the Language, Goldberg and Robson) in Chapter 11 
(Three Examples that Use Collections).  No matter what I do, I can't get  
cards to send the next message to SampleSpaceWithoutReplacement.
	   
	I keep getting "Error: the method is the responsibility of the subclass".

	I could be doing Smalltalk wrong since I'm new the language.  

		Thank You

		Jim Pannozzi
		
		73300.2046@compuserve.com



"Failed Program follows"

Object  subclass: #SampleSpaceWithReplacement
	instanceVariableNames: 'data rand'
	classVariableNames: ''
	poolDictionaries: ''
	category: '' !



!SampleSpaceWithReplacement class methodsFor: 'instance creation' !

data: aSequenceableCollection
		"create an instance of SampleSpaceWith Replacement such that the"
		"argument, aSequenceableCollection is the sample space."
		
		^self new setData: aSequenceableCollection

!!


!SampleSpaceWithReplacement methodsFor: 'Processing' !



next
		"the next element selected is chosen at random from the data collection"
		"the index into the data collection is determined by obtaining a random" 
		"number between 0 and 1 and normalizing it to be within the"
		"range of the size of the data collection"

		self isEmpty

		ifTrue: [self error: 'no values exist in the sample space'].
		^data at: (rand next * data size)truncated + 1
!

next: anInteger
		"Answer an OrderedCollection contianing aninteger number of" 
		"selections from the data collection"		

		|aCollection|

		aCollection _ OrderedCollection new: anInteger.
		anInteger timesRepeat:[aCollection addLast: self next].
		^aCollection 
!

isEmpty
		"Answer whether any items remain to be sampled."

		^self size=0 
!

size
		"Answer whether any items remain to be sampled."
		^data size !

setData: aSequenceableCollection
		"The argument, aSequenceableCollection, is the sample space."
		"Create a rand num generator for sampling from the space."

		data _ aSequenceableCollection.
		rand _ Random new 

!!




"Here is the subclass that inherits from SampleSpaceWithReplacement"

SampleSpaceWithReplacement subclass: #SampleSpaceWithoutReplacement
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: '' !


!SampleSpaceWithoutReplacement methodsFor: 'Math Methods' !



next
		"the next element selected is chosen at random from the data collection"
		"the index into the data collection is determined by obtaining a random" 
		"number between 0 and 1 and normalizing it to be within the"
		"range of the size of the data collection"

		^data remove: super next
!


setData: aCollection
	
		data _ aCollection asOrderedCollection.
		rand _ Random new

!!

SampleSpaceWithoutReplacement superclass: SampleSpaceWithReplacement!



"Here is an example class from the book and it works ok"

Object  subclass: #Card
	instanceVariableNames: 'suit rank'
	classVariableNames: ''
	poolDictionaries: ''
	category: '' !



!Card methodsFor: 'basic' !


asuit "Answer the receiver's suit"
	
	^suit
!

arank "Answer the reciver's rank"

	^rank

!



setSuit: aSymbol rank: anInteger

	suit _ aSymbol.
	rank _ anInteger.

!!


!Card class methodsFor: 'instance creation'!

asuit: aSymbol arank: anInteger

	^self new setSuit: aSymbol rank: anInteger 

"	suit _ aSymbol.
	rank _ anInteger.

	^self
"

!!



Smalltalk at: #people put: (OrderedCollection new) !

^people _ SampleSpaceWithReplacement data: #(mike bob sue will mary joe ted
harpo junior kooky sally ned)!

^(people next: 4)printString! "this works ok"



Smalltalk at: #cardDeck put: (OrderedCollection new: 52) !
Smalltalk at: #cards put: (SampleSpaceWithReplacement new)!

 #(heart club spade diamond) 
	do: [:eachSuit | 1 to: 13 
		do: [:n| cardDeck add: (Card asuit: eachSuit  arank: n)]
	    ] 
 ! 


Smalltalk at: #cards put: (SampleSpaceWithoutReplacement new)!

^cards _ SampleSpaceWithoutReplacement data: cardDeck! "this seems to work"

^cards next! "this fails with the message - Error: the method is the
responsibility of the subclass"




Distribution:
  INTERNET: bug-gnu-smalltalk-request@prep.ai.mit.edu


Newsgroups: gnu.smalltalk.bug