IteratorStream.st
author Stefan Vogel <sv@exept.de>
Wed, 08 Apr 2020 19:02:35 +0200
changeset 5473 de911f462862
parent 5260 2cc9d375be98
permissions -rw-r--r--
*** empty log message ***

"{ Encoding: utf8 }"

"
 COPYRIGHT (c) 2019 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic2' }"

"{ NameSpace: Smalltalk }"

PeekableStream subclass:#IteratorStream
	instanceVariableNames:'idx iterator contentsSpecies'
	classVariableNames:''
	poolDictionaries:''
	category:'Streams'
!

!IteratorStream class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2019 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    This is a pseudo stream, which generates values by using an iterator action;
    this is a block which is called to generate the value to read.
    The block gets the index (1..) as argument.

    [author:]
        Claus Gittinger

    [instance variables:]

    [class variables:]

    [see also:]
        Iterator
"
!

example
"
        |s|

        s := IteratorStream on:[:i | i].
        s next:10.
        s next:10.

        s := IteratorStream on:[:i | i*2].
        s next:10.

"
! !

!IteratorStream class methodsFor:'instance creation'!

on:aBlock
    ^ self basicNew initialize iterator:aBlock
! !

!IteratorStream methodsFor:'accessing'!

atEnd
    ^ false

!

contentsSpecies
    "return a class of which instances will be returned, when
     parts of the collection are asked for. 
     (see upTo-kind of methods in Stream)"

    ^ contentsSpecies ? Array
!

contentsSpecies:aCollectionClass
    contentsSpecies := aCollectionClass.
!

iterator:aOneArgBlock
    iterator := aOneArgBlock.
!

position
    ^ idx
!

position:newPosition
    idx := newPosition
! !

!IteratorStream methodsFor:'initialization'!

initialize
    super initialize.
    idx := 0.
! !

!IteratorStream methodsFor:'reading'!

next
    "return the next element from the stream by evaluating the nextBlock"

    iterator notNil ifTrue:[
        idx := idx + 1.
        ^ iterator value:idx
    ].
    self error:'iterator is undefined'
! !

!IteratorStream class methodsFor:'documentation'!

version_CVS
    ^ '$Header$'
! !