[Pkg] The Trunk: Collections-ar.307.mcz

commits at source.squeak.org commits at source.squeak.org
Sat Feb 13 23:38:19 UTC 2010


Andreas Raab uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-ar.307.mcz

==================== Summary ====================

Name: Collections-ar.307
Author: ar
Time: 13 February 2010, 3:37:40.191 pm
UUID: 310c7f68-38f8-334c-b7bd-89b28955a4ff
Ancestors: Collections-nice.306

Merge class Generator from http://source.lukas-renggli.ch/continuations/Generator-ar.5.mcz

=============== Diff against Collections-nice.306 ===============

Item was added:
+ ----- Method: Generator>>contents (in category 'accessing') -----
+ contents
+ 	"Answer the contents of this generator. Do not call this method on infinite generators."
+ 
+ 	| stream |
+ 	stream := (Array new: 10) writeStream.
+ 	[ self atEnd ]
+ 		whileFalse: [ stream nextPut: self next ].
+ 	^ stream contents!

Item was added:
+ ----- Method: Generator>>atEnd (in category 'testing') -----
+ atEnd
+ 	"Answer whether the receiver can access any more objects."
+ 
+ 	^ continue isNil!

Item was added:
+ ----- Method: Generator>>fork (in category 'private') -----
+ fork
+ 	| result |
+ 	home := thisContext.
+ 	block reentrant value: self.
+ 	thisContext swapSender: continue.
+ 	result := next.
+ 	continue := next := home := nil.
+ 	^ result!

Item was added:
+ ----- Method: Generator>>next (in category 'accessing') -----
+ next
+ 	"Generate and answer the next object in the receiver."
+ 
+ 	^ self atEnd ifFalse: [
+ 		home swapSender: thisContext sender.
+ 		continue := thisContext swapSender: continue
+ 	]!

Item was added:
+ ----- Method: Generator>>reset (in category 'public') -----
+ reset
+ 	"Reset the generator, i.e., start it over"
+ 	continue ifNotNil:[continue unwindTo: home].
+ 	next := nil.
+ 	continue := thisContext.
+ 	[ self fork ] value!

Item was added:
+ Stream subclass: #Generator
+ 	instanceVariableNames: 'block next continue home'
+ 	classVariableNames: ''
+ 	poolDictionaries: ''
+ 	category: 'Collections-Streams'!
+ 
+ !Generator commentStamp: 'ar 2/10/2010 20:51' prior: 0!
+ A Generator transforms callback interfaces into stream interfaces. 
+ 
+ When a producer algorithm provide results as callbacks (blocks) and a consumer algorithm expects streamable input, a Generator transforms one into the other, for example:
+ 
+ 	| generator |
+ 	generator := Generator on: [:g| Integer primesUpTo: 100 do:[:prime| g yield: prime]].
+ 	[generator atEnd] whileFalse:[Transcript show: generator next].
+ 
+ Instance Variables
+ 	block:		<BlockClosure> The block associated with the generator.
+ 	continue:	<MethodContext>	The continuation to return to.
+ 	home:		<MethodContext>	The home (root) context of the activated block
+ 	next:		<Object>		The next object to return from the Generator.
+ !

Item was added:
+ ----- Method: Generator>>peek (in category 'accessing') -----
+ peek
+ 	"Answer the upcoming object of the receiver."
+ 
+ 	^ next!

Item was added:
+ ----- Method: Generator>>printOn: (in category 'printing') -----
+ printOn: aStream
+ 	aStream nextPutAll: self class name; nextPutAll: ' on: '; print: block!

Item was added:
+ ----- Method: Generator classSide>>on: (in category 'instance-creation') -----
+ on: aBlock
+ 	^ self basicNew initializeOn: aBlock!

Item was added:
+ ----- Method: Generator>>close (in category 'accessing') -----
+ close
+ 	"Close the receiving generator and unwind its ensure-blocks."
+ 
+ 	continue ifNotNil:[continue unwindTo: home].
+ 	continue := block := next := nil!

Item was added:
+ ----- Method: Generator>>value: (in category 'public') -----
+ value: anObject 
+ 	"Allows passing generators as arguments to methods expecting blocks.
+ 	A synonym for #yield: / #nextPut:."
+ 	^ self nextPut: anObject!

Item was added:
+ ----- Method: Generator>>yield: (in category 'public') -----
+ yield: anObject 
+ 	"Yield the next value to the consumer of the generator.
+ 	A synonym for #nextPut:"
+ 	^ self nextPut: anObject!

Item was added:
+ ----- Method: Generator>>nextPut: (in category 'accessing') -----
+ nextPut: anObject
+ 	"Add anObject into the generator. A synonym to #yield: and value:."
+ 
+ 	| previous |
+ 	previous := next.
+ 	next := anObject.
+ 	continue := thisContext swapSender: continue.
+ 	^ previous!

Item was added:
+ ----- Method: Generator>>initializeOn: (in category 'initialization') -----
+ initializeOn: aBlock
+ 	block := aBlock.
+ 	self reset!

Item was added:
+ ----- Method: Generator>>size (in category 'accessing') -----
+ size
+ 	"A generator does not know its size."
+ 
+ 	^ self shouldNotImplement!



More information about the Packages mailing list