[PATCH] Add Iterable>>#nextPutAllOn:

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] Add Iterable>>#nextPutAllOn:

Paolo Bonzini-2
A new method that makes sense in Iterable is the double-dispatch version
of #nextPutAll:.

Paolo

2008-08-05  Paolo Bonzini  <[hidden email]>

        * kernel/Stream.st: Implement #nextPutAll: polymorphically.  Implement
        #nextPutAllOn:.
        * kernel/Iterable.st: Implement #nextPutAllOn:.
        * kernel/SeqCollect.st: Implement #nextPutAllOn:.
        * kernel/FileDescr.st: Use #nextPutAllOn: to implement #contents.

diff --git a/kernel/Iterable.st b/kernel/Iterable.st
index ca19a3b..e88d7d2 100644
--- a/kernel/Iterable.st
+++ b/kernel/Iterable.st
@@ -203,6 +203,13 @@ all possible) are left to the subclasses.'>
  ^result
     ]
 
+    nextPutAllOn: aStream [
+        "Write all the objects in the receiver to aStream"
+
+ <category: 'streaming'>
+ self do: [ :each | aStream nextPut: each ]
+    ]
+
     readStream [
  "Return a stream with the same contents as the receiver."
 
diff --git a/kernel/SeqCollect.st b/kernel/SeqCollect.st
index 036bb3a..3d9588f 100644
--- a/kernel/SeqCollect.st
+++ b/kernel/SeqCollect.st
@@ -781,6 +781,12 @@ some access and manipulation methods.'>
     ifFalse: [self first species join: self separatedBy: sepCollection]
     ]
 
+    nextPutAllOn: aStream [
+ "Write all the objects in the receiver to aStream"
+
+ aStream next: self size putAll: self startingAt: 1
+    ]
+
     readStream [
  "Answer a ReadStream streaming on the receiver"
 
diff --git a/kernel/Stream.st b/kernel/Stream.st
index b366db0..79c06a2 100644
--- a/kernel/Stream.st
+++ b/kernel/Stream.st
@@ -115,7 +115,7 @@ provide for writing collections sequentially.'>
  <category: 'accessing-reading'>
  | stream |
  stream := WriteStream on: (self species new: 10).
- self do: [:each | stream nextPut: each].
+ self nextPutAllOn: stream.
  ^stream contents
     ]
 
@@ -225,26 +225,20 @@ provide for writing collections sequentially.'>
  "Write all the objects in aCollection to the receiver"
 
  <category: 'accessing-writing'>
- | coll |
- aCollection isSequenceable
-    ifTrue:
- [^self
-    next: aCollection size
-    putAll: aCollection
-    startingAt: 1].
+ aCollection nextPutAllOn: self.
+ ^aCollection
+    ]
 
- "Try to detect a Stream."
- [aCollection atEnd] on: MessageNotUnderstood
-    do: [:ex | ^self nextPutAll: (self species withAll: aCollection)].
+    nextPutAllOn: aStream [
+        "Write all the objects in the receiver to aStream"
 
- "If we are in a stream, try to facilitate buffering."
- [aCollection atEnd] whileFalse:
- [coll := aCollection nextHunk.
+ | coll |
+ [aStream atEnd] whileFalse:
+ [coll := aStream nextHunk.
  self
     next: coll size
     putAll: coll
     startingAt: 1].
- ^aCollection
     ]
 
     next: anInteger put: anObject [

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk